function validateForm(frm) 
{	

		var  name = trim( frm.name.value);
		var email = trim(frm.email.value);  
		var  company = trim( frm.company.value);
		var  comments = trim( frm.comments.value);

		if (name == "" || name== null ) 
		{
				alert("Please enter \"Name\"");
				frm.name.focus();
				return (false);
		}
		
		if (email == "" || email== null) 
		{
			alert("Please enter \"Email Id\"");
			frm.email.focus();
			return (false);
		}
		
		if (!isValidEmailStrict(email)) 
		{
			alert("Please enter valid \"Email Id\"");
			frm.email.focus();
			return (false);
		}
		
		if (company == "" || company== null ) 
		{
				alert("Please enter \"Company Name\"");
				frm.company.focus();
				return (false);
		}
		
		if (comments == "" || comments== null ) 
		{
				alert("Please enter \"Comments\"");
				frm.comments.focus();
				return (false);
		}
		
		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('@') < 1) 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 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 check_URL(theurl) 
{
     var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
     if (tomatch.test(theurl))
     {
         window.alert("URL OK.");
         return true;
     }
     else
     {
         window.alert("URL invalid. Try again.");
         return false; 
     }
}


//
function openFixedWindow(argURL, argSize) {
	window.open(argURL, "newWindow", "resizable=no," + argSize);
}

function openImageWindow(argURL, argSize) {
	window.open(argURL, "newImageWindow", "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) {
	document.getElementById(ctl).value = val;
	//eval("document.frmMain." + ctl + ".value = val;");
	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.location = document.frmMain.action + "?sort=" + argSort + "&order=" + order;
}

var popUpWin=0;
function popUpWindow(URLStr,width,height,top,left)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'SendSms', 'width='+width+',height='+height+',top='+top+',left='+left+'');
}

