/* Behavior for Williams Apothecary */
/* Last updated on 5 Apr 2011 */

window.onload = function() {
	navImgSwap();
	contactFocus();
	contactSend();
	refillSend();

}

/* -- checker function - checks to see if variables in each function exist -- */

function checkVars(e) {
	var x = e.split(",");
	var pass = true;
	checkerInvalid = new Array;

	for(i=0; i<x.length; i++) {
		
		if(!eval(x[i])) {
		pass = false; 
		checkerInvalid[checkerInvalid.length] = x[i];
		}
		
	}
	
	return pass;

}



/* navigation image swap */
function navImgSwap() {

	var W3CDOM = (document.createElement && document.getElementsByTagName);

	var mouseOvers = new Array();
	var mouseOuts = new Array();


	if (!W3CDOM) return;
	var nav = document.getElementById('int-subnav');
	var imgs = nav.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++) {
		imgs[i].onmouseover = mouseGoesOver;
		imgs[i].onmouseout = mouseGoesOut;
		var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
		mouseOuts[i] = new Image();
		mouseOuts[i].src = imgs[i].src;
		mouseOvers[i] = new Image();
		mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "-o" + suffix;
		imgs[i].number = i;
	}

function mouseGoesOver() {
	this.src = mouseOvers[this.number].src;
}

function mouseGoesOut() {
	this.src = mouseOuts[this.number].src;
}

} 

/* -- on contact page, places cursor focus on name field on page load -- */

function contactFocus() {
	
	if(!document.getElementById("name")) {
		return;
	}
	
	var contact_name = document.getElementById("name");
	
	contact_name.focus();
}

/* -- validate contact form -- */

function valContactForm() {
	
	if(!checkVars('document.contactform.name, document.contactform.email')) {
		return;
	}
	
	var name_value = document.contactform.name.value;
	var email_value = document.contactform.email.value;
	var errormsg = '';


	if (name_value == '') {
		errormsg = errormsg + '-You must input your Name\n';
	}
		
	if (email_value == '') {
		errormsg = errormsg + '-You must input your Email Address\n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function contactSend() {
	
	
	if(!checkVars('document.getElementById("contactform")')) {
		return;
	}
	
	var form_name = document.getElementById("contactform");
	
	form_name.onsubmit = function() {
		return valContactForm();
	}
}


/* -- validate prescription refill form -- */

function valRefillForm() {
	
	if(!checkVars('document.refillform.name, document.refillform.address, document.refillform.zip, document.refillform.phone, document.refillform.presnum')) {
		return;
	}
	
	var name_value = document.refillform.name.value;
	var address_value = document.refillform.address.value;
	var zip_value = document.refillform.zip.value;
	var phone_value = document.refillform.phone.value;
	var pres_value = document.refillform.presnum.value;
	

	var errormsg = '';


	if (name_value == '') {
		errormsg = errormsg + '-You must input your Name\n';
	}
		
	if (address_value == '') {
		errormsg = errormsg + '-You must input your Address\n';
	}
	
	if (zip_value == '') {
		errormsg = errormsg + '-You must input your Zip Code\n';
	}
	
	if (!zc_val(zip_value)) {
		errormsg = errormsg + '-You must input a valid Zip Code\n';
	}
	
	if (phone_value == '') {
		errormsg = errormsg + '-You must input a valid Phone Number\n';
	}

	if (pres_value == '') {
		errormsg = errormsg + '-You must input a Prescription Number\n';
	}

	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function zc_val(z) {

	var zs = "17501,17503,17504,17505,17508,17566,17512,17516,17517,17518,17519,17520,17022,17521,17522,17527,17528,17529,17532,17533,17534,17535,17536,17537,17601,17602,17603,17604,17605,17606,17607,17608,17538,17540,17543,17545,17547,17549,17550,17551,17552,17554,17557,17560,17562,17564,17565,17566,17022,17567,17568,17572,17575,17576,17577,17578,17572,17580,17581,17582,17583,17584,17585";
	var zslice = z.slice(0,5);

	if(zs.match(zslice) && zslice.length == 5) {
		return true;
	}

	else {
		return false;
	}

}

function refillSend() {
	
	
	if(!checkVars('document.getElementById("refillform")')) {
		return;
	}
	
	var form_name = document.getElementById("refillform");
	
	form_name.onsubmit = function() {
		return valRefillForm();
	}
}

