function isDigit(e, goodvals){   
	var key, keychar;
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// check goodkeys
	if (goodvals.indexOf(keychar) != -1)
		return true;

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;

	// else return false
	return false;
} 
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}
var win= null;
function NewWindow(mypage,myname,w,h,scroll){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
	  settings +='width='+w+',';
	  settings +='top='+wint+',';
	  settings +='left='+winl+',';
	  settings +='scrollbars='+scroll+',';
	  settings +='resizable=yes';

  win=window.open(mypage,myname,settings);
  if (window.focus){
  	win.focus();
  }
}

//Removes all the spaces from the given string
function Trim( rsString ){   
	var nIndex = 0;
	var lcReturnString = "";

	if (rsString.length <=0){
		llValid="F"
	}
			
	while (nIndex < rsString.length){ 
		if(rsString.charAt( nIndex )!= " "){
	 		lcReturnString = lcReturnString + rsString.charAt( nIndex )
			}
	 	nIndex++;
	}
			
	return (lcReturnString)
}

function isEmail(src) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(src);
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
	return true
}

function FormatNbr(total,decimals){
	var num = parseFloat(total);
	// First section sets non-number value to zero
    if (!(num = parseFloat(num)))
               num = "0.00";
    // Second section sets two decimal place format
    var Pad = "";
    num = "" + Math.floor(num * Math.pow(10,decimals + 1) + 5);
    // Pad if less than 0.10
    if(num.length < decimals+1){
		for(Count = num.length; Count <= decimals; Count++)
        Pad += "0";
	}
    num = Pad + num;
    // Parse into final string
    num = num.substring(0,num.length - decimals - 1) + "." + num.substring(num.length - decimals -1, num.length -1);
    // If less than 1 then add 0 to the left of the decimal
    if((num == "") || (parseFloat(num) < 1))
		num = "0" + num;
	// Final section returns formatted number
    return num;
}

function setZero(source){
	if (source.value == ''){
		source.value =0;
	}
	return;
}

function setDollarAmount(source){
	source.value=FormatNbr(source.value,2);
	return;
}

function yyToyyyy(source){
	var tyr = source.value;
	if ((tyr.length == 4) || (tyr == '')){
		return;
	}
	if (tyr.length == 2){
		if ((tyr * 1) > 95){
			source.value = '19'+source.value
		}
		if ((tyr * 1) < 20){
			source.value = '20'+source.value
		}
	}
	
	return;
}

function formatPhone(obj){
	var num=obj.value
	
	if (obj.value == ''){
		return;
	}else{
		obj.value = obj.value.toUpperCase();
		if (obj.value != 'N/A'){
			obj.value=obj.value.replace(/(\d{3})(\d{3})/,'('+'$1'+')'+'$2'+'-')
		}
	}
}

function verifyProductModel(pModel){
	var url;
	url='/util/verify_model.asp?pModel='+pModel
	NewWindow(url,'pmodel',200,100,'yes');
}

function presetValue(source){
	//this is use to blank out numeric '0.00' or '0' entry in numeric field when on focus
	if (source.value == ''){
		return;
	}
	if ((source.value * 1) == 0){
		source.value = '';
		return;
	}
	return;
}

function maximizeWin() {
	if (window.screen) {
		var aw = 800; //screen.availWidth;
		var ah = screen.availHeight;
		window.moveTo(screen.availWidth-800, 0);
		window.resizeTo(aw, ah);
	}
}
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}

function resetForm(theForm){
	theForm.reset();
	return true;
}

function SPSValidate(theForm, groupname)
{
	if(eval('theForm.'+groupname+'add.value') =='' || eval('theForm.'+groupname+'desc.value')=='')
	{
		alert("You must enter a name and description for the "+groupname);
		return true;	
	}
	theForm.submit();
}
function sendRequest(theForm){
	if (theForm.lname.value == ''){
		alert("Last Name is required.");
		theForm.lname.focus();
		return true;
	}
	if (theForm.fname.value == ''){
		alert("First Name is required.");
		theForm.fname.focus();
		return true;
	}
	if (theForm.street.value == ''){
		alert("Street address is required.");
		theForm.street.focus();
		return true;
	}
	if (theForm.city.value == ''){
		alert("City is required.");
		theForm.city.focus();
		return true;
	}
	if (theForm.state.value == 'N/A'){
		alert("State is required.");
		theForm.state.focus();
		return true;
	}
	if ((theForm.zip.value == '') || (theForm.zip.value.length < 5)){
		alert("Please enter your zip code.");
		theForm.zip.focus();
		return true;
	}
	if ((theForm.email.value != '') && (!check_email(theForm.email.value))){
		alert("Email entered is invalid.");
		theForm.email.focus();
		return true;
	}
	if ((theForm.area.value != '') || (theForm.prefix.value != '') || (theForm.number.value != '')){
		if (theForm.area.value.length != 3){
			alert("Invalid area code.");
			theForm.area.focus();
			return true;
		}
		if (theForm.prefix.value.length != 3){
			alert("Invalid prefix.");
			theForm.prefix.focus();
			return true;
		}
		if (theForm.number.value.length != 4){
			alert("Invalid number.");
			theForm.number.focus();
			return true;
		}
	}
	theForm.submit();
}
function showHideTable(theTable,theImg)
{
if (document.getElementById(theTable).style.display == 'none')
  {
  document.getElementById(theTable).style.display = 'block';
  document.getElementById(theImg).src='http://corporaterenting.com/images/collapse.bmp';
  }
  else
  {
  document.getElementById(theTable).style.display = 'none';
     document.getElementById(theImg).src='http://corporaterenting.com/images/expand.bmp';
  }
}

function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			return (false);
		}	
	} 
	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}