// JavaScript Document
var newWindow
function OpenWin(url,width,height) {
	newWindow = window.open(url,"NewWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,titlebar=no,width="+width+",height="+height)
}

function GotoSite(form) {
var result = ""
for (var i = 0; i < form.bookseller.length; i++) {
	if (form.bookseller.options[i].selected) {
		result += form.bookseller.options[i].value
	}
}
  window.open(result,'NewWindow','location=yes,status=yes,resizable=yes,copyhistory=yes,toolbar=yes,scrollbars=yes,menubar=yes');
}


var error = "";
var rfString = "";
var form = "";
function ValidateForm(form) {
	error = "";
	for (i=0; i<form.elements.length; i++) {
		// FIRST CHECK TO SEE IF REQUIRED FIELDS EXIST
		if (form.elements[i].name.indexOf("reqfields") != -1) {
			rfString = form.reqfields.value;
			break;
		}
	}
	if (rfString) {
		validateReqFields(form)
	}
	else {
		validateAllFields(form)
	}
	if (error) {
		alert('This form could not be processed because of the following errors:\n\n' + error);
		return false;
	}
}

function validateReqFields(form) {
	rfString = form.reqfields.value;
	rfArray = rfString.split(",");
	rfCount = rfArray.length-1;
	rnametemp = "";
	for (x=0; x<=rfCount; x++) {
		for (i=0; i < form.elements.length; i++) {
			if (form.elements[i].name == rfArray[x]) {
				if ((form.elements[i].type == 'radio' || form.elements[i].type == 'checkbox') && rnametemp != form.elements[i].name) {
					rnametemp = form.elements[i].name;
					checkRadio(rnametemp,form);
				}
				else if (form.elements[i].name.indexOf("email") != -1) {
					checkEmail(form.elements[i].value)
					}
				else if (form.elements[i].value == "") {
					if (form.elements[i].name == rfArray[x]) {
						error = error + '* ' + form.elements[i].name + ' is empty\n'
					}
				}
			}
		}
	}
}

function validateAllFields(form) {
	rnametemp = "";
	for (i=0; i < form.elements.length; i++) {
		if (form.elements[i].type == 'radio' && rnametemp != form.elements[i].name) {
			rnametemp = form.elements[i].name;
			checkRadio(rnametemp,form);
		}
		else if (form.elements[i].name.indexOf("email") != -1) {
			checkEmail(form.elements[i].value)
			}
		else if (form.elements[i].value == "") {
			error = error + '* ' + form.elements[i].name + ' is empty\n'
		}
	}
}

function validateFundraising(form) {
	error = "";
	if (document.freesample.sample.checked || document.freesample.studentpacket.checked) {
		validateAllFields(form);
	}
	else {
		validateReqFields(form);
	}
	if (error) {
		alert('This form could not be processed because of the following errors:\n\n' + error);
		return false;
	}
}


function checkEmail (strng) {
//var error="";
if (strng == "") {
   error = error + "* You didn't enter an email address.\n";
}
else {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = error + "* Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = error + "* The email address contains illegal characters.\n";
       }
    }
}
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// CHECK RADIO

function checkRadio(fname,form) {
	valcheck = "";
	alMsg = "";
	maxnum = form.elements[fname].length
	for (z=0;z<maxnum;z++) {
		if (form.elements[fname][z].checked) {
			valcheck = "1";
			break;
		}
	}
	if (!valcheck) {
		error = error + '* ' + fname + ' is not checked\n';
	}
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
return error;
}    

function showSubNav(name) {
	//xstatus = document.getElementById(name).style.display;
	if (document.freesample.sample.checked || document.freesample.studentpacket.checked) {
		document.getElementById(name).style.display = "block";
	}
	else {
		document.getElementById(name).style.display = "none";
	}
}

function show(element) {
	element = document.getElementById(element);
	if (element.style.display == "block") {
		element.style.display = "none";
	}
	else {
		element.style.display = "block";
	}
}

function showHide2(element,eList) {
	lArray = eList.split(",");
	for (i=0;i<lArray.length;i++) {
		document.getElementByID(lArray[i]).style.display = "none";
	}
	document.getElementByID(element).style.display = "block";
}

function addValue(form,fname) {
	var fnvalue = form.elements[fname];
	if (fnvalue.value == "") {
		fnvalue.value="1";
	}
	else {
		fnvalue.value="";
	}
}






