// Global Variables:

var isNav;
var isIE;
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") isNav = true;
	else isIE = true;
} // if

function toggle ( section )
{
	var target = document.getElementById (section);
	if (target.style.display == "")
	{
		target.style.display = "none" ;
	}
	else
	{
		target.style.display = "" ;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validateData(value,type,obj) {
			var valid = new Array;
			var isValid = false;
			var newValue;
			var testStr;
			var msg;
			switch (type) {
				case "cardNumber":
					testStr = ("" + value).replace(/\D/g,"");
					var cardTypeEl = document.getElementById ("cardType");
					var cardVal = cardTypeEl.options [cardTypeEl.selectedIndex].text;
					isValid = (testStr.length == 16) || (testStr.length == 15)
					if (isValid)
					{
							switch (cardVal) {
								case "MasterCard":
								case "Visa":
//updated 12/2/05 by eswanson - no longer adding dashs to CC numbers
									//if (testStr.length == 16) {
									if (testStr.length != 16) {

										//newValue = testStr.substr(0,4) + "-" +  testStr.substr(4,4) + "-" +  testStr.substr(8,4) + "-" + testStr.substr(12,4);
									//}
									//else {
										isValid = false;
										msg = "Invalid " +cardVal+" number\n\n 16 digit card number required";
									}
									break;
								case "American Express":
									//if (testStr.length == 15) {
									if (testStr.length != 15) {
										//newValue = testStr.substr(0,4) + "-" +  testStr.substr(4,6) + "-" +  testStr.substr(10,5);
									//}
									//else {
										isValid = false;
										msg = "Invalid " +cardVal+" number\n\n 15 digit card number required";
									}
									break;
							}
					} // if
					else msg = "Invalid credit card number\n\n Please enter a valid credit card number";
					break;
				case "zip":
					testStr = "";
					if (value != "")
						testStr = value.replace(/\D/g,"");
					isValid = (testStr.length == 9 || testStr.length == 5)
					if (isValid) {
						newValue = testStr.substr(0,5) + ((testStr.length == 9) ? " - " + testStr.substr(5,4) : "");
					} // if
					else msg = "Please enter a valid zip code\n\n-- 5 or 9 digits are required --";
					break;
				case "telephone":
					valid.isValid = true;
					testStr = value.replace(/^1/,"");
					var num = testStr.replace(/\D/g,"");
					var length = num.length;
					if (length == 10) newValue = "(" + num.substr(0,3) + ") " + num.substr(3,3) + "-" +  num.substr(6,4);
					//else if (length == 7) newValue = num.substr(0,3) + "-" +  num.substr(3,4);
					//else if (length == 5) newValue = num.substr(0,1) + "-" +  num.substr(1,4);
					else {
						msg = "Please enter a valid phone number\n\nEnter full number with area code\n-- 10 digits required --";
						isValid = false;
					} // else
					break;
				case "email":
					if (value == false ||  (value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1))
						msg = "Please enter a valid email address";
					else
						newValue = value; //isValid = (value.search(/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/) != -1);
					break;
			} // switch
			if (msg && value != "")
			{
				alert(msg);
				obj.focus();
			}
			return(newValue);
} // function validateData

function validate(event) {
	var form = document.forms[0];
	var validNames = new Array;
	var validTypes = new Array;
	var isValid = false;
	var newVal = "";
	var vObj;
	var obj;
	var name;
	var valid;
	var value;
	var type;
	var re;
	var i;

	obj = getEvent(event);
	if (obj) {
		name = obj.name || false;
		value = obj.value || false;
		if (name && value)
		{
			newVal = validateData(value,name, obj)
			if (newVal)
				obj.value = newVal;
//			if (!newVal && obj.value != "") obj.focus();
//				else if (newVal) obj.value = newVal;
		}
		/*if (form.valid) {
			re = new RegExp(name);
			valid = "" + form.valid.value;
			if (valid.search(re) != -1) {
				valid = valid.split(",");
				validNames = new Array;
				validTypes = new Array;
				for (i=0; i < valid.length; i=i+2) {
					validNames[i] = valid[i];
					validTypes[i] = valid[i+1];
				} // for
				//alert(validNames[0]);
				//alert(validTypes[0]);
				for (i=0; i < validNames.length; i++) if (name == validNames[i]) type = validTypes[i]
				newVal = validateData(value,type)
				if (!newVal && obj.value != "") obj.focus();
				else if (newVal) obj.value = newVal;
			} // if
		} // if there is validation
		*/
	} // if obj
} // function handleChage

function test() {
	alert("here")
} // funciton test

/*
function checkShip() {
	var index = document.checkout.destination.selectedIndex;
	var value = document.checkout.destination.options[index].value;
	if (value == "null" || value == null) {
	  	alert("Please Select a State to Continue");
		document.checkout.destination.focus();
	} else {
		document.checkout.ship_status.value = "calculate";
		document.checkout.submit();
	} // else
} // funciton checkShip
*/

function checkBrowser() {
	return(isNav || isIE)
} // function checkBrowser

function updateAddressFields() {
	alert("here");
	form.fname_ship.value = form.fname_bill.value;
	form.lname_ship.value = form.lname_bill.value;
	form.address1_ship.value = form.address1_bill.value;
	form.address2_ship.value = form.address2_bill.value;
	form.city_ship.value = form.city_bill.value;
	form.zip_ship.value = form.zip_bill.value;
	alert("here2");
} // function updateAddressFields

function checkFields() {
	var hasData = false;
	var obj = new Array;
	var str = "";
	var required;
	var focusOn;
	var button;
	var index;
	var field;
	var names;
	var value;
	var type;
	var name;
	var same;
	var req;
	var re;

	var form = document.forms[0];
	if (form.same_ship) same = form.same_ship.checked;

	switch (form.name) {
		case "checkout":
//mliskin 3/29/07 removed all payment fields
			if (same) {
//eswanson 12/5/05 removed ccname, added heardabout
				req = "fname_bill,lname_bill,address1_bill,city_bill,state_bill,zip_bill,telephone_bill,email";
				names = "First Name (billing),Last Name (billing),Address (billing),City (billing),State (billing),Zip Code (billing),Telephone (billing),Email";
			} else {
				req = "fname_bill,lname_bill,address1_bill,city_bill,state_bill,zip_bill,fname_ship,lname_ship,address1_ship,city_ship,state_ship,zip_ship,telephone_ship,email";
				names = "First Name (billing),Last Name (billing),Address (billing),City (billing),State (billing),Zip Code (billing),First Name (shipping),Last Name (shipping),Address (shipping),City (shipping),State (shipping),Zip Code (shipping),Telephone (shipping),Email";
			} //else
			required = req.split(",");
			names = names.split(",");
			break;
		case "catalog":
			req = "firstName,lastName,address1,city,state,zip,email";
			names = "First Name,Last Name,Address,City,State,Zip Code,Email";
			required = req.split(",");
			names = names.split(",");
			break;
		case "newsletter":
			req = "email";
			names = "Email";
			required = req.split(",");
			names = names.split(",");
			break;
	} // switch

	if (required && names) {
		for (index = 0; index < form.elements.length; index++) {
			field = form.elements[index];
			name = field.name;

			if (field.type == "select-one") {
				value = field.options[field.selectedIndex].value;
				//alert(name + " = " + value);
			} // if
			else value = field.value;

			re = new RegExp(name);
			if (field.name == "cardType" && field.selected > 0) type = true;

			if (req.search(re) != -1 && (value == "" || value == "null" || value == null)) {
				for (i=0;i<required.length;i++) {
					if (name == required[i]) {
						str += (str == "" ? "" : "\n") + names[i];
					} // if
				} // for
			} // if
		} // for each element in the form
		if (type == "none") str += (str == "" ? "" : "\n" ) + "Credit Card Type";
	} // if
	if (str != "") {
		if (focusOn) focusOn.focus();
		if (str.split("\n").length == 1) alert("Please fill in your " + str);
		else alert("Please fill in all required fields \n\nThere are " + str.split("\n").length + " fields missing information\n\n" + str);
		return(false);
	} // if there is a field missing information
	else return(true);
} // function checkFields

function getEvent(event) {
	var obj;
	if (isNav) obj = event.target;
	else if (isIE) obj = event.srcElement;
	else obj = false;
	return(obj)
} // function getEvent

function handleClick(event) {
	var hasData = false;
	var fieldStr = "";
	var ok = true;
	var str = "";
	var elements;
	var focusOn;
	var parent;
	var value;
	var index;
	var name;
	var field;
	var eObj;
	var obj;
	var id;

	//alert("here");
	obj = getEvent(event);
	if (obj) {
		if (document.checkout) elements = checkout.elements;
		if (obj.name) name = obj.name;

		switch (name) {
			case "sub":
				for (index = 0; index < elements.length; index++) {
					field = elements[index];
					if (field.getAttribute("required") == 1) {
						hasData = (field.value != "" && field.value != " " && field.value != null);
						if (!hasData) {
							if (!focusOn && field.type != "hidden") focusOn = field;
							str += ((str == "") ? "" : "\n") + field.id;
						} // if the data is empty highlight the cell
					} // if the element is required
				} // for each element in the form
				if (str == "") {
					checkout.submit();
				} // if
				else {
					if (focusOn) focusOn.focus();
					alert("Please fill in all required fields \n\nThere are " + str.split("\n").length + " fields missing information\n\n" + str);
					// alert(str);
				} // else
				break;

			case "ship":
				index = document.cartForm.state.selectedIndex;
				value = document.cartForm.state.options[index].value;
				alert("value = " + value);
				if (value == "null" || value == null) {
				  	alert("Please Select a State to Continue");
					document.cartForm.state.focus();
				} else {
					document.cartForm.submit();
				} // else
				break;
			case "addButton":
				document.productForm.action = "product.asp";
				document.productForm.act.value = "add";
				document.productForm.submit();
				break;

			case "back":
				history.go(-1);
				break;
		} // switch
	} // if there is an object returned
	else {
		alert("there is no object");
	}
} // function handleClick


