// JavaScript Document

//trim
String.prototype.trim  = _trim;
function _trim()
{
	return this.replace(/^\s+|\s+$/g, "");
}

//ADDED BY PRAVEEN
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}

/*
function Trim(TRIM_VALUE){

		if(TRIM_VALUE.length < 1){
		return"";
		}
			TRIM_VALUE = RTrim(TRIM_VALUE);
			TRIM_VALUE = LTrim(TRIM_VALUE);
		if(TRIM_VALUE==""){
		return "";
		}
		else{
		
		return TRIM_VALUE;
		}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function
*/

// clear fields function
function Clear(field)
{
	field.value = "";
}


// only number
function validateNumber(Numberfield)
{
		var checkField = Numberfield.value;	
		var checkOK = "0123456789 ";
		var checkStr = checkField;
		var allValid = true;
		var allNum = "";
		for (i = 0;  i < checkStr.length;  i++)
		{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
		allValid = false;
		break;
		}
		if (ch != ",")
		allNum += ch;
		}
		if (!allValid)
		{
		alert("Please enter only numbers in this field.");
		Numberfield.focus();
		return false;
		}
		if (allValid)
		{
		return true;
		}
}


//chk ISBN number

function chkISBN(Numberfield)
{
		var checkField = Numberfield.value;	
		var checkOK = "0123456789 -/";
		var checkStr = checkField;
		var allValid = true;
		var allNum = "";
		for (i = 0;  i < checkStr.length;  i++)
		{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
		allValid = false;
		break;
		}
		if (ch != ",")
		allNum += ch;
		}
		if (!allValid)
		{
		alert("Please enter only numbers in this field.");
		Numberfield.focus();
		return false;
		}
		if (allValid)
		{
		return true;
		}
}


function IsFormatAlphaNum(alphastr)
{	
	// allow ONLY alphanumeric keys, no symbols or punctuation
	// this can be altered for any "checkOK" string you desire
	//chr = chr1;
	//var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.#-";
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,";
	var checkStr = alphastr.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))

	break;
	if (j == checkOK.length)
	{

	allValid = false;
	break;
	}
	}
	if (!allValid)
	{
	alert("Please enter only alphanumeric in this field.");
	alphastr.focus();
	return false;
	}
	else
	{
	return true;
	}
}
function IsAlpha(alphastr)
{	
	// allow ONLY alphanumeric keys, no symbols or punctuation
	// this can be altered for any "checkOK" string you desire
	//chr = chr1;
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
	var checkStr = alphastr.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	}
	if (!allValid)
	{
	alert("Only alphabets are allowed in this field.");
	alphastr.focus();
	return false;
	}
	else
	{
	return true;
	}
}

// EMAIL ID CHECKING
function validateEmailId(EmailId)
{
// Validate email address, must have @ and .

if (EmailId.value!="")
{
var checkEmail = "@.";
var checkStr = EmailId.value;
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
y=0

	for (i = 0;  i < checkStr.length;  i++)
	{
			if (checkStr.indexOf("@")==0 || checkStr.indexOf(".")==0)
			{
				alert("The email field cannot start with an \"@\" or a \".\"");
				EmailId.focus();
				return (false);			
			}
		
			ch = checkStr.charAt(i);		
			for(x=0; x < ch.length; x++)
			{
				if (ch=="@")
				{				
				y = y+1
				
					if (y > 1)
					{
					alert("The email field can contain only single @");
					EmailId.focus();
					return (false);
					}
				}		
				
			}			
			for (j = 0;  j < checkEmail.length;  j++)	
			{
			if (ch == checkEmail.charAt(j) && ch == "@")							
			EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
			EmailPeriod = true;
				  if (EmailAt && EmailPeriod)
					break;
				  if (j == checkEmail.length)
					break;
			}	
			// if both the @ and . were in the string
			if (EmailAt && EmailPeriod)
			{
					EmailValid = true;
					break;
			}
	}
			if (!EmailValid)
			{
			alert("The \"email\" field must contain an \"@\" and a \".\".");
			EmailId.focus();
			return (false);
			}
			else
			{
				return true;
			}
	}
			
}
////////////////
function checkemp(str1,strmes,obj)
{
	if(str1.length == 0)
	{
		alert(strmes);
		if(obj.disabled == false)
			obj.focus();
		return false
	}
}
/*	THESE FUNCTION HAVE BEED ADDED BY PRAVEEN*/

