// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, path, expires, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toUTCString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	
	document.cookie = curCookie;
}
	

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name){
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
		begin += 2;
	
	var end = document.cookie.indexOf(";", begin);
	
	if (end == -1)
		end = dc.length;
	
	return unescape(dc.substring(begin + prefix.length, end));
}

function GetCookieNonNull(cookieName){
	var cookieval = getCookie(cookieName);

	if (("" + cookieval) == null || cookieval == null)
		cookieval="";

	return cookieval;
}
	
function GetExpireDate(daystoexpire){
	var date = new Date();
	date.setTime(date.getTime() + (daystoexpire*24*60*60*1000));
	return date;
}
	
function openNewWindow(win){
	openNewWindowSized(win, '375', '500');
}
	
	
function openNewWindowSized(win, hgt, wdt){
	if (hgt=='')
		hgt = '375';
	
	if (wdt=='')
		wdt = '500';
	
	hgt = (parseInt(hgt) + 20) + '';
	wdt = (parseInt(wdt) + 10) + '';
	
	newWindow = window.open("", win,"width=" + wdt + ",height=" + hgt + ",menubar=no,toolbar=no,scrollbars=yes,directories=no,location=no,resizable=yes");
}

function openPrintableWindow(win){
	newWindow = window.open("", win,"width=500,height=375,menubar=yes,toolbar=no,scrollbars=yes,directories=no,location=no,resizable=yes");
}

function openBigWindow(win){
	newWindow = window.open("", win,"width=500,height=525,menubar=no,toolbar=no,scrollbars=yes,directories=no,location=no,resizable=yes");
}
	
function getRadioValue(radio){
	for (var i=0; i<radio.length; i++){
		if (radio[i].checked) 
			return radio[i].value;
	} 
	return "";
}

	
function IsNumberType(strtocheck, numtype){
	var rtn = false;
		
	if (numtype == "INT")
		s = parseInt(strtocheck,10);
	else
		s = parseFloat(strtocheck,10);
	
	if (s == strtocheck)
		rtn = true;

	return rtn;
}


function CheckNumLength(nmbr,lngth){
	var i = Number(nmbr);
	var x = nmbr.length;
	
	if ( x == lngth){
		if (isNaN(i) == true) 
		{
			return false;
		}
		else 
		{
			return true;
		}
	}			
	else{
		return false;				
	}
}



function isNumeric(StrChkVal){
	var RetVal = true;
	var NumberOfPoints = 0;
	for (i = 0 ; i < StrChkVal.length ; i++){
		if (mid(StrChkVal, i + 1, 1) == "."){
			if (NumberOfPoints == 0){
				NumberOfPoints = 1;
			}
			else{
				RetVal = false
			}
		}
		else{
			if (isNaN(parseInt(mid(StrChkVal, i + 1, 1), 10))){
				RetVal = false;
			}
		}
	}

	return RetVal;
}



function StrTrim(StrVar){
	for (I = 0 ; I < StrVar.length ; I++){
		if ((StrVar.substring(0,1)) == " "){
			StrVar = StrVar.substring(1,StrVar.length);
			I = I - 1;
		}
	}
	
	for (I = 0 ; I < StrVar.length ; I++){
		if ((StrVar.substring(StrVar.length - 1,StrVar.length - 0)) == " "){
			StrVar = StrVar.substring(0,StrVar.length - 1);
			I = I - 1;
		}
	}

	return StrVar;
}



function mid(str,start,n){
	var strlen = str.length;
	var jj = str.substring(start-1,strlen);
	jj = jj.substring(0,n);
	return jj;
}


