﻿//Script for auto tab for phone numbers
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }
  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }
function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function openWindow(langId, imgIdx) {
    myleft=(screen.width)?(screen.width-500)/2:100;
    window.open('recipe.asp?lID='+langId+'&img='+imgIdx, 'recipe', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=520,left ='+myleft+',top = 50');
}
function GetXmlHttpObject(handler) { 
    var objXmlHttp=null;
    if (navigator.userAgent.indexOf("MSIE")>=0) { 
        var strName="Msxml2.XMLHTTP";
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0) {
            strName="Microsoft.XMLHTTP";
        } 
        try { 
            objXmlHttp=new ActiveXObject(strName)
            objXmlHttp.onreadystatechange=handler;
            return objXmlHttp;
        } catch(e) { 
            alert("Error. Scripting for ActiveX might be disabled");
            return;
        } 
    } else {
        objXmlHttp=new XMLHttpRequest();
        objXmlHttp.onreadystatechange=handler;
        objXmlHttp.onerror=handler;
        return objXmlHttp;
    }
} 
//Update the shopping cart
function UpdateCart(ProductID,ProductQty,MinOrderQty,MaxOrderQty,Premium) { 
    document.getElementById('productcontent').innerHTML="Loading...";
    var url="product_updated.asp?ProductID=" + ProductID + "&ProductQty=" + ProductQty + "&MinOrderQty=" + MinOrderQty + "&MaxOrderQty=" + MaxOrderQty + "&Premium=" + Premium;
    xmlHttp=GetXmlHttpObject(cartChanged);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
} 
function cartChanged() { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
	    document.getElementById('productcontent').innerHTML=xmlHttp.responseText;
    }
} 
function ShowContent(id) {
       var e = document.getElementById(id);
          e.style.display = 'block';
    }
function HideContent(id) {
       var e = document.getElementById(id);
          e.style.display = 'none';
    }
function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

function MagHideOthers(){
	HideContent('contests_overlay');
	HideContent('specials_overlay');
	HideContent('resources_overlay');
	HideContent('search_overlay');
}
function ContestsHideOthers(){
	HideContent('magazines_overlay');
	HideContent('specials_overlay');
	HideContent('resources_overlay');
	HideContent('search_overlay');
}
function SpecialsHideOthers(){
	HideContent('magazines_overlay');
	HideContent('contests_overlay');
	HideContent('resources_overlay');
	HideContent('search_overlay');
}
function ResourcesHideOthers(){
	HideContent('magazines_overlay');
	HideContent('contests_overlay');
	HideContent('specials_overlay');
	HideContent('search_overlay');
}
function SearchHideOthers(){
	HideContent('magazines_overlay');
	HideContent('contests_overlay');
	HideContent('specials_overlay');
	HideContent('resources_overlay');
}
     
var rPosNavCab;
function showCabInfo(){
	showPosNavCab(document.getElementById('navCab'));
}
function cabInfoClose(){
	document.getElementById('navCabDrop').style.display='none';
}
function getPosNav(theObj){
  x = y = 0;
  h = theObj.offsetHeight;
  w = theObj.offsetWidth;
  while(theObj){
    x += theObj.offsetLeft;
    y += theObj.offsetTop;
    theObj = theObj.offsetParent;
  }
  return {height:h,width:w,x:x,y:y}
}
function showPosNavCab(){
	rPosNavCab = getPosNav(document.getElementById('popupReferencePoint'));	
	var navCabDrop = document.getElementById('navCabDrop');
	navCabDrop.style.display='block';
	navCabDrop.style.top = rPosNavCab.y-20 + 'px';
	navCabDrop.style.left = rPosNavCab.x-272 + 'px';
}

function getObjByName(objForm,objName)
{
	for (i = 0; i < objForm.length; i++) {
	
		if(objForm.elements[i].name!=null) {  
			if ( objForm.elements[i].name.indexOf(objName)!=-1 && objForm.elements[i].name.indexOf("OldValue")==-1) {
	               return objForm.elements[i];
			}
		}
	}
}

function getCheckedObjByName(objForm,objName)
{
	for (i = 0; i < objForm.length; i++) {

	    if(objForm.elements[i].name!=null) {    
			if ( objForm.elements[i].name.indexOf(objName)!=-1 && objForm.elements[i].name.indexOf("OldValue")==-1) {
  
		        if(objForm.elements[i].checked) {
		              return objForm.elements[i]; 
		        }            
			}
		}	
	}
}

/**
 *	Check whether the inputed mail address is right in format.
 *	value - Should be a string, which contains a mail address.
 */
function mailCheck(value){
   return value == null ? false : value.match(/\b[A-Za-z0-9._%-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,4}\b/);
}

/**
 *	Check whether the inputed postal code is right in format(Match any case M1H 2J2 or without space).
 *	value - Should be a string, which contains a mail address.
 */
function postalCodeCheck(value){
   return value == null ? false : value.match(/\b[A-Za-z][0-9][A-Za-z]\s{0,1}[0-9][A-Za-z][0-9]\b/);
}

/**
 * Trim all the spaces in the string.
 */
function trimSpaces(value){
    x = value.replace(/\s+/g, '');
    return x;
}

