function charsOnly(obj)
{
	var regex = /[^a-zA-Z]+/g;
	var itemVal = obj.value;
	itemVal = itemVal.replace(regex, "");
	obj.value = itemVal;
}

function numorcharsOnly(obj,lowerCaseOnly)
{
	var itemVal = obj.value;
	if ( lowerCaseOnly ) {
		var regexLower = /[^a-z0-9]+/g;
		itemVal = itemVal.replace(regexLower, "");
	}
	else {
		var regex = /[^a-zA-Z0-9]+/g;
		itemVal = itemVal.replace(regex, "");
	}
	obj.value = itemVal;
}

function maxTextAreaChars(obj, maxChrs)
{
	var text = "" + obj.value;
	if (text.length > maxChrs) {
		text = text.substring(0,maxChrs);
		obj.value = text;
	}
}

function numbersOnly(obj)
{
	var regex = /[^0-9\.]+/g;// /(\D|\.)+/g;
	var itemVal = obj.value;
	var newVal = itemVal.replace(regex, "");
	if ( newVal != itemVal )
		obj.value = newVal;
}
function intsOnly(obj)
{
	var regex = /[^0-9]+/g;
	var itemVal = obj.value;
	var newVal = itemVal.replace(regex, "");
	if ( newVal != itemVal )
		obj.value = newVal;
}

function intsOnlyWithMinQuantity(obj, prodID, minQuantity)
{
	var regex = /[^0-9]+/g;
	var itemVal = obj.value;
	var newVal = itemVal.replace(regex, "");
	var intQuantity = parseInt(newVal);
	if ( (intQuantity > 0) && (intQuantity < minQuantity) ) {
		alert("aThe minimum order quantity for product " + prodID + " is " + minQuantity);
		//itemVal = minQuantity;
	}
	if ( newVal != itemVal )
		obj.value = newVal;
}
function validateEmail(emailObj)
{
// This will modify the passed in object, stripping away illegal chars
// emailObj should be a js object with a value field such as a text field
	var regex = /[^a-zA-Z0-9@\-_\.]+/g;
	var itemVal = emailObj.value;
	itemVal = itemVal.replace(regex, "");
	emailObj.value = itemVal;
}
function insert(str, intoStr, pos)
{
	var result = intoStr.substring(0,pos);
	result += str;
	result += intoStr.substring(pos,intoStr.length);
	return result;
}

function validatePhone(phoneObj)
{
// This will modify the passed in object, stripping away illegal chars
// emailObj should be a js object with a value field such as a text field
	var regex = /[^0-9]+/g;
	var itemVal = phoneObj.value;
	itemVal = itemVal.replace(regex, "");
	var strLen = itemVal.length;
	if ( strLen > 0 )
		itemVal = insert("(",itemVal,0);
	if ( strLen >= 3 )
		itemVal = insert(") ",itemVal,4);
	if ( strLen >= 6 )
		itemVal = insert("-",itemVal,9);
	if ( strLen > 9 )
		itemVal = insert(" ",itemVal,14);
	if ( strLen > 10 )
		itemVal = insert("ext ",itemVal,15);
	phoneObj.value = itemVal;
}

function isEmailValid(emailText)
{
// this returns a boolean indicating true if email is valid
	var regex = /[^a-zA-Z0-9@\-_\.]+/g;
	var matched =  regex.test(emailText);
	if ( matched )
		return false;
	var indexOfAt = emailText.indexOf("@");
	if ( -1 == indexOfAt )
		return false;
	var lastIndexOfAt = emailText.lastIndexOf("@");
	if ( indexOfAt != lastIndexOfAt )
		return false;
	var lastIndexOfDot = emailText.lastIndexOf(".");
	if ( (-1 == lastIndexOfDot ) || (indexOfAt > lastIndexOfDot ) )
		return false;
	return true;
}
function setSelectionOptionByText(obj, text)
{
	//alert ("got here");
	for (var i=0; i<obj.length; i++ ) {
		if ( obj.options[i].text == text ) {
			obj.options[i].selected = true;
			return;
		}
	}
}

