// this method will return the URL parameter value given a parameter name
function getParamValue(paramName){
  //alert ("paramName="+paramName);
  var paramValue = "";
  var strURL = window.location.href;
  //alert ("strURL="+strURL);
  if ( strURL.indexOf("&") > -1 ){
    var initialQueryString = strURL.substr(strURL.indexOf("&"));
    var queryString = initialQueryString.split("&");
    for (var i=0; i<queryString.length; i++) {
      if (queryString[i].indexOf(paramName + "=") > -1) {
        var paramArray = queryString[i].split("=");
        paramValue = paramArray[1];
        break;
      }
    }
  }
  //alert ("paramValue="+paramValue);
  return paramValue;
}

// the following two methods are used to hide the mini-cart from view upon user actions
var doHideMiniCart = true;
function hideMiniCart() {
    	if (doHideMiniCart) {
		if (eval(document.getElementById("cartBox"))) {
			document.getElementById("cartBox").style.display="none";
		}
	}
	doHideMiniCart = true;
}
function setHideMiniCart(hideMiniCartBoolean) {
	doHideMiniCart = hideMiniCartBoolean;
}

// the following two methods are used to hide the store finder view upon user actions
var doHideStoreFinder = true;
function hideStoreFinder() {
    	if (doHideStoreFinder) {
		if (eval(document.getElementById("storeFinderBox"))) {
			document.getElementById("storeFinderBox").style.display="none";
		}
	}
	doHideStoreFinder = true;
}
function setHideStoreFinder(hideStoreFinderBoolean) {
	doHideStoreFinder = hideStoreFinderBoolean;
}


function isMonth(month, months){
	for (i = 0; i < months.length; i ++){
		//alert(month.toUpperCase()+" "+months[i].toUpperCase());
		if (month.toUpperCase().indexOf(months[i].toUpperCase()) > -1) return true;
	}
}

function isValidDate(value){
	isValidDateString = true;
	has30Days = ["Apr","Jun","Sep","Nov"];
	has29Days = ["Feb"];
	month = value[0];
	day = value[1];
	year = value[2];
	if (year.length < 4) isValidDateString=false;
	isLeapYear = parseInt(year)%4==0;
	if (isMonth(month, has30Days) && day > 30) isValidDateString=false;
	if (isMonth(month, has29Days) && day > 29) isValidDateString=false;
	if (isMonth(month, has29Days) && !isLeapYear && day > 28) isValidDateString=false;
	return isValidDateString;
}

  
  
  
// validation start...
var vMsg="";
var vFormattedMsg="";
var VT_IS_NOT_NULL=1;
var VT_IS_NUMERIC=2;
var VT_IS_EMAIL=3;
var VT_IS_PHONE_NUMBER=4;
var VT_IS_US_POSTALCODE=5;
var VT_IS_CANADIAN_POSTCODE=6;
var VT_IS_EQUAL=7;
var VT_IS_DIFFERENT=8;
var VT_IS_TRUE=9;
var VT_IS_FALSE=10;
var VT_IS_CREDITCARDNUMBER=11;
var VT_IS_OVER_18=12;
var VT_IS_BAD_WORD=13;
var VT_IS_NOT_EMAIL=14;
var VT_IS_GR_PASSWORD=15;
var VT_IS_PAST_DATE=16;
var VT_IS_GR_BIRTHDATE=17;

var invalidFieldCount=0;
var invalidFields=new Array();

function initValidation(){
	vMessages = new Array();
	vMessageCount = 0;
	vMsg="";
	vFormattedMsg="";
	invalidFieldCount=0;
	invalidFields=new Array();
}

function isInvalidField(captionId){
	for(i=0; i<=invalidFields.length; i++){
		if (invalidFields[i]==captionId){
			return true;
		}
	}
	return false;
}

function assertFormfield(vType, errMsg, formfield, label) {
	//alert("label: " + label.id);
	if (formfield == null) {
		// generic error
		alert("Generic error");
		return false;
	} else {
		var assertion=assertCondition(vType, formfield.value, errMsg);
		if(label!=null) {
			if (!assertion) {
				showErrMsgInLabel(label, errMsg);
			} else {
				clearErrMsgInLabel(label);
			}
		}
	}
	return assertion;
}

function assertArray(vType, errMsg, anArray, label, errElement) {
	//alert("label: " + label.id);
	if (anArray == null) {
		// generic error
		alert("Generic error");
		return false;
	} else {
		var assertion=assertCondition(vType, anArray, errMsg);
		if(label!=null) {
			if (!assertion) {
				showErrMsgInLabel(label, errMsg);
			} else {
				clearErrMsgInLabel(label);
			}
		}
	}
	return assertion;
}

