// JavaScript Document
function validateForm(frm) {
	for (cnt=0; cnt < frm.elements.length; cnt++) {
		
		ctl = frm.elements[cnt];
		
		value = trim(frm.elements[cnt].value);
		type = frm.elements[cnt].type.toUpperCase();
		title = frm.elements[cnt].title;
		
		req = frm.elements[cnt].accept;
		
		if (req != "" && req != null && req != "0") {
			
			req = req.toUpperCase();
			
			//textbox
			if (type == "TEXT" || type == "TEXTAREA") {
				
				//Simple blnak value check											[MUST]
				if (req.indexOf("MUST") != -1) {
					if (value == "" || value == null) {
						alert("Please enter \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("MUST", "");
				}
				
				//Integer value check												[(MUST)INT]
				if (req.indexOf("INT") != -1) {
					if (isNaN(value)) {
						alert("Please enter numeric values for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("INT", "");
					
					//Positive Integer Check										[(MUST)INT+]
					if (req.indexOf("+") != -1 && parseInt(value) <= 0) {
						alert("Please enter positive numerics for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("+", "");
					
					//Negative Integer Check										[(MUST)INT-]
					if (req.indexOf("-") != -1 && parseInt(value) >= 0) {
						alert("Please enter negative numerics for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("-", "");
					

					//Comparison Integer Check										[(MUST)INT<=X]
					if (req.indexOf("<=") != -1 && 
					(parseInt(value) > parseInt( req.substr(req.indexOf("<=")+2, req.length - req.indexOf("<=")) ))
					) {
						alert("Please enter numerics <= " + req.substr(req.indexOf("<=")+2, req.length - req.indexOf("<=")) + " for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("<=", "");
					
					//																[(MUST)INT>=X]
					if (req.indexOf(">=") != -1 && 
					(parseInt(value) < parseInt( req.substr(req.indexOf(">=")+2, req.length - req.indexOf(">=")) ))
					) {
						alert("Please enter numerics >= " + req.substr(req.indexOf(">=")+2, req.length - req.indexOf(">=")) + " for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace(">=", "");
					
					continue;
				}
				
				
				//Double value check												[(MUST)DBL]
				if (req.indexOf("DBL") != -1) {
					if (isNaN(value)) {
						alert("Please enter numeric values for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("DBL", "");
					
					//Positive Double Check											[(MUST)DBL+]
					if (req.indexOf("+") != -1 && parseFloat(value) <= 0) {
						alert("Please enter positive numerics for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("+", "");
					
					//Negative Double Check											[(MUST)DBL-]
					if (req.indexOf("-") != -1 && parseFloat(value) >= 0) {
						alert("Please enter negative numerics for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("-", "");
					

					//Comparison Double Check										[(MUST)DBL<=X]
					if (req.indexOf("<=") != -1 && 
					(parseFloat(value) > parseFloat( req.substr(req.indexOf("<=")+2, req.length - req.indexOf("<=")) ))
					) {
						alert("Please enter numerics <= " + req.substr(req.indexOf("<=")+2, req.length - req.indexOf("<=")) + " for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("<=", "");
					
					//																[(MUST)DBL>=X]
					if (req.indexOf(">=") != -1 && 
					(parseFloat(value) < parseFloat( req.substr(req.indexOf(">=")+2, req.length - req.indexOf(">=")) ))
					) {
						alert("Please enter numerics >= " + req.substr(req.indexOf(">=")+2, req.length - req.indexOf(">=")) + " for \"" + title + "\"");
						ctl.focus();
						return (false);

					}
					req = req.replace(">=", "");
					
					continue;
				}
				
				//Characters and string value check
				if (req.indexOf("LEN") != -1) {
					if (req.indexOf("LEN=") != -1 && value.length != parseInt(req.substr(req.indexOf("LEN=")+4)) ) {
						alert("Please enter " + req.substr(req.indexOf("LEN=")+4) + " characters for " + title);
						ctl.focus();
						return (false);
					}
					
					if ( (req.indexOf("LEN<") != -1) && !(value.length <= parseInt(req.substr(req.indexOf("LEN<")+4))) ) {
						alert("Please enter atmost " + req.substr(req.indexOf("LEN<")+4) + " characters for " + title);
						ctl.focus();
						return (false);
					}
					
					if ( (req.indexOf("LEN>") != -1) && !(value.length >= parseInt(req.substr(req.indexOf("LEN>")+4))) ) {
						alert("Please enter atleast " + req.substr(req.indexOf("LEN>")+4) + " characters for " + title);
						ctl.focus();
						return (false);
					}
				}
				
				//18+ Age															[AGE18+]
				if (req == "AGE18+") {
					if (isNaN(value)) {
						alert("Please enter numeric values for \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					
					if (parseInt(value) < 18) {
						alert("You should be 18+ to fill this form.");
						ctl.focus();
						return (false);
					}
					
					continue;
				}
				
				//Character Checks
				
				
				//Date Checks														[DATE*][MMDDYY]
				if (req.indexOf("DATE") != -1 && value != "") {
					if (isDate(req)) {
						alert("Please enter valid date for \"" + title + "\". [Format :mm/dd/yyyy]");
						ctl.focus();
						return (false);
					}
					
					
					if (req.indexOf("FUTURE") != -1) {
						
						var dt = new Date();
						var dtVal = new Date(value);
						
						if (dt > dtVal) {
							alert("Please enter a future date for \"" + title + "\"");
							ctl.focus();
							return (false);
						}
						req = req.replace("FUTURE", "");
					}
					
				}//DATE CHECK
				
				//EMAIL
				if (req.indexOf("EMAIL") != -1 && value != "") {
					if (!isValidEmailStrict(value)) {
						alert("Please enter valid " + title);
						ctl.focus();
						return (false);
					}
					
				}//EMAIL CHECK
			}

			if (type == "PASSWORD") {
				if (value == "" || value == null) {
					alert("Please enter \"" + title + "\"");
					ctl.focus();
					return (false);
				}
			}

			if (ctl == "CHECKBOX") {
				if (ctl.checked == false) {
					alert("Please check \"" + title + "\" to proceed.");
					ctl.focus();
					return (false);
				}
			}
			
			if (ctl.tagName == "SELECT") {
				if (value == "") {
					alert("Please select \"" + title + "\" to proceed.");
					ctl.focus();
					return (false);
				}
			}
			
			//Simple blnak value check											[MUST]
			if (type == "FILE") {
				if (req.indexOf("MUST") != -1) {
					if (value == "" || value == null) {
						alert("Please enter \"" + title + "\"");
						ctl.focus();
						return (false);
					}
					req = req.replace("MUST", "");
				}
			}//FILE IF
			
		}
	}
	
	return (true);
}





////////////////////////////////////////////////
////////////////STRING FUNCTIONS////////////////
////////////////////////////////////////////////
function trim( str ) {
	// Immediately return if no trimming is needed
	if( (str.charAt(0) != ' ') && (str.charAt(str.length-1) != ' ') ) { return str; }
	// Trim leading spaces
	while( str.charAt(0)  == ' ' ) {
		str = '' + str.substring(1,str.length);
	}
	// Trim trailing spaces
	while( str.charAt(str.length-1)  == ' ' ) {
		str = '' + str.substring(0,str.length-1);
	}
	return str;
}

// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) {
	if (string.replace) {
		string.replace(/[<>\"\'%;\)\(&\+]/, '');
	}
	return string;
}

// Check that a string contains only letters
function isAlphabetic(string) {
	return isAlphabetic1(string, true);
}

function isAlphabetic1(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only numbers
function isNumeric(string) {
	return isNumeric1(string, false);
}

function isNumeric1(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

// Remove all spaces from a string
function trimAll(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
}

// Check that a string contains only letters and numbers
function isAlphanumeric(string) {
	return isAlphanumeric1(string, false);
}
function isAlphanumeric1(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
	}
	return true;
}

// Check that the number of characters in a string is between a max and a min
function isValidLength(string, min, max) {
	if (string.length < min || string.length > max) return false;
	else return true;
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
	if (address.indexOf('@') < 3) return false;
	var name = address.substring(0, address.indexOf('@'));
	var domain = address.substring(address.indexOf('@') + 1);
	if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;
	if (domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;
	return true;
}
// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(address) {
	if (isValidEmail(address) == false) return false;
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1) return false;
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
	return true;
}


////////////////////////////////////////////////
////////////////DATE VALIDATION/////////////////
////////////////////////////////////////////////
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 dtCh= "/";
	var minYear=1900;
	var maxYear=2100;
	
	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 checkAllCB(cb, blnVal) {
	var iCnt;
	for (iCnt=0; iCnt < cb.length; iCnt++) {
		cb[iCnt].checked = blnVal;
	}
}

function checkAllFRM(frm, blnVal) {
	var iCnt;
	for (iCnt=0; iCnt < frm.elements.length; iCnt++) {
		if (frm.elements[iCnt].type == 'checkbox') {
			frm.elements[iCnt].checked = blnVal;
		}
	}
}

function isAllChecked(cb, resCb) {
	var iCnt;
	for (iCnt=0; iCnt < cb.length; iCnt++) {
		if (!cb[iCnt].checked) {
			break;
		}
	}
	if (iCnt == cb.length)
		resCb.checked = true;
	else
		resCb.checked = false;
}

function isAnyCheckedFRM(frm, msg) {
	for (var iCnt = 0; iCnt < frm.elements.length; iCnt++) {
		if (frm.elements[iCnt].type.toLowerCase() == "checkbox") {
			if (frm.elements[iCnt].checked) {
				return (true);
			}
		}
	 }
	alert(msg);
	return (false);
}

//
function openFixWin(argURL, argSize) {
	window.open(argURL, "x_win" + (new Date()).getTime(), "resizable=no," + argSize);
}

function openWin(argURL, argSize) {
	window.open(argURL, "x_win" + (new Date()).getTime(), "resizable=yes,scrollbars=yes," + argSize);
}

function clearCombo(varCombo) {
	for (var iCnt = varCombo.options.length; iCnt >= 0; --iCnt)
		varCombo.options[iCnt] = null;
}


function Highlight(e) {
	var r = null;
	
	r = document.getElementById("tr_" + e.value).className;
	
	if (r == "trListValue")
		r = "trListValue1";
	else
		r = "trListValue";
	
	document.getElementById("tr_" + e.value).className = r;
}

function createIDs(e) {
	var nm = e.name;
	var cb = eval("document.frmMain." + e.name);
	
	var t = "";
	var f = "";
	
	if (cb.length) {
		for (var i = 0; i < cb.length; i++) {
			if (cb[i].checked)
				t += "'" + cb[i].value + "',";
			else
				f += "'" + cb[i].value + "',";
		}
		t = t.substr(0, t.length - 1);
		f = f.substr(0, f.length - 1);
	}
	else {
		if (cb.checked)
			t = "'" + cb.value + "'";
		else
			f = "'" + cb.value + "'";
	}
	
	document.getElementById(nm.replace("cb_", "h_")).value = t + "|" + f;
}

function doChangeWay(ctl, val) {
	eval("document.frmMain." + ctl + ".value = val;");
	document.frmMain.act.value = ctl;
	document.frmMain.submit();
}

function doChangeSort(argSort) {
	var sort = document.frmMain.sort.value;
	var order = document.frmMain.order.value.toLowerCase();
	
	if (sort == argSort) {
		if (order == "asc")
			order = "desc";
		else
			order = "asc";
	}
	else {
		order = "asc";
	}
	
	document.frmMain.sort.value = sort;
	document.frmMain.order.value = order;
	document.frmMain.submit();
	alert(document.frmMain.action);
}