function setSelectionOptionByTextWithDefault(obj, text, defaultIndex)
{
	//alert ("got here");
	for (var i=0; i<obj.length; i++ ) {
		if ( obj.options[i].text.toUpperCase() == text.toUpperCase() ) {
			obj.options[i].selected = true;
			return;
		}
	}
	if ( defaultIndex < obj.length ) {
		obj.options[defaultIndex].selected = true;
	}
}

function setSelectionOptionByValue(obj, val)
{
	//alert ("got here" + val);
	for (var i=0; i<obj.length; i++ ) {
		if ( obj.options[i].value == val ) {
			obj.options[i].selected = true;
			return;
		}
	}
}

function setRadioByValue(obj, val)
{
	var len = obj.length;
	for (var i=0; i<len; i++ ) {
		obj[i].checked = (obj[i].value == val );
	}
}

function setDisabled(theItem, disabled, emptyVal)
{
	theItem.disabled = disabled;
	if ( emptyVal )
		theItem.value = "";
}
function sameAsBillingChanged(control)
{
	var form = control.form;
	setDisabled(form.shipFirstName, control.checked, control.checked);
	setDisabled(form.shipLastName, control.checked, control.checked);
	setDisabled(form.shipCompany, control.checked, control.checked);
	setDisabled(form.shipAddr1, control.checked, control.checked);
	setDisabled(form.shipAddr2, control.checked, control.checked);
	setDisabled(form.shipCity, control.checked, control.checked);
	setDisabled(form.shipState, control.checked, false);
	setDisabled(form.shipZip, control.checked, control.checked);
	setDisabled(form.shipPhone, control.checked, control.checked);
	setDisabled(form.shipCounty, control.checked, false);
	if ( control.checked )
		form.shipState.options[0].selected = true;
	else
		billStateChanged(form.shipState, form.shipCounty);	
	

}

function setCountyOptionsFromCountyArray(pArray, countyItem)
{
	var i = 0;
	 
	countyItem.options.length = 0;
	for ( i = 0; i < pArray.length; i++) {
		var name = pArray[i].name;
		var val = pArray[i].id;
		countyItem.options[i] = new Option(name,val);
	}
	
}
function County(id,name)
{
	this.id = id;
	this.name = name;
	return this;
}

function StateCountyList(sid,pArray)
{
	this.stateID = sid;
	this.pArray = pArray;
	return this;
}

function billStateChanged(statePopup, countyItem)
{
	initGArray();
	var stateID = statePopup.value;	
	for ( var i = 0; i < gArray.length; i++) {

		if ( stateID == gArray[i].stateID ) {
			countyItem.disabled = false;
			setCountyOptionsFromCountyArray(gArray[i].pArray, countyItem);
			return;
		}
	}
	countyItem.options.length = 0;
	countyItem.options[0] = new Option("N/A","");
	countyItem.disabled = true;
}