function assert(vType, value, errMsg, captionId){
	//alert(captionId);
	captionObj=document.getElementById(captionId);
	assertion=assertCondition(vType, value, errMsg);
	if(captionObj!=null){
		if (!assertion || isInvalidField(captionId)){
			captionObj.style.color='red';
			invalidFieldCount++;
			invalidFields[invalidFieldCount]=captionId;
			
		}
		else{
			captionObj.style.color="";
		}
	}
	return assertion;
}

function assertCondition(vType, value, errMsg){
  	if (vType==VT_IS_NOT_NULL){
  		return appMsgIfError(!isEmptyString(value), errMsg);
  	}
  	if (vType==VT_IS_NUMERIC){
  		return appMsgIfError((value!=null && /^[0-9]+$/.test(value)), errMsg);
  	}
  	if (vType==VT_IS_EMAIL){
  		return appMsgIfError((value!=null && /^[a-zA-Z0-9]+([\.\-_]*[a-zA-Z0-9]+)*[\.\-_]*@[a-zA-Z0-9]+([\.\-_]*[a-zA-Z0-9]+)*(\.[a-zA-Z0-9]{2,3})+$/.test(value)), errMsg);
  	}
  	if (vType==VT_IS_PHONE_NUMBER){
  		return appMsgIfError(!isEmptyString(value), errMsg);
  	}
  	if (vType==VT_IS_US_POSTALCODE){
  		//appMsgIfError((value==null||!/^[0-9]{10}$/.test(value)), errMsg);
  		return appMsgIfError(!isEmptyString(value), errMsg);
  	}
  	if (vType==VT_IS_CANADIAN_POSTCODE){
  		return appMsgIfError(!isEmptyString(value), errMsg);
  	}
  	if (vType==VT_IS_EQUAL){
  		val1=value[0];
  		val2=value[1];
  		return appMsgIfError( !isEmptyString(val1)&&!isEmptyString(val2) && val1==val2, errMsg);
  	}
  	if (vType==VT_IS_DIFFERENT){
  		val1=value[0];
  		val2=value[1];
  		return appMsgIfError( !((isEmptyString(val1)&&emptyString(val2))||val1==val2), errMsg);
  	}

  	if (vType==VT_IS_TRUE){
  		return appMsgIfError( value!=null&&value, errMsg);
  	}

  	if (vType==VT_IS_FALSE){
  		return appMsgIfError( value!=null&&!value, errMsg);
  	}
  	if (vType==VT_IS_CREDITCARDNUMBER){
  		return appMsgIfError((value!=null&&/^[0-9\*]+$/.test(value)), errMsg);
  	}
  	if (vType==VT_IS_OVER_18){
  		return appMsgIfError(isOver18(value), errMsg);
  	}
  	if (vType==VT_IS_BAD_WORD) {
  		return appMsgIfError( value!=null && value != "****", errMsg);
  	}
  	if (vType==VT_IS_NOT_EMAIL){
  		return appMsgIfError((value!=null && value.indexOf("@")==-1), errMsg);
  	}

  	if (vType==VT_IS_GR_PASSWORD){
  		return appMsgIfError((value!=null && value.length>3 && value.length<26 && value.indexOf(" ")==-1 && isGoodChars(value)), errMsg);
  	}
  	if (vType==VT_IS_PAST_DATE){
  		return appMsgIfError(isPastDate(value), errMsg);
  	}
  	if (vType==VT_IS_GR_BIRTHDATE){
  		return appMsgIfError(isPastDate(value) && isLater(value, ['January',1,1850]), errMsg);
  	}

}

function isGoodChars(val){
	if (val!=null){
		var badChars="@";
		for(i=0;i<badChars.length;i++){
			if (val.indexOf(badChars.charAt(i))>-1) 
				return false;
		}		
	}
	return true;
}
function isEmptyString(value){
	return value==null||value.replace(/ /g, "")=="";
}

function isOver18(value){
	ofAge = false;
	month = value[0];
	day = value[1];
	year = value[2];
	try{
		if (isValidDate(value)){
			dateString = day + " " + month + " " + year;
			var currDate = new Date();
			var birthDate = Date.parse(dateString);
			years = (currDate.getTime() - birthDate)/31556926000;
		
			if (years > regMinimumAge){
			ofAge = true;
			} else {
				setCookie("u18", "1", false, '/', false, false);
				ofAge = fale;
			}
		}
	} catch (error) { 
		//alert("caught exception:"+error);
		ofAge = false 
	}
	
	return ofAge;
		
}

function isPastDate(value){
	ofAge = false;
	month = value[0];
	day = value[1];
	year = value[2];
	try{
		if (isValidDate(value)){
			dateString = day + " " + month + " " + year;
			var currDate = new Date();
			var birthDate = Date.parse(dateString);
			return currDate.getTime() > birthDate;
		}
	} catch (error) { 
		alert("caught exception:"+error);
		ofAge = false 
	}
	
	return ofAge;
		
}