var TINY={};

function T$(i){return document.getElementById(i)}

TINY.box=function(){
	var p,m,b,fn,ic,iu,iw,ih,ia,f=0;
	return{
		show:function(c,u,w,h,a,t){
			if(!f){
				p=document.createElement('div'); p.id='tinybox';
				m=document.createElement('div'); m.id='tinymask';
				b=document.createElement('div'); b.id='tinycontent';
				document.body.appendChild(m); document.body.appendChild(p); p.appendChild(b);
				m.onclick=TINY.box.hide; window.onresize=TINY.box.resize; f=1
			}
			if(!a&&!u){
				p.style.width=w?w+'px':'auto'; p.style.height=h?h+'px':'auto';
				p.style.backgroundImage='none'; b.innerHTML=c
			}else{
				b.style.display='none'; p.style.width=p.style.height='100px'
			}
			this.mask();
			ic=c; iu=u; iw=w; ih=h; ia=a; this.alpha(m,1,80,3);
			if(t){setTimeout(function(){TINY.box.hide()},1000*t)}
		},
		fill:function(c,u,w,h,a){
			if(u){
				p.style.backgroundImage='';
				var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
				x.onreadystatechange=function(){
					if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}
				};
				x.open('GET',c,1); x.send(null)
			}else{
				this.psh(c,w,h,a)
			}
		},
		psh:function(c,w,h,a){
			if(a){
				if(!w||!h){
					var x=p.style.width, y=p.style.height; b.innerHTML=c;
					p.style.width=w?w+'px':''; p.style.height=h?h+'px':'';
					b.style.display='';
					w=parseInt(b.offsetWidth); h=parseInt(b.offsetHeight);
					b.style.display='none'; p.style.width=x; p.style.height=y;
				}else{
					b.innerHTML=c
				}
				this.size(p,w,h)
			}else{
				p.style.backgroundImage='none'
			}
		},
		hide:function(){
			TINY.box.alpha(p,-1,0,3)
		},
		resize:function(){
			TINY.box.pos(); TINY.box.mask()
		},
		mask:function(){
			m.style.height=TINY.page.total(1)+'px';
			m.style.width=''; m.style.width=TINY.page.total(0)+'px'
		},
		pos:function(){
			var t=(TINY.page.height()/2)-(p.offsetHeight/2); t=t<10?10:t;
			p.style.top=(t+TINY.page.top())+'px';
			p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px'
		},
		alpha:function(e,d,a){
			clearInterval(e.ai);
			if(d==1){
				e.style.opacity=0; e.style.filter='alpha(opacity=0)';
				e.style.display='block'; this.pos()
			}
			e.ai=setInterval(function(){TINY.box.ta(e,a,d)},20)
		},
		ta:function(e,a,d){
			var o=Math.round(e.style.opacity*100);
			if(o==a){
				clearInterval(e.ai);
				if(d==-1){
					e.style.display='none';
					e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''
				}else{
					e==m?this.alpha(p,1,100):TINY.box.fill(ic,iu,iw,ih,ia)
				}
			}else{
				var n=Math.ceil((o+((a-o)*.5))); n=n==1?0:n;
				e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'
			}
		},
		size:function(e,w,h){
			e=typeof e=='object'?e:T$(e); clearInterval(e.si);
			var ow=e.offsetWidth, oh=e.offsetHeight,
			wo=ow-parseInt(e.style.width), ho=oh-parseInt(e.style.height);
			var wd=ow-wo>w?0:1, hd=(oh-ho>h)?0:1;
			e.si=setInterval(function(){TINY.box.ts(e,w,wo,wd,h,ho,hd)},20)
		},
		ts:function(e,w,wo,wd,h,ho,hd){
			var ow=e.offsetWidth-wo, oh=e.offsetHeight-ho;
			if(ow==w&&oh==h){
				clearInterval(e.si); p.style.backgroundImage='none'; b.style.display='block'
			}else{
				if(ow!=w){var n=ow+((w-ow)*.5); e.style.width=wd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				if(oh!=h){var n=oh+((h-oh)*.5); e.style.height=hd?Math.ceil(n)+'px':Math.floor(n)+'px'}
				this.pos()
			}
		}
	}
}();

TINY.page=function(){
	return{
		top:function(){return document.documentElement.scrollTop||document.body.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},
		total:function(d){
			var b=document.body, e=document.documentElement;
			return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):
			Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();