/*
* File: functions.js
* Purpose: JavaScript Functions
* Site: Click2Translate.com
* Created: Tony Ruscoe (10 April 2002)
*/

/*
* Function: Generic Browser Sniff
* Last Modified: Tony Ruscoe (10 April 2002)
* Returns: true if browser variable is true; false otherwise
*/
      isMac=(navigator.appVersion.indexOf("Mac")!=-1);
       isNS=(navigator.appName.indexOf("Netscape")!=-1);
    isOpera=(navigator.userAgent.indexOf("Opera")!=-1);
isKonqueror=(navigator.userAgent.indexOf("Konqueror")!=-1);
    isXPSP2=(navigator.userAgent.indexOf("SV1")!=-1);
      isDOM=(document.getElementById)?true:false;
       isIE=(document.all)?true:false;
      isIE4=isIE&&!isDOM;
     isIE4M=isIE4&&isMac;
   isIE5or6=isIE&&isDOM;
      isNS4=(document.layers)?true:false;
   isNS4old=(isNS4&&(parseFloat(navigator.appVersion)<4.02));
      isNS6=isNS&&isDOM;

function focusField(field){if(field.value == field.defaultValue)field.value="";return true;}
function blurField(field){if(field.value == "")field.value=field.defaultValue;return true;}

/*
* Function: gSafeOnLoad, SafeAddOnLoad
* Modified from: http://javascript.about.com/library/scripts/blsafeonload.htm
* Last Modified: Tony Ruscoe (10 April 2002)
* Syntax: SafeAddOnload([String]);
* Returns: Nothing
*/
var gSafeOnload=new Array();
function SafeAddOnload(f){
	if (isIE4M){ // IE 4.5 blows out on testing window.onload
		window.onload=SafeOnload;
		gSafeOnload[gSafeOnload.length]=f;
	}else if (window.onload){
		if (window.onload != SafeOnload){
			gSafeOnload[0]=window.onload;
			window.onload=SafeOnload;
		}
		gSafeOnload[gSafeOnload.length]=f;
	} else window.onload=f;
}
function SafeOnload(){
	for (var i=0;i<gSafeOnload.length;i++) gSafeOnload[i]();
}

/*
* Function: switchLanguage
* Last Modified: Tony Ruscoe (8 March 2004)
* Syntax: switchLanguage([Integer]);
* Returns: changes the current session language and locale
*/
function switchLanguage(intLocalLangID){
	document.frmLocale.localLangID.value = intLocalLangID;
	document.frmLocale.submit();
}

/*
* Function: isEmpty
* Last Modified: Tony Ruscoe (10 April 2002)
* Syntax: isEmpty([String]);
* Returns: true if string is does not exist, contains only whitespace or is zero length; false otherwise
*/
function isEmpty(str){
	if (!str) return true;
	if (str.length==0) return true;
	if (str.search(/\S/)==-1) return true;
	return false;
}

/*
* Function: isEmail
* Last Modified: Tony Ruscoe (30 January 2004)
* Syntax: isEmail([String]);
* Returns: true if string is does not exist, contains only whitespace or is zero length; false otherwise
*/
function isEmail(str) {
	var oRegEx = /^[\w-\.]+\@([\da-z-]+\.)+[a-z]{2,4}$/gi
	return oRegEx.test(str);
}

/*
* Function: isLengthOK
* Last Modified: Tony Ruscoe (24 April 2002)
* Syntax: isLengthOK([String],[Integer],[Integer]);
* Returns: true if the length of the string is within the supplied limits; false otherwise
*/
function isLengthOK(str,min,max) {
	if(str.length < min || str.length > max) return false;
	else return true;
}

/*
* Function: isPassword
* Last Modified: Tony Ruscoe (24 April 2002)
* Syntax: isPassword([String]);
* Returns: true if password contains only alpha numeric characters and is > 6 and <= 15 characters long; false otherwise
*/
function isPassword(str){
	if (isEmpty(str)) return false;
	else if (!isLengthOK(str, 6, 15)) return false;
	else if (str.search(/[\W_]+/) > -1) return false;
	return true;
}