function checklet(str1,str2,strmes,obj)
{
	var strlook;
	strlook = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " + str2;
	var cntr;
	var ch;
	let="";
	cntr=0;
	var retval;
	for(cntr=0;cntr < str1.length;cntr++)
	{
		let = str1.charAt(cntr);	
		if(strlook.indexOf(let) == -1)
		{
			retval = false;
			break;
		}
		else
		{
			retval =true;
		}
	}
	if(retval == false)
	{
		alert(strmes);
		if(obj.disabled == false)
			obj.focus();
	}
	return retval;
}
function IsURL(stringurl)
{
	 
	var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
    
    var matchURL=stringurl.match(Url);
     if(matchURL==null)
     {
               alert("Web URL does not look valid");
           
               return false;
     }

}

	function ValURL(strurl) {
	  
		var v = new RegExp();
		 v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		if (!v.test(strurl)) {
        alert("You must supply a valid URL.");
        return false;
    }
} 
function IsAlphaNum(alphastr)
{		
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,'._-";
	var checkStr = alphastr.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	}
	if (!allValid)
	{
	alert("Only alphanumeric values are allowed in this field.");
	alphastr.focus();
	return false;
	}
	else
	{
	return true;
	}
}
function IsAlphaNumQuest(alphastr)
{		
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,/-? ";
	var checkStr = alphastr.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	
	break;
	if (j == checkOK.length)
	{
	allValid = false;
	break;
	}
	}
	if (!allValid)
	{
	alert(checkStr);
	alert("Only alphanumeric values are allowed in this field.");
	alphastr.focus();
	return false;
	}
	else
	{
	return true;
	}
}
function checknum(str1,str2,strmes,obj)
{
	//alert(str1);
	var strlook;
	strlook = "0123456789" + str2;
				<!--0123456789@._0123456789-->
	var cntr;
	var ch;
	let="";
	cntr=0;
	var retval;
	//alert(str1);
	for(cntr=0;cntr < str1.length;cntr++)
	{
		let = str1.charAt(cntr);
		//alert(str1);	
		if(strlook.indexOf(let) == -1)
		{
			retval = false;
			break;
		}
		else
		{
			retval =true
		}
	}
	if(retval == false)
	{
		alert(strmes);
		if(obj.disabled == false)
			obj.focus();
	}
	return retval;
}

function checklen(str1,strlen,strmes,obj)
{
	if(str1.length > strlen)
	{
		alert(strmes);
		//alert("str1.length");
		//alert("strlen");
		if(obj.disabled == false)
			obj.focus();
		return false
	}
}

			function checkAllowedPictures(inputFileName)
			{
				
				var fileName = inputFileName;
				//alert("File Name: " +fileName + "File Length : " +fileName.length);
				var inputFileNameLength = parseInt(fileName.length) - 4;
				
				var inputFileExtension = fileName.substring(inputFileNameLength, inputFileNameLength+4);
				//alert("File Extension : " +inputFileExtension + inputFileNameLength);
				
				var fileFormats = new Array(".jpg", ".gif", "jpeg");	// ".png", ".tif", ".ppt", ".pps");
				//alert(fileFormats.length);

				for(i = 0; i < fileFormats.length; i++)
				{	
					
					if(inputFileExtension.toLowerCase() == fileFormats[i])
					{
						return true;
					}
				}
				
				//alert(inputFileExtension + fileFormats[0]);
				alert("Only " +fileFormats.toString() + " are allowed");
				return false;
			}
			function checkAllowedDocument(inputFileName)
			{
				var fileName = inputFileName;
				//alert("File Name: " +fileName + "File Length : " +fileName.length);
				var inputFileNameLength = parseInt(fileName.length) - 4;
				//alert("File Name Length : " + inputFileNameLength);

				var inputFileExtension = fileName.substring(inputFileNameLength, inputFileNameLength+4);
				//alert("File Extension : " +inputFileExtension + inputFileNameLength);
				
				var fileFormats = new Array(".pdf", ".doc");	// ".png", ".tif", ".ppt", ".pps");
				//alert(fileFormats.length);

				for(i = 0; i < fileFormats.length; i++)
				{
					//alert(inputFileExtension + fileFormats[i]);
					if(inputFileExtension.toLowerCase() == fileFormats[i])
					{
						return true;
					}
				}
				
				//alert(inputFileExtension + fileFormats[0]);
				alert("Only " +fileFormats.toString() + " are allowed");
				return false;
			}
function emailCheck (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("Please enter proper Email Id");
					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("Please enter proper Email Id");
						return false;
					}
				}
				
				for (i=0; i<domain.length; i++)
				{
					if (domain.charCodeAt(i)>127)
					{
						//alert("Please enter proper Email Id");
						return false;
					}
				}

				// See if "user" is valid 

				if (user.match(userPat)==null)
				{
					// user is not valid
					//alert("Please enter proper Email Id");
					return false;
				}

				var IPArray=domain.match(ipDomainPat);
				if (IPArray!=null)
				{
					for (var i=1;i<=4;i++)
					{
						if (IPArray[i]>255)
						{
							//alert("Please enter proper Email Id");
							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("Please enter proper Email Id");
					return false;
				}
			}

			if (checkTLD && domArr[domArr.length-1].length!=2 && 
				domArr[domArr.length-1].search(knownDomsPat)==-1)
				{
					//alert("Please enter proper Email Id");
					return false;
				}

			if (len<2)
			{
				//alert("Please enter proper Email Id");
				return false;
			}
			
			if (len>3)
			{
				//alert("Please enter proper Email Id");
				return false;
			}

			return true;
		}