function getQueryStringParam(field)
{
	var rtn = '';
	var url = window.location.toString();
	url.match(/\?(.+)$/);
	var params = RegExp.$1;
	params = params.split("&"); 			

	for(var i=0;i<params.length;i++)
	{
		var tmp = params[i].split("=");
		if(tmp[0] == field)
		{
			rtn = unescape(tmp[1]);
			break;
		}
	}			
	return rtn;
}
		
/* 
	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.toGMTString() : "") +
					((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 "";
}



/* 
	set the radio button with the given value as being checked
	do nothing if there are no radio buttons
	if the given value does not exist, all the radio buttons
	are reset to unchecked
*/
function setRadioCheckValue(radioObj, newValue){
	if(!radioObj)
		return;
		
	var radioLength = radioObj.length;
	if(radioLength == undefined){
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	
	for(var i = 0; i < radioLength; i++){
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}



function disableAllRadiosBut(radioObj, value){
	if(!radioObj)
		return;

	var radioLength = radioObj.length;
	if(radioLength == undefined){
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	
	if(value == null){
		for(var i = 0; i < radioLength; i++){
			if(i == 0){
				radioObj[i].checked = true;
			}
			else{
				radioObj[i].checked = false;
			}
		
			radioObj[i].disabled = false;				
		}			
	}
	else{
		for(var i = 0; i < radioLength; i++){
			radioObj[i].checked = false;
			if(radioObj[i].value == value.toString()){
				radioObj[i].disabled = false;
				radioObj[i].checked = true;
			}
			else{
				radioObj[i].disabled = true;
			}
		}
	}		
}



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;
}



function Checkdate(datestr){
	flag = false;

	if (datestr.length == 10){
		firstsl = datestr.indexOf("/");
		secondsl = datestr.indexOf("/", firstsl + 1);
		var dd = (mid(datestr, 1 , firstsl)).valueOf();
		var mm = (mid(datestr, firstsl + 2, secondsl - (firstsl + 1))).valueOf();
		var yy = (mid(datestr, secondsl + 2, datestr.length - secondsl)).valueOf();

		if ((firstsl != -1) && (secondsl != -1)){
			if ((firstsl == 2) || (firstsl == 1)){
				if ((secondsl == 4) || (secondsl == 5) || (secondsl ==3) && ((secondsl - firstsl) > 1)){
					if (((datestr.length - secondsl) == 3) || ((datestr.length - secondsl) == 5)){	
						if (!(isNumeric(mm) && isNumeric(dd) && isNumeric(yy))){
						  return flag;
						}
						else{
							if (parseInt(mm, 10) < 1 || parseInt(mm, 10) > 12){
								mm = Number.NaN;
								return flag;
							}

							if (parseInt(dd, 10) < 1 || (parseInt(dd, 10) > getDays((parseInt(mm, 10) - 1 ),parseInt(yy, 10)))){
								dd = Number.NaN;
								return flag;
							}
							
							flag = true;
							return flag;
						}
					}
				}
			}
		}
	}
	else{
		//alert("Invalid Date.")
	}

	return flag;
}



function Checkdate1(datestr){
	flag = false;

	if ((datestr.length >= 6) &&(datestr.length <= 10)){
		firstsl = datestr.indexOf("/");
		secondsl = datestr.indexOf("/", firstsl + 1);
		var dd = (mid(datestr, 1 , firstsl)).valueOf();
		var mm = (mid(datestr, firstsl + 2, secondsl - (firstsl + 1))).valueOf();
		var yy = (mid(datestr, secondsl + 2, datestr.length - secondsl)).valueOf();

		if ((firstsl != -1) && (secondsl != -1)){
			if ((firstsl == 2) || (firstsl == 1)){
				if ((secondsl == 4) || (secondsl == 5) || (secondsl ==3) && ((secondsl - firstsl) > 1)){
					if (((datestr.length - secondsl) == 3) || ((datestr.length - secondsl) == 5)){
						if (!(isNumeric(mm) && isNumeric(dd) && isNumeric(yy))){
							return flag;
						}
						else{
							if (parseInt(mm, 10) < 1 || parseInt(mm, 10) > 12){
								mm = Number.NaN;
								return flag;
							}

							if (parseInt(dd, 10) < 1 || (parseInt(dd, 10) > getDays((parseInt(mm, 10) - 1 ),parseInt(yy, 10)))){
								dd = Number.NaN;
								return flag;
							}
			
							flag = true;
							return flag;
						}
					}
				}
			}
		}
	}
	else{
		//alert("Invalid Date.")
	}
	
	return flag;
}



//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;
	}	
}



//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;
	}
}



//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 = "/images/cart/processing.gif";
var imgBottomNew = new Image();
imgBottomNew.src = "/images/cart/processing.gif";