/*
* Function: isPromoCode
* Last Modified: Tony Ruscoe (22 August 2002)
* Syntax: isPromoCode([String]);
* Returns: true if the promo code is empty or contains only alpha numeric characters; false otherwise
*/
function isPromoCode(str){
	if (str.search(/[\W_]+/) > -1) return false;
	return true;
}

/*
* Function: isCreditCard
* Modified from: http://www.sislands.com/jscript/week6/ccvalidation.htm
* Credit card format data from: http://en.wikipedia.org/wiki/Credit_card_numbers
* Last Modified: James Law (06 March 2007)
* Syntax: isCreditCard([String]);
* Returns: true if card number is valid syntax; false otherwise
*/
function isCreditCard(strNumber){
	// REMOVE ANY NON-NUMERIC CHARACTERS
	strNumber=strNumber.replace(/[^0-9]/gi, "");
	// REJECT CC NUMBER IF EMPTY, SHORTER THAN 13 OR LONGER THAN 19
	if(isEmpty(strNumber)||strNumber.length<13||strNumber.length>19) return false;
	// THE LUHN FORUMLA / MODULUS 10 CHECK
	var sum=0;
	var mul=1;
	var lenNumber=strNumber.length;
	for(var i=0;i<lenNumber;i++){
		var digit=strNumber.substring(lenNumber-i-1, lenNumber-i);
		var tproduct=parseInt(digit, 10)*mul;
		if (tproduct>=10) sum+=(tproduct%10)+1
		else sum+=tproduct;
		if (mul==1) mul++;
		else mul--;
	}
	if((sum%10)==0){
		var oRegEx = /^(((37|34)\d{13})|((5020|5038|4)\d{12})|((51|52|53|54|55)\d{14})|(4\d{15})|((4903|4905|4911|4936|6333|6334|6759|6767|564182|63110)\d{10,15}))$/i;
		return oRegEx.test(strNumber);
	}else{
		return false;
	}
}

/*
* Function: isExpiryDate
* Last Modified: Tony Ruscoe (28 January 2003)
* Syntax: isExpiryDate([String],[String]);
* Returns: true if the expiry date is valid; false otherwise
*/
function isExpiryDate(strMonth, strYear){
	var enteredMonth=parseInt(strMonth,10);
	var enteredYear=parseInt(strYear,10);
	if (enteredYear<100){ // IF A 2 DIGIT YEAR IS ENTERED
		if (enteredYear<70) enteredYear+=2000; // IF LESS THAN 70 ASSUME 20xx
		else enteredYear+=1900; // ELSE ASSUME 19xx
	}
	var now=new Date();
	var nowMonth=now.getMonth()+1;
	var nowYear=now.getFullYear();
	// IF ENTERED YEAR IS GREATER THAN TODAY'S YEAR, IT'S VALID
	if (enteredYear>nowYear) return true;
	// IF TODAY'S MONTH IS GREATER THAN OR EQUAL TO ENTERED MONTH, IT'S VALID
	if (enteredYear==nowYear){
		if (enteredMonth>=nowMonth) return true;
	}
	return false;
}

/*
* Function: doUpload
* Modified from: http://www.websupergoo.com/helpupload/frames.htm
* Last Modified: Tony Ruscoe (18 August 2004)
* Syntax: doUpload([Object], [String]);
* Returns: appends a unique ID to the form action, opens a progress bar window and then submits the form
*/
var blnDoneUpload = false;
function doUpload(objFormElement, strLangCode){
	var intUniqueID=(new Date()).getTime()%1000000000;
	if (!blnDoneUpload){
		blnDoneUpload=true;
		objFormElement.disabled=true;
		var strUrl="/quote/upload_progress_bar_"+strLangCode+".asp?ID="+intUniqueID;
		var iHeight = isXPSP2 ? 140 : 120; // XP SP2 ADDS THE STATUS BAR REGARDLESS OF WHETHER WE SET "status:no" SO ADD 20 PIXELS TO THE HEIGHT;
		var iWidth = 500;
		if (window.showModelessDialog){
			window.showModelessDialog(strUrl, window, "dialogHeight:"+eval(iHeight+20)+"px;dialogWidth:"+eval(iWidth+10)+"px;help:no;scroll:no;status:no;");
		}else{
			window.open(strUrl, "progress", "directories=no,height="+iHeight+",left="+((screen.width-500)/2)+",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,top="+((screen.height-120)/2-50)+",width="+iWidth);
		}
		objFormElement.form.action+="?ID="+intUniqueID;
		objFormElement.form.submit();
	}
}