/**
 * Trim the spaces at the begin and end in the string.
 */
function trim(value){
    return value == null ? null : value.replace(/^ +/, '').replace(/ +$/, '');
}

/**
 * Trim the left spaces in the string.
 */
function lTrim(value){
    return value == null ? null : value.replace(/^ +/, '');
}

/**
 * Trim the right spaces in the string.
 */
function rTrim(value){
    return value == null ? null : value.replace(/ +$/, '');
}


function postcodevalidate(ctry,zip){
	zip = trimSpaces(zip).toUpperCase(); 
	provinveInitial="ABCEGHJKLMNPRSTVXY";
	if(ctry=="ca"&&provinveInitial.indexOf(zip.substring(0,1))==-1){
		return false;
	}else{
	var tmp ="";
	switch(trim(ctry)){
		case "ca":{
			if(zip.length!=6)
				return false;
			tmp= new RegExp("^[a-zA-Z][0-9][a-zA-Z][\\s]{0,1}[0-9][a-zA-Z][0-9]");
			break;
		}
		case "us":{
			tmp = new RegExp("^[0-9]{5}");
			break;
		}
		default:
			tmp= new RegExp("^[a-zA-Z][0-9][a-zA-Z][[:space:]][0-9][a-zA-Z][0-9]");
	}
	return tmp.test(zip);
	}
}
 



/*function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}*/

function getCompareString(string){
		
		string=string.toUpperCase(); 
		string=trimSpaces(string);
		return string;
	
	}	

function getProvinceAbbre(pc){
	province="";
	pc=getCompareString(pc);
	initial=pc.substring(0,1)
	
	switch (initial){
			case 'A':
				province = "NL";
				break;
			case 'B':
				province = "NS";
				break;
			case 'C':
				province = "PE";
				break;
			case 'E':
				province = "NB";
				break;
			case 'G':
				province = "QC";
				break;
			case 'H':
				province = "QC";
				break;
			case 'J':
				province = "QC";
				break;
			case 'K':
				province = "ON";
				break;
			case 'L':
				province = "ON";
				break;
			case 'M':
				province = "ON";
				break;
			case 'N':
				province = "ON";
				break;
			case 'P':
				province = "ON";
				break;
			case 'R':
				province = "MB";
				break;
			case 'S':
				province = "SK";
				break;
			case 'T':
				province = "AB";
				break;
			case 'V':
				province = "BC";
				break;
			case 'X':
				initial3=pc.substring(0,3)
			
				if (initial3=="X0A"||initial3== "X0B"||initial3=="X0C"){ 
					province = "NU";
				}else{
					province = "NT";
				}
				break;
			case 'Y':
				province = "YT";
				break;
	
	}
	return province;

}

function provinceEqual(sessionProvince,pc){
		
	inputProvince=getProvinceAbbre(pc)
	if(sessionProvince == "PQ")
	{
	sessionProvince="QC"
	}
	if(sessionProvince==inputProvince){
	    return true;
	}else{
		return false;
	}

}

	/**
	* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
	* IE has a problem with wanted select form tags to always be the topmost z-index or layer
	*/
	function hideSelectBoxes() {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT") {
					 document.forms[i].elements[e].style.visibility="hidden";
				}
			}
		}
	}
	
	/**
	* Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
	* IE has a problem with wanted select form tags to always be the topmost z-index or layer
	*/
	function displaySelectBoxes() {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT") {
					 document.forms[i].elements[e].style.visibility="visible";
				}
			}
		}
	}

	function textCounter(obj, maxlimit) {
	    var spaceLength = 0 ;
		/**
		for (var i = 0; i < obj.value.length ; i++) {
		    var filterChar = obj.value.substring(i, i + 1) ;
			if (filterChar==" " || filterChar=="\n" || filterChar=="\r") {
				spaceLength ++ ;	
			}					
		}
	
	    var trimValue =  obj.value.replace(/ /g, "");   //trim space
	    */ 
		if (obj.value.length > maxlimit) {
			obj.value = obj.value.substring(0, maxlimit + spaceLength);
		}
	}

	function showBlackOverlay(){

		var browser=navigator.appName
		var maxheight=document.documentElement.clientHeight;
		
		var maxwidth = document.documentElement.clientWidth;
        var offsetWidth; 
        var offsetTop;
		if (browser=='Netscape'){
			offsetTop = document.body.scrollTop;
		}else{
			offsetTop = document.documentElement.scrollTop;
		}
		offsetWidth = document.body.scrollLeft;
        if(document.documentElement.clientHeight > document.body.scrollHeight){
            maxheight = document.documentElement.clientHeight;       
        }else{
            maxheight = document.documentElement.scrollHeight;       
		}
		var footer = document.getElementById('footer');
		
		if(footer=="undefined" || footer==null){
			maxheight = maxheight + 650;
		}else{
		    maxheight = maxheight + 50;
		}
		if(document.getElementById('blackOverlayID')!= null)
		{
	    	document.getElementById('blackOverlayID').style.height=maxheight;
			document.getElementById('blackOverlayID').style.width=maxwidth;
	    	document.getElementById('blackOverlayID').style.display='block' ;
	    }
	}
