function LTrim(str) 
{ 
	 for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
	 return str.substring(k,str.length);
}
function RTrim(str) 
{
	 for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
	 return str.substring(0,j+1);
}
function Trim(str) 
{
	 return LTrim(RTrim(str));
}


/////////////// For Empy Fields Validation ///////////////////
function isEmpty(thisfield, msg)
{
	if (Trim(thisfield.value) == "")
	{
		alert(msg);
		thisfield.focus();
		return false;
	}
	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For Zip Code Validation ////////////////////
function isZipCode(thisfield)
{
	field = thisfield.value;
	var valid = "0123456789-";
	var hyphencount = 0;
	
	if (field.length!=5 && field.length!=10)
	{
		alert("Please enter correct zip code.");
		thisfield.focus();
		return false;
	}
	
	for (var i=0; i < field.length; i++)
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		
		if (valid.indexOf(temp) == "-1")
		{
			alert("Invalid characters in your zip code. Please try again.");
			thisfield.focus();
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-"))
		{
			alert("The hyphen character should be used with a properly formatted zip code, like '12345-6789'. Please try again.");
			thisfield.focus();
			return false;
	   }
	}
	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For Number Validation ////////////////////
function isNumber(thisfield, msg)
{
	if(isNaN(Trim(thisfield.value))) 
	{
		alert(msg);
		thisfield.focus();
		return false;
	}
	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For Email Validation ////////////////////
function isEmail(thisfield)
{	
	if (Trim(thisfield.value) != "")
	{
		var str=thisfield.value;
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
		testresults=true
		else
		{
			alert("A valid email address is required")
			thisfield.focus();
			return false;
		}		
	}
	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For URL Validation ////////////////////
function isURL(thisfield)
{	
	if (Trim(thisfield.value) != "")
	{
		/*
		var str=thisfield.value;
		//var filter=/^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)/i
		//var filter=^(?#http)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$
		
		if (filter.test(str))
		testresults=true
		else
		{
			alert("Please enter valid URL")
			thisfield.focus();
			return false;
		}
		*/
	}
	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For Match Password ////////////////////
function isEmptyUsername(thisfield)
{
	if (Trim(thisfield.value) == "" || thisfield.value.length < 6)
	{
		alert ("Username is required with minimum 6 characters");
		thisfield.focus();
		return false;
	}
	
	return true;
}

/////////////////////////////////////////////////////////////

//////////////// For Match Password ////////////////////
function isEmptyPassword(thisfield)
{
	if (Trim(thisfield.value) == "" || thisfield.value.length < 6)
	{
		alert ("Password is required with minimum 6 characters");
		thisfield.focus();
		return false;
	}
	
	return true;
}

function matchPassword(thisfield1, thisfield2)
{	
	if (Trim(thisfield1.value) == "" || thisfield1.value.length < 6)
	{
		alert ("Password is required with minimum 6 characters");
		thisfield1.focus();
		return false;
	}
	if (Trim(thisfield2.value) == "" || thisfield2.value.length < 6)
	{
		alert ("Confirm password is required with minimum 6 characters");
		thisfield2.focus();
		return false;
	}
	
	if (thisfield1.value != thisfield2.value)
	{
		alert ("Password is not confirmed");
		thisfield1.focus();
		return false;
	}
	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For Match Email Address ////////////////////
function matchEmail(thisfield1, thisfield2)
{
	if (Trim(thisfield1.value) != Trim(thisfield2.value))
	{
		alert ("Email address is not confirmed, please enter correct email address");
		thisfield1.focus();
		return false;
	}
	
	return true;
}
/////////////////////////////////////////////////////////////


////////////////// For Date Validation //////////////////////

// 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 checkDate(dt)
{	
	if (isDate(dt.value)==false)
	{
		dt.focus()
		return false
	}
    return true
 }
 
/////////////////////////////////////////////////////////////

//////////////// For URL Validation ////////////////////
function fileType(thisfield, types, msg)
{	
	if (Trim(thisfield.value) != "")
	{
		var str=thisfield.value;
		var ext = str.substring(str.indexOf(".") , str.length ); // get the file Extension
		
		var typeArray = types.split(", ");		
		
		var error="1";
		for(i = 0; i < typeArray.length; i++)
		{
			var currType = typeArray[i];
			
			if(ext == currType)
			{
				error = 0;
			}
		}	
		
		if(error.length > 0)
		{
			alert (msg);
			thisfield.focus();
			return false;
		}
		
		return true;
	}	
	return true;
}
/////////////////////////////////////////////////////////////

//////////////// For popup ////////////////////
function popup(URL, width, height, left, top)
{
	OpenWin = this.open(URL, "", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,width="+width+",height="+height+",left="+left+",top="+top+"");
}

//////////////// For Text ////////////////////
function limitText(limitField, limitCount, limitNum)
{
	limitCount = document.getElementById(limitCount);
	if (limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else
	{
		limitCount.innerHTML = limitNum - limitField.value.length;
	}
}