function isLater(testDate, refDate){
	ofAge = false;
	try{
		if (isValidDate(testDate) && isValidDate(refDate)){
			date1=Date.parse(testDate[0]+" "+testDate[1]+" "+testDate[2]);
			date2=Date.parse(refDate[0]+" "+refDate[1]+" "+refDate[2]);
			return date1>date2;
		}
		else{
			alert("Date formating error!");
		}
	} catch (error) { 
		alert("caught exception:"+error);
		ofAge = false 
	}
	
	return ofAge;
		
}

function duplicateMessage(errMsg){
	for (i = 0; i < vMessages.length; i++){
		if (vMessages[i] == errMsg) return true;
	}
	return false;
}

function appMsgIfError(condition, errMsg){
	if (!condition){
		if (!duplicateMessage(errMsg)){
			vMessages[vMessageCount] = errMsg;
			vMessageCount++;
		}
		return false;
	}
	else{
		return true;
	}
}

function isvalid(){
	return vMessages.length == 0;
}

function getErrorMessage(){
	return vMsg;
}
function getFormattedErrorMessage(){
	for (i = 0; i < vMessages.length; i++){
	 vFormattedMsg += vMessages[i] + "<br />\n";
	}
	return vFormattedMsg;
}

function displayOptionsErrorMessage(divName){
	displayOptionsErrorMessageDetail(divName,getFormattedErrorMessage());
}

function displayOptionsErrorMessageDetail(divName, errorMsg){
	msgArea = document.getElementById(divName);
	msgArea.innerHTML="<p class=\"error\">"+errorMsg+"</div>";
	msgArea.visibility="visible";
}

function displayErrorMessage(divName){
	displayErrorMessageDetail(divName, getFormattedErrorMessage());
}

function displayErrorMessageDetail(divName, errorMsg){
	msgArea = document.getElementById(divName);
	msgArea.innerHTML="<div id=\"errorMsgBox\">"+errorMsg+"</div>";
	msgArea.visibility="visible";
}
/*
function displayErrorMessage(divName){
	msgArea = document.getElementById(divName);
	msgArea.innerHTML="<div id=\"errorMsgBox\">"+getFormattedErrorMessage()+"</div>";
	msgArea.visibility="visible";
}
*/
function removeErrorMessage(divName) {
//	alert("removing error msg for: "+divName);
	msgArea = document.getElementById(divName);
	if (msgArea != null) {
		msgArea.style.display = "none";
	}	
}

function clearErrorMessage(divName){
	msgArea = document.getElementById(divName);
	if (msgArea != null) {
		msgArea.innerHTML = "";
		msgArea.visibility="hidden";
	}	
}

function emptyString(stringVar){
	if (stringVar==null || stringVar=='') return true;
	for (var index=0; index < stringVar.length; index++){
        	if (stringVar.charAt(index) != ' ') {return false;}
     	}
     	return false;
}

// changes input field to strip out all characters other than ones in filteredValues
function onlyNumeric (input, filteredValues) {
	s = input.value;
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) != -1) returnString += c;
	}	
	input.value = returnString;
}

// validation end

// display errors that are a result of an Ajax action
function showErrorMessage(errMsg, divName){
	if (errMsg!=null && errMsg!=""){
		initValidation();
		appMsgIfError(false, errMsg);
	   	if(!isvalid()){
	   		if (document.getElementById(divName)!=null){
				displayErrorMessage(divName);
	   		}
	   		else{
	   			alert(errMsg);
	   		}		
	   	}
	}
}

// Appends 'error' css classname to the element.
// This ensures no pending spaces at the end of the clasName value
function addErrorStyleToElement(elementName) {
	if (elementName != null) {
		var newClassName = stripErrorClass(elementName.className);
		if (newClassName.length > 0) {
			newClassName = newClassName + " ";
		}
		newClassName = newClassName + "error";
		elementName.className = newClassName;
	}
}

function removeErrorStyleFromElement(elementName) {
	if (elementName != null) {
		elementName.className = stripErrorClass(elementName.className);
	}
}

function showErrMsgInLabel(label, errMsg) {
	if (label != null) {
		label.innerHTML=errMsg;
		label.style.display='block';
	}
}

function clearErrMsgInLabel(label) {
	if (label != null) {
		label.innerHTML = "";
		label.style.display = 'none';
	}
}

function stripErrorClass(strClassName) {
	var nClassName = "";
	var classNames = strClassName.split(" ");
	for (i = 0; i < classNames.length; i++) {
		if (classNames[i] != '') {
			if (classNames[i].match(/^error$/) == null) {
				if (nClassName.length > 0) {
					nClassName = nClassName + " ";
				}
				nClassName = nClassName + classNames[i];
			}
		}
	}
	return nClassName;
}