function validateCheckout(frm)
{
	
	
	if ( frm.billFirstName.value.length == 0 ) {
		frm.billFirstName.focus();
		alert("Please fill out your billing first name");
		return false;
	}
	if ( frm.billLastName.value.length == 0 ) {
		frm.billLastName.focus();
		alert("Please fill out your billing last name");
		return false;
	}
	if ( frm.billCompany.value.length == 0 ) {
		frm.billCompany.focus();
		alert("Please fill out your billing company name");
		return false;
	}
	if (( frm.billAddr1.value.length == 0 ) && ( frm.billAddr2.value.length == 0 ) ) {
		frm.billAddr1.focus();
		alert("Please fill out your billing address.");
		return false;
	}
	if ( frm.billCity.value.length == 0 ) {
		frm.billCity.focus();
		alert("Please fill out your billing address city.");
		return false;
	}
	if ( frm.billState.selectedIndex == 0 ) {
		frm.billState.focus();
		alert("Please select your billing address state.");
		return false;
	}
	if ( frm.billZip.value.length == 0 ) {
		frm.billZip.focus();
		alert("Please fill out your billing address zipcode.");
		return false;
	}
	if ( frm.billCounty.options.length > 1 ) {
		if ( frm.billCounty.selectedIndex == 0 ) {
			alert("Please select your billing county");
			return false;
		}
	}
	if ( frm.billPhone.value.length == 0 ) {
		frm.billPhone.focus();
		alert("Please fill out your billing phone number");
		return false;
	}
	if ( frm.billEmail.value.length == 0 ) {
		frm.billEmail.focus();
		alert("Please fill out your billing email");
		return false;
	}
	if ( isEmailValid(frm.billEmail.value) == false ) {
		frm.billEmail.focus();
		alert("Please correct your billing email");
		return false;
	}	
	
	if ( frm.shipToBillingAddr.checked == false ) {
		
		if ( frm.shipFirstName.value.length == 0 ) {
			frm.shipFirstName.focus();
			alert("Please fill out your shipping first name");
			return false;
		}
		if ( frm.shipLastName.value.length == 0 ) {
			frm.shipLastName.focus();
			alert("Please fill out your shipping last name");
			return false;
		}
		if ( frm.shipCompany.value.length == 0 ) {
			frm.shipCompany.focus();
			alert("Please fill out your shipping company name");
			return false;
		}
		
		
		if (( frm.shipAddr1.value.length == 0 ) && ( frm.shipAddr2.value.length == 0 ) ) {
			frm.shipAddr1.focus();
			alert("Please fill out your shipping address.");
			return false;
		}
		if ( frm.shipCity.value.length == 0 ) {
			frm.shipCity.focus();
			alert("Please fill out your shipping address city.");
			return false;
		}
		if ( frm.shipState.selectedIndex == 0 ) {
			frm.shipState.focus();
			alert("Please select your shipping address state.");
			return false;
		}
		if ( frm.shipZip.value.length == 0 ) {
			frm.shipZip.focus();
			alert("Please fill out your shipping address zipcode.");
			return false;
		}
		if ( frm.shipPhone.value.length == 0 ) {
			frm.shipPhone.focus();
			alert("Please fill out your shipping phone number");
			return false;
		}
	}
	if ( ! frm.agreeWithTerms.checked  ) {
		alert("Please agree to the Servco Terms and Agreement to continue.");
		return false;
	}
	return true;
}

function addProduct(prodPK, quantityText, text1, text2, minQuantity, prodID)
{
	var quantity = parseInt(quantityText.value);
	if ( quantity <= 0 )
		return;
	if ( quantity < minQuantity ) {
		if ( confirm("The minimum order quantity for product " + prodID + " is " + minQuantity + ". Purchase " + minQuantity + "?") )
			quantity = minQuantity;
		else {
			quantityText.focus();
			return;
		}
	}
	//alert("adding product " + prodPK + ", quan: " + quantity);	
	document.prodAddForm.addProdPK.value = prodPK;
	document.prodAddForm.addProdQuantity.value = quantity;
	document.prodAddForm.text1.value = text1;
	document.prodAddForm.text2.value = text2;
	document.prodAddForm.postTime.value = (new Date()).getTime();
	document.prodAddForm.submit();
	
}

function deleteProduct(quantityTextField)
{
	//alert("removing product ");	
	quantityTextField.value = 0;
	quantityTextField.form.submit();
}

function setValForObj(val, obj)
{
	obj.value = val;	
}

