/*
	Filename         : functions.js
	Last modified by : gabram
	Created          : Tuesday, March 30, 2004 10:13:26 AM
	Last Updated     : Thursday, August 19, 2004 12:45:43 PM
	Comments         : 
*/

function debug(txt) {
	if (typeof(document.debug)!='undefined') {
		if (txt == 'cls') {
			document.debug.out.value='';
		} else {
			document.debug.out.value = document.debug.out.value+txt;
			document.debug.out.scrollTop = document.debug.out.scrollHeight;
		}
	}
}

function valEmpty(o,t) {
	var val = o.value;
	if (val == null) val = "";
	if (trim(val) == "") {
		if (t.length > 0) alert(t);
		focusObject(o);
		return(false);
	}
	return(true);
}

function focusObject(o) {
	if (o.type == 'hidden') return(false);
	o.focus();
	if ((o.type == 'text')||(o.type == 'password')) o.select();
}

function focusLogin(o1,o2) {
	if (o1.value.length==0) {
		focusObject(o1);
	} else {
		focusObject(o2)
	}
}
function formatCurrency(amount) {
	amount = amount.toString().replace(/\$|\,/g,'');
	if(isNaN(amount)) {amount = "0";}
		var sign = (amount == (amount = Math.abs(amount)));
		amount = Math.floor(amount*100+0.50000000001);
		var cents = amount%100;
		amount = Math.floor(amount/100).toString();
	if(cents<10) {cents = "0" + cents;}
	for (var i = 0; i < Math.floor((amount.length-(1+i))/3); i++)
		amount = amount.substring(0,amount.length-(4*i+3))+','+
		amount.substring(amount.length-(4*i+3));
	return (((sign)?'':'-') + '$' + amount + '.' + cents);
}
function getRadio(radioName) {
		if (ns||opera) {
			for (i=0;i<10;i++) {
				if (eval('document.form.'+radioName+'['+i+'].checked')) {return i;}
			}
	  }
		if (ie) {
		  var coll;
		  if (ie) coll = document.all[radioName];			
		  for (i=0;i<coll.length;i++) {
		    if (coll[i].checked)
				return(i);
		  }
		}
	}

function ltrim(txt) {
	return txt.replace(/^\s+/,'');
}

function rtrim(txt) {
	return txt.replace(/\s+$/,'');
}

function trim(txt) {
	return txt.replace(/^\s+/,'').replace(/\s+$/,'');
}

function validateDate(dDate, sFormat) {
			// Validates dates in the d/m/y format from 01/01/1600 - 31/12/9999.
			// The days are validated for the given month and year. 
			// Leap years are validated for all 4 digits years from 1600-9999, and all 2 digits years except 00 since it could be any century (1900, 2000, 2100). 
			// Days and months must be 1 or 2 digits and may have leading zeros. 
			// Years must be 2 or 4 digit years. 4 digit years must be between 1600 and 9999. 
			// Date separator may be a slash (/), dash (-), or period (.)
	if (dDate.length == 0) {return false};
	if (sFormat.length == 0) {return false};
	var re 	= "";
	
	var f 	= sFormat.toLowerCase();
	switch (f) {
		case "dd/mm/yyyy":
		case "dd/mm/yy":
		case "dd-mm-yyyy":
		case "dd-mm-yy":
			re = /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/ ;
			break;
		case "mm/dd/yyyy" :
		case "mm/dd/yy" :
		case "mm-dd-yyyy" :
		case "mm-dd-yy" :
			re = /^(?:(?:(?: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})$/ ;
			break;
		case "yyyy/mm/dd" :
		case "yy/mm/dd" :
		case "yyyy-mm-dd" :
		case "yy-mm-dd" :
			alert(sFormat + " format is under development.");
			return false;
		default :
			alert("Invalid date picture in global.asa file. Please report this problem.");
			return false;
	}
	if (!re.test(dDate))  {return false;}
	return true;
}
function validateEmail(sEmail) {
	var re = /^([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})(\]?)$/;
	if (!re.test(sEmail)) {return false;}
	return true;
}

function validateZipCode(val) {
	var oRe  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
  return oRe.test(val);
}
function validatePostalCode(val) {
	// "V1Y 5Y8" or "V1Y5Y8"
	var oRe  = /^([a-z]{1}\d[a-z]{1})( |-)?(\d[a-z]{1}\d)$/i
  return oRe.test(val);
}
function validatePhone(val) {
	//"(999) 999-9999" or "(999)999-9999"
	var oRe  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
  return oRe.test(val);
}
function validateCreditCard(card, n) {
	if (card == "VI") {// Visa: len 16, prefix 4, dashes optional.
		var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	} else if (card == "MC") {// MC: len 16, prefix 51-55, dashes optional.
		var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	} else if (card == "DS") {// Discover: len 16, prefix 6011, dashes optional.
		var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	} else if (card == "AX") {// Amex: len 15, prefix 34 or 37.
		var re = /^3[4,7]\d{13}$/;
	} else if (card == "DI") {// Diners: len 14, prefix 30, 36, or 38.
		var re = /^3[0,6,8]\d{12}$/;
	}
   if (!re.test(n)) return false;
   // Checksum Mod 10, see http://perl.about.com/library/weekly/aa073000a.htm
   var checksum = 0;
   for (var i=(2-(n.length % 2)); i<=n.length; i+=2) {
      checksum += parseInt(n.charAt(i-1));
   }
   for (var i = ( n.length % 2) + 1; i < n.length; i += 2) {
      var digit = parseInt(n.charAt(i-1)) * 2;
      if (digit < 10) {
				checksum += digit; 
			} else { 
				checksum += (digit-9); 
			}
   }
   if ((checksum % 10) == 0) return true; 
	 else return false;
}