// Useful script functions
// #################################################################
var email = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
var strUsername = /^[a-zA-Z0-9]{3,20}$/;
var strPassword = /^[a-zA-Z0-9]{6,20}$/;
var zipcode = /^\d{5}(-\d{4})?$/;
//var phone = /^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/;
var phone = /^\d+$/;
var money = /^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{1,2})?$/;
var date = /^(([1-9])|(0[1-9])|(1[0-2]))\/(([1-9])|(0[1-9])|([1-3][0-9]))\/((\d{4}))$/;
//var date2 = /^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/;
var ccard = /^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/;
var cvv = /^\d{3,4}$/;
var ccexp = /^(([1-9])|(0[1-9])|(1[0-2]))\/(((0[1-9])|([1-3][0-9])))$/;
var ccexp2 = /^((0[1-9])|(1[0-2]))((0[1-9])|([1-3][0-9]))$/;
var numeric = /^\d/;
//var numeric = /^\d+$/;

function parseToNumeric(sString) {
	for(i = 1; i <= sString.length; i++) {
		sString = sString.replace(/([^0-9])+/, "");
	}
	return sString;
}

function trim(element) {
	var sTrim;
	if(element.name) {
		sTrim = element.value.replace(/(^\s+)|(\s+$)/g, "");
		element.value = sTrim;
	}else{
		sTrim = element.replace(/(^\s+)|(\s+$)/g, "");
	}
	return sTrim;
}//trim

//luhn cc validator
function ccValidator(ccnum) {
	var checkSum = 0;
	var iMult = 0;
	if(ccnum.length <= 0) return false;
	for(var i=ccnum.length;i>0;i--) {
		iDigit = (iMult % 2 + 1) * parseInt(ccnum.substring(i-1,i));
		checkSum += (iDigit > 9) ? (iDigit - 9) : iDigit;
		iMult++;
	}//for
	return (checkSum % 10 == 0) && (checkSum > 0);
}//ccValidator
// *****************************************************************
// *****************************************************************

//luhn cc validator
function ccValidator2(ccnum,typeIdx) {
	var ret;
	
	var checkSum = 0;
	var iMult = 0;
	for(var i=ccnum.length;i>0;i--) {
		iDigit = (iMult % 2 + 1) * parseInt(ccnum.substring(i-1,i));
		checkSum += (iDigit > 9) ? (iDigit - 9) : iDigit;
		iMult++;
	}//for
	ret = (checkSum % 10 == 0);
	if(!ret) return ret; 
	
	//typeindex 1=visa;2=ax;3=mc;4=dc
	switch (typeIdx) {
		case 1:
			return (ret && ( /^(4\d{3}-?\d{4}-?\d{4}-?\d{4})/.test(ccnum)));
		case 2:
			return (ret && ( /^3[4,7]\d{13}$/.test(ccnum)));
		case 3:
			return (ret && ( /^(5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4})/.test(ccnum)));
		case 4:
			return (ret && ( /^((6011)-?\d{4}-?\d{4}-?\d{4})/.test(ccnum)));
		default:
			return false;
	}//switch
}//ccValidator
// *****************************************************************
// *****************************************************************

//ABA Bankrouting check
function abaValidator(t) {
	var n = 0;
	for (i=0;i<t.length;i+=3) {
	n += parseInt(t.charAt(i),     10) * 3
	  +  parseInt(t.charAt(i + 1), 10) * 7
	  +  parseInt(t.charAt(i + 2), 10);
	}

	// If the resulting sum is an even multiple of ten (but not zero),
	// the aba routing number is good.

	return (n % 10 == 0) && (n > 0);
}


function formatMoney(mAmount) {
	var newMoneyAmount;
	
	newMoneyAmount = (Math.floor(Math.round(mAmount * 100)) / 100) + "";
	//format trailing 0's
	if (newMoneyAmount.lastIndexOf(".") < 0) {
		newMoneyAmount = newMoneyAmount + ".00";
	}else{
			
		newMoneyAmount = newMoneyAmount + "00";
		var temp = parseInt(newMoneyAmount);
		newMoneyAmount = (parseInt(newMoneyAmount) + newMoneyAmount.substring(newMoneyAmount.lastIndexOf("."), newMoneyAmount.lastIndexOf(".")+3));
	}
	//format comas
	var comaPosition;
	var replaceString;
	var numRequiredComas = parseInt((newMoneyAmount.length - 4)/3);
	//alert(numRequiredComas);
	for(i=1;i<=numRequiredComas;i++){
		comaPosition = newMoneyAmount.length - (i * 3) - (i - 1) - 3;
		replaceString = "," + newMoneyAmount.charAt(comaPosition);
		newMoneyAmount = newMoneyAmount.substring(0,comaPosition) + "," + newMoneyAmount.substring(comaPosition);
	}
	return newMoneyAmount;
}//function

function IsOn(oControl) {
	//oControl.runtimeStyle.backgroundColor = "#E3EBF1";
	oControl.style.backgroundColor = "#FEE800";
	//oControl.className = "IsOn";	// works but is slower
}
	
function IsOff(oControl) {
	//oControl.runtimeStyle.backgroundColor = "";
	oControl.style.backgroundColor = "#ffffff";
	//oControl.className = "IsOff";	// works but is slower
}

function fnImageSwap(imageSrc) {
	var imageFileName = imageSrc;
	if (imageFileName.lastIndexOf("plus.gif") != -1) {
		imageFileName = imageFileName.replace(/plus.gif/,"minus.gif");
	} else {
		imageFileName = imageFileName.replace(/minus.gif/,"plus.gif");
	}//if
	return imageFileName;
}//function imageSwap	

function popupHelpPage(sURL, iWidth, iHeight) {
//	window.open(sURL,"dPortalHelp","menubar=no,resizable=no,status=no,scrollbars=yes,width=" + iWidth + ",height=" + iHeight + ",location=no");
	window.open(sURL,"vPortalHelp","menubar=no,resizable=no,status=no,scrollbars=yes,width=" + iWidth + ",height=" + iHeight + ",location=no");
}

// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
var bites = document.cookie.split("; "); // break cookie into array of bites

function getCookie(name) { // use: getCookie("name");
	for (var i=0; i < bites.length; i++) {
		nextbite = bites[i].split("="); // break into name and value
		if (nextbite[0] == name) // if name matches
			return unescape(nextbite[1]); // return value
	}
	return null;
}
	
function setCookie2(name, value) { // use: setCookie("name", value);
	if (value != null && value != "")
		document.cookie=name + "=" + escape(value) // + "; expires=" + expiry.toGMTString();
	bites = document.cookie.split("; "); // update cookie bites
}
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");

  document.cookie = curCookie;
}



//rollovers

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)	x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; 
		if(!x.oSrc) 
			x.oSrc = (x.origSrc) ? x.origSrc : x.src;
			//x.oSrc=x.src; 
		x.src=a[i+2];}
}

function vpfn_AS_SetWindow(width,height) {
	try {
		//document.getElementById("f_content").width = width;
		document.getElementById("f_content").height = height;
		window.scrollTo(0,0);
	}catch(e){}
}

//keypress capture	
function checkkey(e) {
	return (String.fromCharCode(e.keyCode).match(/[\da-zA-Z]/)) ? true: false;
}//checkkey	

function onlinesupport() {
	var sURL = "http://www.monavie.com/phplive/request_email.php?l=peterd&x=1&deptid=0";
	window.open(sURL,"_onlinesupport","menubar=no,resizable=no,status=no,scrollbars=yes,width=111,height=111,location=no");
}


