/*
List of functions
	> Trim
		Purpose: Remove the leading and trailing space.
		
	> ValidInteger
		Purpose: Onkey press accept only 0123456789

	> ValidFloat
		Purpose: Onkey press accept only .0123456789

	> ValidPhone
		Purpose: Onkey press accept only 1234567890-()+

	> digit
		Purpose: At the time of submit Check for all this characters: 0123456789

	> alphachar
		Purpose: At the time of submit Check for all this characters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

	> phonenumber
		Purpose: At the time of submit Check for all this characters: 1234567890-()+

	> formatAsMoney
		Purpose:	At the time of onblur event return amount value with two digit floating point
					E.g.
						12.999 replace with 13.00
						12.994	replace with 12.99

	> countInstances
		> alphanum
		> alpha
		> smallLetter
		> capLetter
		> symbol
		> checkAll
		> getCheckCount
		> isValidEmail
		> get_referer
		> Add_As_Bookmark
		> todayDate
		> getMonthName
		> getCookie
		> setCookie
		> makeNewWindow
		> isValidUrl
*/

function Trim(s) 
{
  // Remove leading spaces and carriage returns
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }
  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

//********************* START of function for email-id validation  ****************************//
function isValidEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters in e-mail address.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters in e-mail address.");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("The username doesn't seem to be valid in e-mail address.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
	   		}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid in e-mail address.");
			return false;
	   }	
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	if (len<2) {
		alert("This e-mail address is missing a hostname!");
		return false;
	}	
	return true;
}

function countInstances(string, word) {
  var substrings = string.split(word);
  return substrings.length - 1;
}

function isValidUrl(urlval)
{
	var y = countInstances(urlval, "http://");
	var z = countInstances(urlval, "https://");
	var p = countInstances(urlval, "ftp://");
	if(y==0 && z==0 && p==0)
	{
		alert("Please Enter \" http:// \" , \" https:// \"  or \" ftp:// \" in your URL");
		return false;
	}
	var x = countInstances(urlval, ".")
	if(x<1) 
	{
		alert("Please Enter the Proper URL");
		return false;
	}
	return true;
}
//********************* END of function for email-id validation  ****************************//

function usernameValidation(value,length)
{
	chk1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.@";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function datedifferance(date1,date2)
{
	if(date1 == "")
	{
		var currentTime = new Date()
		var month = currentTime.getMonth() + 1
		var day = currentTime.getDate()
		var year = currentTime.getFullYear()
		var strDate1 = year + "/" + month + "/" + day;
	}
	else { var strDate1 = date1; }
	var strDate2 = date2;
	var difference = ((Date.parse(strDate2))-(Date.parse(strDate1)))/(24*60*60*1000).toFixed(0);
	return difference;
}

function datevalidator(year,month,day)
{
	if (month == 2) {
		if ((year % 4) == 0) { if (day > 29) { return 0; } }
		else { if (day > 28) { return 0; } }
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11)	{ if (day > 30) { return 0; } }
	else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day > 31) { return 0; } }
	else { return 1; }
}

function checknum(value,length)
{
	chk1="1234567890";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checknumhyphen(value,length) // Normally used for phone number validaiton
{
	chk1="1234567890- +";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checkalnumdashdot(value,length)
{
	chk1="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-@ ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checkname(value,length)
{
	chk1="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ() .";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
} 

function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else
		countfield.value = maxlimit - field.value.length;
}