function prevent_multiclick(){
	if (multiclick_flag == 0){
		//change button images to indicate processing
		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 deleteHTMLObject(element){
	element.parentNode.removeChild(element);	
}

function center(elm){
	
	if(!elm){
		return;
	}
	
	if(elm.id != 'center-div' && document.getElementById('center-div')){
		document.getElementById('center-div').appendChild(elm);		
	}
	
	var eW = getNodeWidth(elm);
	var eH = getNodeHeight(elm);	

	var sX = getScreenWidth();
	var sY = getScreenHeight();
	
	//calculate positions:
	var xPos = (sX/2) - (eW / 2)
	var yPos = (sY/2) - (eH / 2)

	//move layers to the above positions:
	if(document.getElementById('center-div')){
		document.getElementById('center-div').style.marginLeft = xPos + "px";
		document.getElementById('center-div').style.marginTop = yPos + "px";
	}
	else{
		elm.style.marginLeft = xPos + "px";
		elm.style.marginTop = yPos + "px";
	}
}


function resizeAndCenter(){
	if(document.getElementById('doc_overlay')){
		document.getElementById('doc_overlay').style.width = getScreenWidth() + "px";
		document.getElementById('doc_overlay').style.height = getScreenHeight() + "px";
	}
	
	if(document.getElementById('center-div')){
		center(document.getElementById('center-div'));
	}
}




/************************************************************************
THE FOLLOWING GETS SIZE DIMENSIONS AND RETURNS THE VALUES AS AN INTEGERS
*************************************************************************/
function getScreenHeight(){
	var screenY;
	
	if (typeof window.innerWidth != 'undefined'){
		screenY = window.innerHeight;
	}
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)		
	else if (typeof document.documentElement != 'undefined'
			 && typeof document.documentElement.clientWidth !=
			 'undefined' && document.documentElement.clientWidth != 0){
		screenY = document.documentElement.clientHeight;
	}
	
	// older versions of IE
	else{
		screenY = document.getElementsByTagName('body')[0].clientHeight;
	}		

	if(isNaN(screenY))
		return 0;
	else
		return screenY;
}


function getScreenWidth(){
	var screenX;
	
	if (typeof window.innerWidth != 'undefined'){
		screenX = window.innerWidth;	
	}
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)		
	else if (typeof document.documentElement != 'undefined'
			 && typeof document.documentElement.clientWidth !=
			 'undefined' && document.documentElement.clientWidth != 0){
		screenX = document.documentElement.clientWidth;
	}
	
	// older versions of IE
	else{
		screenX = document.getElementsByTagName('body')[0].clientWidth;
	}		
	
	if(isNaN(screenX))
		return 0;
	else
		return screenX;
}



function getNodeWidth(element){
	var nodeWidth;	
	if(element){
		if (document.layers && element.id) { // Netscape 4
			nodeWidth = parseInt(element.document.width); //Netscape 4
		} 
		else if (document.all || document.getElementById){
			nodeWidth = parseInt(element.offsetWidth); //MSIE + NS6
		}
		
		return nodeWidth;
	}
	else
		return 0;
}


function getNodeHeight(element){
	var nodeHeight;
	
	if(element){
		if (document.layers && element.id) { // Netscape 4
			nodeHeight = parseInt(element.document.height); //Netscape 4
		} 
		else if (document.all || document.getElementById){
			nodeHeight = parseInt(element.offsetHeight); // MSIE + NS6
		}	
		
		return nodeHeight
	}
	else
		return 0;
}

function showPopup(element)
{
	documentOverlay.show();
	element.style.display = "block";
	center(element);
}

function closePopup(element)
{
	element.style.display = 'none';
	documentOverlay.hide();
}

function closeMessage()
{
	setOpacity(document.getElementById('messagePanel'), 100);
	fadeOut('messagePanel', 0);
}

function validateEmail(frmEmail) 
{
	var email_value = StrTrim(frmEmail.value);
	if (validateEmailAddress(frmEmail))
		return true;
	else
		return false;
}

function validateEmailAddress(frmEmail)
{
	var Email_filter=/^([a-zA-Z0-9_\-\.]+)@(([a-zA-Z0-9\-]+\.)+)([a-zA-Z]{2,4})$/;
	
	 if (Email_filter.test(frmEmail.value)) 
		return true;
		
	alert("Please Input a correct email address");
	frmEmail.focus();
	 return false;
}

function setOpacity(obj, opacity){
    opacity = (opacity == 100)?99.999:opacity;
 
    // IE/Win
      obj.style.filter = "alpha(opacity:"+opacity+")";
 
      // Safari<1.2, Konqueror
      obj.style.KHTMLOpacity = opacity/100;
 
     // Older Mozilla and Firefox
      obj.style.MozOpacity = opacity/100;
 
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
}

function fadeIn(objId, opacity) {
    if(document.getElementById){
        if(!document.getElementById(objId))
            return;
       
        obj = document.getElementById(objId);
   
        if (opacity <= 100){
              setOpacity(obj, opacity);
              opacity += 2;
              setTimeout("fadeIn('"+objId+"',"+opacity+")", 25);
        }
    }
}

function fadeOut(objId, opacity) {
    if(document.getElementById){
        if(!document.getElementById(objId))
            return;
           
        obj = document.getElementById(objId);
       
        if (opacity >= 0){
              setOpacity(obj, opacity);
              opacity -= 2;
              setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
        }
	else
		obj.style.display = 'none';
    }
}

function checkUrlValidity(element, include_http){
	element.value = StrTrim(element.value);

	if(!include_http)
		element.value = element.value.replace(/http.*:\/+/i, '');

		
	var url_filter =/([a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)+(\/*[A-Za-z0-9\/\-_&:?\+=\/\/.%]*)*/;

	if (url_filter.test(element.value)) { 
		return true;
 	}

	return false;
}

function numbersonly(e) {
    var unicode=e.charCode? e.charCode : e.keyCode;
    
    //Allow backspace, tab, delete, and arrow keys
    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;
} 

// 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();
            }
        }
    }
}


