// JavaScript Document
//useful javascript functions
function checkfieldInteger(current_form) {
var IsValid
IsValid = true
//test to see if textfield contains numerical data
	for (counter = 0; counter < current_form.length; counter++) {
		if (current_form[counter].type == "text") {		
			//check if the text field is empty, if it is lets set the IsValid to false
			if (current_form[counter].value == "" || current_form[counter].value == null) {
				IsValid = false
			}
				//check if the text field contains numerical data
			var digit_characters = "0123456789"
			if (isNaN(current_form[counter].value)) {
				IsValid = false
			}
		}
	}
	//check if the field is InValid, if it is display an alert and cancel form submission
	if (IsValid == false) {
		alert("Please enter the quantity as a number only")
		current_form.reset()
	}
}

function get_cookie(name_to_get) {
	var cookie_array = document.cookie.split("; ")
	for (counter=0;counter<cookie_array.length; counter++) {
		cookie_pair = cookie_array[counter].split("=")
		cookie_name = cookie_pair[0]
		cookie_value = cookie_pair[1]
		
		if (cookie_name == name_to_get) {
			return unescape(cookie_value)
		}
	}
};

function backtoItem() {
//this function sends the user back to the item page 
//ensuring it doesnt keep them on the same page
//get the item page from the cookie
var referrerURL = get_cookie("referrerURL")
document.location = referrerURL
};

function SetReferrer() {
var theReferrerURL, thisURL
theReferrerURL = document.referrer
theReferrerURL = theReferrerURL.split("?")
thisURL = location.href
thisURL = thisURL.split("?")
if (theReferrerURL[0] != thisURL[0] && theReferrerURL[0] != '' ) { 
	//set temp cookie with referrer URL
	document.cookie = "referrerURL=" + theReferrerURL[0]
	}
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function TestCookie(){
testValue=Math.floor(1000*Math.random());
SetCookie('AreCookiesEnabled',testValue);
if (testValue!=ReadCookie('AreCookiesEnabled')) {
document.location = "cart.asp?checkcookies=0"
}
};

