// JavaScript Document

// need a base list of inks to prevent list from degenerating
rootInks = new Array();
rootInks[00] = "";
rootInks[01] = "Black";
rootInks[02] = "White";
rootInks[03] = "Blue";
rootInks[04] = "Green";
rootInks[05] = "Red";
rootInks[06] = "Yellow";
rootInks[07] = "Pink";


// define banned color/ink combos
banned = "black:black, black:navy, black:green, black:black/red, white:white, white:white/black, white:white/navy, white:white/pink, white:white/red, blue:navy, blue:royal, green:green, red:red, yellow:yellow, yellow:gold";

function alterInkList(prefInk) {
	if (document.optionsform.color) {
		var colorList = document.optionsform.color;
		var inkList = document.optionsform.ink;
		if (inkList.options)
			var selectID = inkList.options[inkList.selectedIndex].value;
		else
			var selectID = prefInk;
		choice = rootInks.length;
		inkList.length = 0;
		j = 1;
		for (var i = 0; i < choice; i++) {
			colorName = colorList.options[colorList.selectedIndex].text;
			colorName = colorName.toLowerCase();
			inkName = rootInks[i];
			inkName = inkName.toLowerCase();
			searchString = inkName+":"+colorName;
			if (inkList.options) {
				inkList.options[0] = new Option("");
				inkList.options[0].value = 00;
				if (banned.indexOf(searchString) == -1) {
					inkList.options[j] = new Option(rootInks[i]);
					inkList.options[j].value = i;
					//alert(inkList.options[j].text + ' = ' + inkList.options[j].value);
					//alert(inkList.options[j].value +'=='+ parseInt(selectID));
					if (inkList.options[j].value == parseInt(selectID)) {
						inkList.options[j].selected = true;
					}
					j = j + 1;
				}
			}
		}
		if (document.layers) {
			history.go(0);
		}
	}
}


function checkAmount(amountField) {
	if (amountField.value == "" || amountField.value < 1) {
		alert('The minimum gift certificate amount is $1.00');
		amountField.focus();
		return false;
	}
}


function checkCard(form)
{
	if (form.cardnumber.value=="")
	{
		alert('You must enter a credit card number.');
		form.cardnumber.focus();
		return false;
	}
	if (form.cardexpmonth.value=="")
	{
		alert('You must select an expiration month.');
		form.cardexpmonth.focus();
		return false;
	}
	if (form.cardexpyear.value=="")
	{
		alert('You must select an expiration year.');
		form.cardexpyear.focus();
		return false;
	}
	if (form.cvmvalue.value=='')
	{
		alert('You must enter the CV Number. If you don\'t have a CV Number, please use the PayPal option to pay with a credit card.');
		form.cvmvalue.focus();
		return false;
	}
}


function checkGC()
{
	theForm = document.gc_details;
	for (i=0; i<theForm.elements.length; i++){
		// Text fields (name and email address) are required
		subName = '';
		fieldName = theForm.elements[i].name;
		fieldValue = theForm.elements[i].value;
		subName = fieldName.substr(0,4);
		if (subName == 'name' && fieldValue == "") {
			alert('You must enter a recipient name for your gift certificate.');
			theForm.elements[i].select();
			return false;
		}
		if (subName == 'emai' && echeck(fieldValue) == false) {
			alert('That does not appear to be a valid e-mail address. Please double-check it.');
			theForm.elements[i].select();
			return false;
		}
	}
	return true;
}


function checkOrder(form)
{
	if (form.first_name.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.first_name.focus();
		return false;
	}
	if (form.last_name.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.last_name.focus();
		return false;
	}
	if (form.email.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.email.focus();
		return false;
	}
	if (echeck(form.email.value)==false)
	{
		alert('Your email address does not appear to be valid. Please enter a valid email address.');
		form.email.value="";
		form.email.focus();
		return false;
	}
	if (form.address.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.address.focus();
		return false;
	}
	if (form.city.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.city.focus();
		return false;
	}
	if (form.state.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.state.focus();
		return false;
	}
	if (form.zip.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.zip.focus();
		return false;
	}
	if (form.country.value=="")
	{
		alert('You have left a required field blank. Please fill in all required fields.');
		form.country.focus();
		return false;
	}
	if (form.password.value != "" && form.password.value != form.confirm_password.value)
	{
		alert('The passwords you entered do not match. Please re-enter them.');
		form.password.value ="";
		form.confirm_password.value ="";
		form.password.focus();
		return false;
	}
	if (form.ship_method.value=="")
	{
		form.ship_method.value = 1;
	}
	if (form.ship_method.value!="3" && form.ship_method.value!="4" && ((form.ship_country.value=="" && form.country.value!="US" && form.country.value!="PR" && form.country.value!="GU" && form.country.value!="AS" && form.country.value!="VI" && form.country.value!="PW" && form.country.value!="FM" && form.country.value!="MH" && form.country.value!="MP") || (form.ship_country.value!="" && form.ship_country.value!="US" && form.ship_country.value!="PR" && form.ship_country.value!="GU" && form.ship_country.value!="AS" && form.ship_country.value!="VI" && form.ship_country.value!="PW" && form.ship_country.value!="FM" && form.ship_country.value!="MH" && form.ship_country.value!="MP")))
	{
		alert('International orders must be shipped via Global Priority or Global Express Mail.');
		form.ship_method.focus();
		return false;
	}
	if (form.ship_method.value!="1" && form.ship_method.value!="2" && ((form.ship_country.value=="" && (form.country.value=="US" || form.country.value=="PR" || form.country.value=="GU" || form.country.value=="AS" || form.country.value=="VI" || form.country.value=="PW" || form.country.value=="FM" || form.country.value=="MH" || form.country.value=="MP")) || (form.ship_country.value!="" && (form.ship_country.value=="US" || form.ship_country.value=="PR" || form.ship_country.value=="GU" || form.ship_country.value=="AS" || form.ship_country.value=="VI" || form.ship_country.value=="PW" || form.ship_country.value=="FM" || form.ship_country.value=="MH" || form.ship_country.value=="MP"))))
	{
		alert('Domestic orders must be shipped via USPS Priority or Express Mail.');
		form.ship_method.focus();
		return false;
	}
	return true;
}


