// JavaScript Document
function isEmpty(fieldvalue) {
	if (fieldvalue == "") { return true; } 
	return false; 
}//end isEmpty

/*
isNumeric function - check if field contains non-numeric value
*/
function isNumeric(fieldvalue) {
	
	var field = fieldvalue;
	var valid = "0123456789"; //valid character
		
	var accept = false; //assume field contains non-numeric value
	var temp;
	if (field != "") { //if field is not empty, process checking
		for (var i=0; i<field.length; i++) { //for-loop, check every character
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) != "-1") accept = true; //Numeric value
			else accept = false; // else not accept

			if (!accept) { //if field value contains non-numeric value, return errmsg
				return false; //contains non-numeric value;
			}//end if
		}// end for-loop
   }//end if 
   return true; //contains all numeric value
} //End isNumeric

/*
isDouble function - checks if field contains double value
*/
function isDouble(fieldvalue) {
	
	var field = fieldvalue;
	var valid = "0123456789."; //valid character
	var dotCount = 0;
		
	var accept = false; //assume field not contains double value
	var temp;
	if (field != "") { //if field is not empty, process checking
		for (var i=0; i<field.length; i++) { //for-loop, check every character
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) != "-1") {
				accept = true; //Numeric value
				if (temp == ".") dotCount++;
			} else {
				accept = false; // else not accept
			}

			if ((!accept)||(dotCount > 1)) { //if field value not contains double value, return errmsg
				return false; //contains non-numeric value;
			}//end if
		}// end for-loop
   }//end if 
   return true; //contains all numeric value
} //End isDouble


/*
isText function - check if field contains non-text value
*/
function isText(fieldvalue) {
	
	var field = fieldvalue;
	
	var invalid = "0123456789`~!#$%^||*_=+[]{};\"\\</?>"; //Invalid Character
	var accept = true; //assume field contains all text value
	var temp;
	
	//Check if field contains text value
	if (field != "") {
		for (var i=0; i<field.length; i++) { //for loop, check every character
			temp = "" + field.substring(i, i+1);
			if (invalid.indexOf(temp) != "-1") accept = false; //contains non-text value
		}//end for loop
	} //end if
	
	if (!accept) {//if field contains non-text value
		return false;
   }//end if
     
	return true; //return empty string, contains all text value
}//end isText

/*
isDate function - check if field contains valid date in required format
*/
function isDate(fieldvalue) {
	
	var dateStr = fieldvalue;
	var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/; //Format requirement
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	//If date format is not correct return errmsg
	if (matchArray == null) {
	return "<li>Date format as yyyy-mm-dd</li>";
	}
	
	month = matchArray[3]; // parse date into variables
	day = matchArray[5];
	year = matchArray[1];
	
	if (month < 1 || month > 12) { // check month range
		return "<li>month must be between 1 and 12</li>";
	}
	
	if (day < 1 || day > 31) {
		return "<li>day must be between 1 and 31</li>";
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return "<li>month "+month+" doesn't have 31 days</li>";
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			return "<li>February " + year + " doesn't have " + day + " days</li>";
		}
	}
	return ""; // date is valid
}//End isDate

/*
isSelected function - check status of list box/drop down box, 
*/
function isSelected (fieldvalue) {
	if (fieldvalue == -1) { return false };
	return true;
} //end isSelected

/*
isRBChecked function - check radio button group status
*/
function isRBChecked(btngrp) {
    var cnt = -1;
    for (var i=btngrp.length-1; i > -1; i--) {
        if (btngrp[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return true;
    else return false;
}
  
/*
isCBChecked function - check whether the checkbox is ticked
*/
function isChkBoxChecked(chkbox) {
	if (chkbox.checked)
		return true;
	return false;
}

/*
formatCurrency function - to format the number in currency format with dollar sign
*/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

/*
Check to see if its a valid email address
*/
function isValidEmail(fieldvalue) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i	
	if (filter.test(fieldvalue))
		return true;
	else
		return false;
} //end isValidEmail

function go2url(url) {
	window.location = url;
}

/* Date Selector ***************************************************************************************/

var date_arr = new Array;
var days_arr = new Array;

date_arr[0]=new Option("January",31);
date_arr[1]=new Option("February",28);
date_arr[2]=new Option("March",31);
date_arr[3]=new Option("April",30);
date_arr[4]=new Option("May",31);
date_arr[5]=new Option("June",30);
date_arr[6]=new Option("July",31);
date_arr[7]=new Option("August",30);
date_arr[8]=new Option("September",30);
date_arr[9]=new Option("October",31);
date_arr[10]=new Option("November",31);
date_arr[11]=new Option("December",30);

function fill_select(f)
{
        document.writeln("<SELECT name=\"months\" onchange=\"update_days(this.form)\" class=\"combo\">");
        for(x=0;x<12;x++) {
				var monthvalue = x+1;
                document.writeln("<OPTION value=\"" + monthvalue + "\">" + date_arr[x].text);
		}
        document.writeln("</SELECT>&nbsp;<SELECT name=\"days\" class=\"combo\"></SELECT>");
        selection=f.months[f.months.selectedIndex].value;
}

function update_days(f)
{
        temp=f.days.selectedIndex;
        for(x=days_arr.length;x>0;x--)
        {
                days_arr[x]=null;
                f.days.options[x]=null;
         }
        selection=parseInt(date_arr[f.months.selectedIndex].value);
        ret_val = 0;
        if(f.months[f.months.selectedIndex].value == 2)
        {
                year=parseInt(f.years.options[f.years.selectedIndex].value);
                if (year % 4 != 0 || year % 100 == 0 ) ret_val=0;
                else
                        if (year % 400 == 0)  ret_val=1;
                        else
                                ret_val=1;
        }
        selection = selection + ret_val;
        for(x=1;x < selection+1;x++)
        {
                days_arr[x-1]=new Option(x);
                f.days.options[x-1]=days_arr[x-1];
				f.days.options[x-1].value=x;
        }
        if (temp == -1) f.days.options[0].selected=true;
        else
             f.days.options[temp].selected=true;
}
function year_install(f)
{
		var d = new Date();
		var thisyear = d.getFullYear();
		
        document.writeln("&nbsp;<SELECT name=\"years\" id=\"years\" onchange=\"update_days(this.form)\" class=\"combo\">")
        for(x=thisyear-99;x<thisyear+1;x++) document.writeln("<OPTION value=\""+x+"\">"+x);
        document.writeln("</SELECT>");
        update_days(f)
}
/* Date Selector ***************************************************************************************/