/*
* Function: setcookie
* Last Modified: Tony Ruscoe (11 July 2002)
* Syntax: setcookie([String], [String]);
* Returns: creates a cookie with the specified name and value
*/
function setCookie(strName, strValue){
	var expires=new Date(2015,9,21,12,0,0);
	document.cookie=strName+"="+escape(strValue)+"; expires="+expires.toGMTString()+"; path=/";
}

/*
* Function: deleteCookie
* Last Modified: Tony Ruscoe (11 July 2002)
* Syntax: deleteCookie([String], [String]);
* Returns: deletes the cookie with the specified name
*/
function deleteCookie(strName){
	document.cookie=strName+"=''; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/";
}

/*
* Function: openPopup
* Last Modified: Tony Ruscoe (30 January 2004)
* Syntax: openPopup([String]);
* Returns: pops up window
*/
function openPopup(strUrl){
	window.open(strUrl, "popup", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=350,top="+((screen.height-350)/2-50)+",left="+((screen.width-450)/2));
}

/*
* Function: safeSubmit
* Last Modified: Tony Ruscoe (26 June 2002)
* Syntax: safeSubmit([Object]);
* Returns: submits the form if not already submitted and disables the submit button.
*/
var blnSubmitted=false;
function safeSubmit(objFormElement){
	if (!blnSubmitted){
		blnSubmitted=true;
		objFormElement.disabled=true;
		objFormElement.form.submit();
	}
}

/*
* Function: checkAll
* Last Modified: Tony Ruscoe (3 July 2002)
* Syntax: checkAll([Object]);
* Returns: checks all checkboxes in the same form as the element passed to the function
*/
function checkAll(objFormElement){
	with (objFormElement.form){
		for (var i = 0; i < elements.length; i++){
			if (elements[i].type == "checkbox"){
				elements[i].checked = true;
			}
		}
	}
}

/*
* Function: checkNone
* Last Modified: Tony Ruscoe (3 July 2002)
* Syntax: checkNone([Object]);
* Returns: unchecks all checkboxes in the same form as the element passed to the function
*/
function checkNone(objFormElement){
	with (objFormElement.form){
		for (var i = 0; i < elements.length; i++){
			if (elements[i].type == "checkbox"){
				elements[i].checked = false;
			}
		}
	}
}

/*
* Function: checkInvert
* Last Modified: Tony Ruscoe (3 July 2002)
* Syntax: checkInvert([Object]);
* Returns: inverts the select of checked checkboxes in the same form as the element passed to the function
*/
function checkInvert(objFormElement){
	with (objFormElement.form){
		for (var i = 0; i < elements.length; i++){
			if (elements[i].type == "checkbox"){
				elements[i].checked = !elements[i].checked;
			}
		}
	}
}

/*
* Function: populateDate
* Last Modified: Tony Ruscoe (3 June 2003)
* Syntax: populateDate([Object]);
* Returns: resets the number of days to prevent non-dates from being selected
*/
function populateDate(objDay, objMonth, objYear) {
	var iDay = objDay.selectedIndex;
	var dtmA = new Date(objYear.options[objYear.selectedIndex].value, objMonth.options[objMonth.selectedIndex].value, 1);
	var dtmDiff = dtmA - 86400000;
	var dtmB = new Date(dtmDiff);
	var iDaysInMonth = dtmB.getDate();
	if (iDaysInMonth != objDay.length) {
		objDay.length = null;
		for (var i = 0; i < iDaysInMonth; i++) objDay.options[i] = new Option(i+1,i+1);
		objDay.options.selectedIndex = (iDay+1 > iDaysInMonth) ? iDaysInMonth-1 : iDay;
	}
}

/*
* Function: toggleRow
* Last Modified: Tony Ruscoe (13 January 2004)
* Syntax: toggleRow([Object]);
* Returns: toggles the class of a row to highlight cells 
*/
function toggleRow(oRow){
	oRow.className = (oRow.className!="on") ? "on" : "";
}

/*
* Function: printPage
* Last Modified: Tony Ruscoe (14 January 2004)
* Syntax: printPage();
* Returns: opens the window's print dialog or alerts instructions on how to print
*/
function printPage(){
	window.focus();
	if(window.print){window.print();}
	else{alert("How to print this page:\n\n- On your browser's \"Edit\" menu, click \"Print\".\n\nPC Shortcut:\tCtrl+P\n\nMac Shortcut:\tCommand+P");}
}

/*
* Function: isChecked
* Last Modified: Tony Ruscoe (21 January 2004)
* Syntax: isChecked([Object]);
* Returns: returns whether the specified checkbox / radio button collection has at least one element checked
*/
function isChecked(objElement){
	var blnChecked = false;
	if (isNaN(objElement.length)){
		if(objElement.checked) blnChecked=true;
	}else{
		for (i=0;i<objElement.length;i++){
			if(objElement[i].checked) blnChecked=true;
		}
	}
	return blnChecked;
}

/*
* Function: showTable
* Last Modified: Tony Ruscoe (02 February 2004)
* Syntax: showTable([String]);
* Returns: shows the table with the specified ID and hides the link
*/
function showTable(strID){
	if(isDOM&&document.getElementById(strID)){
		document.getElementById(strID).style.display='block';
		document.getElementById(strID+'Link').style.display='none';
	}
}

/*
* Function: hideTable
* Last Modified: Tony Ruscoe (02 February 2004)
* Syntax: hideTable([String]);
* Returns: hides the table with the specified ID and shows the link
*/
function hideTable(strID){
	if(isDOM&&document.getElementById(strID)){
		document.getElementById(strID).style.display='none';
		document.getElementById(strID+'Link').style.display='block';
	}
}

/*
* Function: urlEncode
* Last Modified: Tony Ruscoe (02 February 2004)
* Syntax: urlEncode([String]);
* Returns: a URL encoded string
*/
function urlEncode(str){
	str = escape(str);
	// THESE NEED TO BE IN SQUARE BRACKETS SO IT DOESN'T COCK UP WHEN WE COMPRESS THE FILE
	str = str.replace(/[\*]/gi,"%2A");
	str = str.replace(/[\+]/gi,"%2B");
	str = str.replace(/[-]/gi,"%2D");
	str = str.replace(/[\.]/gi,"%2E");
	str = str.replace(/[\/]/gi,"%2F");
	str = str.replace(/[@]/gi,"%40");
	str = str.replace(/[_]/gi,"%5F");
	return str;
}

/*
* Function: trackLink
* Last Modified: Tony Ruscoe (02 February 2004)
* Syntax: trackLink([Object]);
* Returns: tracks the link clicked by redirecting the page
*/
function trackLink(oLink){
	var sText = "";
	if (document.getElementById){
		if(oLink.innerText){ // FOR INTERNET EXPLORER
			sText = oLink.innerText.replace(/\s+/gi," ");
		}else if(oLink.textContent){ // FOR MOZILLA
			sText = oLink.textContent.replace(/\s+/gi," ");
		}else if(oLink.text){ // FOR NETSCAPE, BUT IT DOESN'T WORK PROPERLY
			sText = oLink.text.replace(/\s+/gi," ");
		}else if(oLink.innerHTML.search(/<img/i) == 0){ // FOR IMAGES
			sText = oLink.firstChild.alt.replace(/\s+/gi," ")+" "+"<"+oLink.firstChild.src+">"; // THE SPACE NEEDS TO BE INSERTED LIKE THIS SO IT DOESN'T COCK UP WHEN WE COMPRESS THE FILE
		}
	}
	if(oLink.href.search(/redirect.asp/gi)==-1){
		oLink.href="/redirect.asp?r="+urlEncode(oLink.href)+"&p="+urlEncode(window.location)+"&t="+urlEncode(sText);
	}
}

/*
* Function: doRedirect
* Last Modified: Tony Ruscoe (02 February 2004)
* Syntax: doRedirect([String], [String], [String]);
* Returns: tracks the link clicked by redirecting the page
*/
function doRedirect(sLink,sText,sTarget){
	window.open("/redirect.asp?r="+urlEncode(sLink)+"&p="+urlEncode(window.location)+"&t="+urlEncode(sText), sTarget);
}

/********
 * JavaScript fix for the 2nd level menu dropdowns in IE6 because it doesn't support li:hover
********/
EnableMenu = function() {
	if (document.all && document.getElementById && document.getElementById("Menu")) {
		oRoot = document.getElementById("Menu");
		for (i=0; i<oRoot.childNodes.length; i++) {
			oNode = oRoot.childNodes[i];
			if (oNode.nodeName == "LI") {
				oNode.onmouseover = function() { this.className += " over"; }
				oNode.onmouseout = function() { this.className = this.className.replace(" over", ""); }
			}
		}
	}
}
//window.onload=EnableMenu;
SafeAddOnload(EnableMenu);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  ALTTXT V1.2
//  BY: BRIAN GOSSELIN OF SCRIPTASYLUM.COM
//  ADDED FADING EFFECT FOR IE4+ AND NS6+ ONLY AND OPTIMIZED THE CODE A BIT.
//  SCRIPT FEATURED ON DYNAMIC DRIVE (http://www.dynamicdrive.com) 

var blnFade=true;       // ENABLES FADE-IN EFFECT FOR IE4+ AND NS6 ONLY
var blnCenterBox=false;  // CENTERS THE BOX UNER THE MOUSE, OTHERWISE DISPLAYS BOX TO THE RIGHT OF THE MOUSE
var blnCenterText=false; // CENTERS THE TEXT INSIDE THE BOX. YOU CAN'T SIMPLY DO THIS VIA STYLE BECAUSE OF NS4.
                         // OTHERWISE, TEXT IS LEFT-JUSTIFIED.

////////////////////////////// NO NEED TO EDIT BEYOND THIS POINT //////////////////////////////////////
var w_y, w_x, objTooltip, intBoxHeight, intBoxWidth;
var isHover=false;
var isLoaded=false;
var intOpacity=0;
var intOpacityID=0;

function getWindowDimensions(){
w_y=(isNS4||isNS6)? window.innerHeight : (isIE5or6||isIE4)? document.body.clientHeight : 0;
w_x=(isNS4||isNS6)? window.innerWidth : (isIE5or6||isIE4)? document.body.clientWidth : 0;
}

function getBoxWidth(){
if(isNS4)intBoxWidth=(objTooltip.document.width)? objTooltip.document.width : objTooltip.clip.width;
if(isIE5or6||isIE4)intBoxWidth=(objTooltip.style.pixelWidth)? objTooltip.style.pixelWidth : objTooltip.offsetWidth;
if(isNS6)intBoxWidth=(objTooltip.style.width)? parseInt(objTooltip.style.width) : parseInt(objTooltip.offsetWidth);
}

function getBoxHeight(){
if(isNS4)intBoxHeight=(objTooltip.document.height)? objTooltip.document.height : objTooltip.clip.height;
if(isIE4||isIE5or6)intBoxHeight=(objTooltip.style.pixelHeight)? objTooltip.style.pixelHeight : objTooltip.offsetHeight;
if(isNS6)intBoxHeight=parseInt(objTooltip.offsetHeight);
}

function moveTooltip(x,y){
if(isNS4)objTooltip.moveTo(x,y);
if(isDOM||isIE4){
objTooltip.style.left=x+'px';
objTooltip.style.top=y+'px';
}}

function getScrollVertical(){
if(isNS4||isNS6)return window.pageYOffset;
if(isIE5or6||isIE4)return document.body.scrollTop;
}

function getScrollHorizontal(){
if(isNS4||isNS6)return window.pageXOffset;
if(isIE5or6||isIE4)return document.body.scrollLeft;
}

function writeTooltip(strText){
strText=strText.replace(/\n/gi, "<br>");
if(isNS4){
objTooltip.document.open();
objTooltip.document.write(strText);
objTooltip.document.close();
}
if(isDOM||isIE4)objTooltip.innerHTML=strText;
}

function showTooltip(strText){
if(isLoaded){
isHover=true;
if(isNS4)strText='<div class="tooltip">'+((blnCenterText)?'<center>':'')+strText+((blnCenterText)?'</center>':'')+'</div>';
writeTooltip(strText);
getBoxHeight();
if((isDOM||isIE4)&&blnFade){
intOpacity=0;
doOpacity();
}}}

function hideTooltip(){
if(isLoaded){
if(isNS4)objTooltip.visibility="hide";
if(isIE4||isDOM){
if(blnFade)clearTimeout(intOpacityID);
objTooltip.style.visibility="hidden";
}
writeTooltip('');
isHover=false;
}
}

function doOpacity(){
if(intOpacity<=100){
intOpacity+=25;
if(isIE4||isIE5or6)objTooltip.style.filter="alpha(opacity="+intOpacity+")";
if(isNS6)objTooltip.style.MozOpacity=intOpacity/100;
intOpacityID=setTimeout('doOpacity()',5);
}}

function moveObj(evt){
if(isLoaded&&isHover){
intMargin=(isIE4||isIE5or6)?1:23;
if(isNS6)if(document.height+27-window.innerHeight<0)intMargin=15;
if(isNS4)if(document.height-window.innerHeight<0)intMargin=10;
//mx=(isNS4||isNS6)? evt.pageX : (isIE5or6||isIE4)? event.clientX : 0;
//my=(isNS4||isNS6)? evt.pageY : (isIE5or6||isIE4)? event.clientY : 0;
if(isNS4){mx=evt.pageX; my=evt.pageY;}
else if(isNS6){mx=evt.clientX; my=evt.clientY;}
else if(isIE5or6){mx=event.clientX; my=event.clientY;}
else if(isIE4){mx=0; my=0;}

if(isNS4){
mx-=getScrollHorizontal();
my-=getScrollVertical();
}
xoff=(blnCenterBox)?mx-intBoxWidth/2:mx+30;
yoff=(my+intBoxHeight-10-getScrollVertical()+intMargin>=w_y)?-5-intBoxHeight:10;
moveTooltip(Math.min(w_x-intBoxWidth-intMargin,Math.max(2,xoff))+getScrollHorizontal(),my+yoff+getScrollVertical());
if(isNS4)objTooltip.visibility="show";
if(isDOM||isIE4)objTooltip.style.visibility="visible";
}}

if(isNS4)document.captureEvents(Event.MOUSEMOVE);
document.onmousemove=moveObj;
function loadTooltip(){
  objTooltip=(isNS4)?document.layers['objTooltip']:(isIE4)?document.all['objTooltip']:(isDOM)?document.getElementById('objTooltip'):null;
  getBoxWidth();
  getBoxHeight();
  getWindowDimensions();
  isLoaded=true;
  if((isDOM||isIE4)&&blnCenterText)objTooltip.style.textAlign="center";
  if(isDOM)objTooltip.style.padding='4px';
  if(isIE4||isIE5or6&&blnFade)objTooltip.style.filter="alpha(opacity=0)";
}
window.onresize=getWindowDimensions;
SafeAddOnload(loadTooltip);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