function securePage(pg)
{
	if ( (pg == "checkout") || (pg == "shipping") || (pg == "confirm") || (pg == "placeOrder") )
		return true;
	return false;
}
function setSecure(frm)
{
	var frmAction = frm.action;
	var needsSSL = securePage(frm.pg.value);
	var isSSL = frmAction.indexOf("https") == 0;
	
	if ( isSSL && ( ! needsSSL ) ) {
		frmAction = "http" + frmAction.substr(5);
		frm.action = frmAction;
	}
	else if ((! isSSL ) && needsSSL ) {
		frmAction = "https" + frmAction.substr(4);
		frm.action = frmAction;
	}

}
function validateCart(frm,setSec)
{
	if ( setSec )
		setSecure(frm);
	return validateQuantities();
}
function validateShip(frm)
{
	var obj = frm.shipMethod;
	for (var i = 0; i< obj.length; i++) {
    	if (obj[i].checked) {
			if ( obj[i].value == 'shipWillCall' ) {
				if ( frm.willCallLocation.selectedIndex == 0 ) {
					frm.ccNumber.focus();
					alert("Please select your will call location.");
					return false;
				}
			}
			break;
		}
	} 


	if ( frm.paymentMethod.value == 'ccard' ) {
		
		if ( frm.cardType.selectedIndex == 0 ) {
			frm.cardType.focus();
			alert("Please select your credit card type.");
			return false;
		}
		if ( frm.cardHolderName.value.length == 0 ) {
			frm.cardHolderName.focus();
			alert("Please fill out your credit card holder name.");
			return false;
		}
		if ( frm.ccNumber.value.length == 0 ) {
			frm.ccNumber.focus();
			alert("Please fill out your credit card number.");
			return false;
		}
		if ( frm.expMonth.selectedIndex == 0 ) {
			frm.expMonth.focus();
			alert("Please select your credit card expiration month.");
			return false;
		}
		if ( frm.expYear.selectedIndex == 0 ) {
			frm.expYear.focus();
			alert("Please select your credit card expiration year.");
			return false;
		}
		if ( frm.secCode.value.length == 0 ) {
			frm.secCode.focus();
			alert("Please fill out your credit card security number.");
			return false;
		}
		
		var tmpmonth = frm.expMonth.options[frm.expMonth.selectedIndex].value;
		var tmpyear = frm.expYear.options[frm.expYear.selectedIndex].value;
		if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
			alert("This card has already expired.");
			return false;
		}
		
		//if ( frm.origCCNumber.value != frm.ccNumber.value ) {
			card = frm.cardType.options[frm.cardType.selectedIndex].value;
			var retval = eval(card + ".checkCardNumber(\"" + frm.ccNumber.value + "\", " + tmpyear + ", " + tmpmonth + ");");
			cardname = "";
			if (retval) {
				// comment this out if used on an order form
				//alert("This card number appears to be valid.");
			}
			else {
				// The cardnumber has the valid luhn checksum, but we want to know which cardtype it belongs to.
				for (var n = 0; n < Cards.size; n++) {
					if (Cards[n].checkCardNumber(frm.ccNumber.value, tmpyear, tmpmonth)) {
						cardname = Cards[n].getCardType();
						break;
					}
				}
				if (cardname.length > 0) {
					alert("This looks like a " + cardname + " number, not a " + card + " number.");
					return false;
				}
				else {
					alert("This card number is not valid.");
					return false;
				}
			}
		//}
	}
	else {
		if ( frm.servcoAccountPurchaserName.value.length == 0 ) {
			frm.servcoAccountPurchaserName.focus();
			alert("Please fill out your Servco account purchaser name.");
			return false;
		}
		if ( frm.servcoAccountPhone.value.length == 0 ) {
			frm.servcoAccountPhone.focus();
			alert("Please fill out your phone number for confirmation.");
			return false;
		}
		if ( frm.servcoAccountNumber.value.length == 0 ) {
			frm.servcoAccountNumber.focus();
			alert("Please fill out your Servco account number.");
			return false;
		}
		
		
		
	}
	return true;
}

function printPage()
{
	window.print();	
}



function openImgWind(imageW, imageH, url) {
	var topPos = window.screenY + 24;
	var leftPos = window.screenX + 24;
	
	imageH += 40; // for close button
	imageW += 10;
	imgWin = window.open(url, 'imageWindow', 'width=' + imageW + ',height=' + imageH + ',top=' + topPos + ',left=' + leftPos + ',toolbar=no,scrollbars=no,menubar=yes, location=no,directories=no,status=no,resizable=yes');
	if ( imgWin ) // could be null if popup blocker blocked its opening
		imgWin.focus();
	
} 

function openTermsWind() {
	var topPos = window.screenY + 24;
	var leftPos = window.screenX + 24;
	
	var url = "http://www.servco1.com/Site/online_ordering_conditions.html";
	termsWin = window.open(url, 'imageWindow', 'width=960' + ',height=400' + ',top=' + topPos + ',left=' + leftPos + ',toolbar=no,scrollbars=yes,menubar=yes, location=no,directories=no,status=no,resizable=yes');
	if ( termsWin ) // could be null if popup blocker blocked its opening
		termsWin.focus();
	
} 