// Checks a string to see if it in a valid date format
// of (D)D/(M)M/(YY)YY and returns true/false
function isValidDate(s) {
    // format D(D)/M(M)/(YY)YY
    var dateFormat = /^\d{1,4}[\.|\/|-]\d{1,2}[\.|\/|-]\d{1,4}$/;

    if (dateFormat.test(s)) {
        // remove any leading zeros from date values
        s = s.replace(/0*(\d*)/gi,"$1");
        var dateArray = s.split(/[\.|\/|-]/);
		var day = dateArray[0];
		var mnth = dateArray[1];
		var yr = dateArray[2];

		//1st check for leap year dates
		if (yr%4 == 0 && mnth == 2){
			if (((yr%100 == 0 && yr%400 == 0) && day > 29) || 
				((yr%100 == 0 && yr%400 != 0) && day > 28)){		
				return false;
			}
		}
		
		//2nd check for leap year dates
		if (yr%4 != 0 && mnth == 2 && day > 28){
			return false;
		}
		
		//Check if months that have 30 days have 31 selected
		if ((mnth == 4 || mnth == 6 || mnth == 9 || mnth == 11) && day > 30){
			return false;
		}
			
		return true;

    } else {
        return false;
    }
}
	


// 4/12/2004 Const added these two functions to validate zip/postal codes in shippingaddress.asp and to distinguish between the two
//Canadian Postal Code Validation
function validate_Canadian_Postal_code(post_val){
	post_val = StrTrim(post_val);
		
	while (post_val.indexOf(" ") > -1)
		post_val = post_val.replace(" ","");
			
	if (post_val == ""){
		alert("Please enter your Postal Code!");
		return false;
	}        
		
	if (post_val.search(/^([a-zA-Z]{1}\d{1}){3}$/) == -1){
		alert("You have entered an invalid postal code! Please verify and correct your postal code.");
		return false;
	}	
	
	return true;
}
	

//US Zip Code Validation
function validate_US_Zip_code(zip_val){
	zip_val = StrTrim(zip_val);
	if (zip_val == ""){
		alert("Please enter your Zip Code!");
		return false;
	}        
		
	if (zip_val.search(/^\d{5}(-\d{4})?$/) == -1){
		alert("You have entered an invalid zip code! Please verify and correct your zip code. Examples: 11788 or 11788-2624");
		return false;
	}
	
	return true;
}
	

//this is used in the cart to make sure people don't press the button more than once
var multiclick_flag = 0;
/*var imgTopNew = new Image();
imgTopNew.src = "<%=imgPath%>/cart/small-processing.gif";
var imgBottomNew = new Image();
imgBottomNew.src = "<%=imgPath%>/cart/processing.gif";
*/

function prevent_multiclick(){
	if (multiclick_flag == 0){
			//change button images to indicate processing
			/*if (document.images.imgTop)
				document.imgTop.src = imgTopNew.src;

			document.imgBottom.src = imgBottomNew.src;
			 */
			// allow the first submit only
			multiclick_flag++ ;
			return true ;
	} 
	else 
		return false ;	// kill the subsequent submits
}



function replacemultiple(str, find, repl){
	while (str.indexOf(find) > -1)
		str = str.replace(find, repl);
		
	return str;
}


function ReverseContentDisplay(d) {
    if (document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
    else { document.getElementById(d).style.display = "none"; }
}

function getParameterByName(name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}



// TabNext()
// Function to auto-tab phone field
// Arguments:
//   obj :  The input object (this)
//   event: Either 'up' or 'down' depending on the keypress event
//   len  : Max length of field - tab when input reaches this length
//   next_field: input object to get focus after this one
// -------------------------------------------------------------------
var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
	}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
			}
		}
	}
}



function numbersonly(e) {
	var unicode=e.charCode? e.charCode : e.keyCode;
	
	if (unicode!= 8 && unicode!= 9 && unicode != 46 && unicode != 144 && (unicode < 37 || unicode > 40)){ 
	  if( !e.shiftKey  &&
		  ((unicode == 189 || unicode == 109) ||
		  (unicode >= 48 && unicode <= 57) || 
		  (unicode >= 96 && unicode <= 105))
		  )
			return true;
		else
			return false;
	}
	else
		return true;
} 