function checkPass(form) {
	if (form.return_password.value != "" && form.return_password.value != form.confirm_password.value)
	{
		alert('The passwords you entered do not match. Please re-enter them.');
		form.return_password.value ="";
		form.confirm_password.value ="";
		form.return_password.focus();
		return false;
	}
}


function checkSize(sizeYes,colorYes,inkYes) {
	if (colorYes) {
		thisObject = document.optionsform.color;
		if (thisObject.value == "") {
			alert("Please select a Color.");
			return false;
		}
	}
	if (inkYes) {
		thisObject = document.optionsform.ink;
		if (thisObject.value == "0") {
			alert("Please select an Ink.");
			return false;
		}
	}
	if (sizeYes) {
		thisObject = document.optionsform.size;
		if (thisObject.value == "0") {
			alert("Please select a Size.");
			return false;
		}
	}
}


function checkState(stateField, countryField) {
	stateFieldObject = eval("document." + stateField);
	countryFieldObject = eval("document." + countryField);
	if (countryFieldObject.value == "CA" && stateFieldObject.value == "ZZ") {
		alert("Please select a Canadian province from the drop-down list");
	}
	stateFieldObject.focus();
	return false;
}


function checkStyle() {
	thisObject = document.optionsform.style;
	if (thisObject.value == "02") {
		alert("NOTE: Our Ladies t-shirts are cut to be form-fitting and tend to run smaller than normal. For a standard fit, we recommend ordering one size larger than usual. For a looser fit, order two sizes larger, or consider purchasing a regular adult tee.");
	}
	if (thisObject.value == "03") {
		alert("NOTE: Our Spaghetti-strap t-shirts are cut for a tight fit. We recommend you order one or two size(s) larger, depending on how tight a fit you desire.");
	}
}


function echeck(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false;
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false;
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false;
	 }
	 if (str.indexOf(" ")!=-1){
	    return false;
	 }
	 return true;					
}


// initialize color list based on preferred style & color

function setColorList(which, prefColor) {
	if (document.optionsform.color) {
		//alert(which);
		var selectID = 1;
		var listObj = document.optionsform.color;
		var valArrayObj = eval('colors_' + which);
		var idArrayObj = eval('ids_' + which);
		// determine array length
			choice = idArrayObj.length;
		// empty options from list
			listObj.length = 0;
		// create new option object for each entry
		for (var i = 0; i < choice; i++) {
			listObj.options[i] = new Option(valArrayObj[i]);
			listObj.options[i].value = idArrayObj[i];
			if (idArrayObj[i] == prefColor) {
				selectID = i;
			}
		}
		listObj.options[selectID].selected = true;
		if (document.layers) {
			history.go(0);
		} else {
			listObj.disabled = false;
		}
	}
}


// initialize size list based on preferred style

function setSizeList() {
	if (document.optionsform.size) {
		var selectID = 3;
		var listObj = document.optionsform.size;
		var valArrayObj = sizeValues;
		// determine array length
			choice = valArrayObj.length;
		// empty options from list
			listObj.length = 0;
		// create new option object for each entry
		for (var i = 0; i < choice; i++) {
			listObj.options[i] = new Option(valArrayObj[i]);
			listObj.options[i].value = i;
		}
		listObj.options[0].selected = true;
		if (document.layers) {
			history.go(0);
		} else {
			listObj.disabled = false;
		}
	}
}


function swap_shirt(styleID, colorID, inkID, shirtID) {
	//alert (inkID);
	destinationPage = "shirt_preview.php?s=" + styleID + "&b1="+ colorID +"&f1=" + shirtID + "&i=" + inkID;
	window.shirt_previewer.location.href = destinationPage;
}


