// JAvaScript Document
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validalphabet(e) 
	{
	var mykey = window.event ? e.keyCode : e.which; 
	
		if (mykey==46 || mykey==126) 
		{ 
			return false; 
		}
		
		if (((mykey > 32 && mykey < 48) || (mykey > 57 && mykey < 65) || (mykey > 90 && mykey < 97) || (mykey > 45 && mykey <= 57)))
		{ 
			return false; 
		}
	return true;
	}
	function validatenumber(e) 
    {
var myKey = window.event ? e.keyCode : e.which;

if(
(myKey > 47 && myKey < 58) || //48 To 57 = 0-9
myKey == 8 || //8 = BackSpace
myKey == 0 ||//0 = Delete
myKey == 13 //13 = Enter
) {
return true;
}
return false;
}



function validalphanumeric(e) 
	{
	var mykey = window.event ? e.keyCode : e.which; 
	
		if (mykey==46 || mykey==126) 
		{ 
			return false; 
		}
		
		if (((mykey > 32 && mykey < 48) || (mykey > 57 && mykey < 65) || (mykey > 90 && mykey < 97) || (mykey > 122 && mykey <= 127)))
		{ 
			return false; 
		}
	return true;
}

function validalphanumeric1(e) 
	{
	var mykey = window.event ? e.keyCode : e.which; 
	
		if (mykey==46 || mykey==126) 
		{ 
			return false; 
		}
		
		if (((mykey > 32 && mykey < 48) || (mykey > 57 && mykey < 65) || (mykey > 90 && mykey < 97) || (mykey > 45 && mykey <= 57)))
		{ 
			return false; 
		}
		
		if ((mykey <= 47 || mykey > 57) && mykey !=13 && mykey !=0 && mykey != 8) 	
	    { 	
			return false; 
		} 	
		
	return true;
}

function removeLeadingAndTrailingChar(inputString)
{
	var removeChar = " ";
	var returnString = inputString;
	if (removeChar.length)
   		{
		  while(''+returnString.charAt(0)==removeChar)
			{
			  returnString=returnString.substring(1,returnString.length);
			}
    	  while(''+returnString.charAt(returnString.length-1)==removeChar)
	 	    {
	  	      returnString=returnString.substring(0,returnString.length-1);
			} 
		}
		return returnString;
}

function validemail(mailStr)
{
	var matchStr=mailStr;
 	var isValid = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(matchStr));

	if(isValid) 
	{
 		return true;
	}
	else
	{
		return false;
	}
} 
function tabvalidatePhone(phfield1,phfield2)
{
	if(document.getElementById(phfield1).value.length==3) 
		document.getElementById(phfield2).focus();
}
function validAddUser(theform)
{   
        var Name = theform.name.value
		var Designation = theform.designation.value
		var Userid = theform.userid.value
		var Password = theform.password.value
		
		Name=removeLeadingAndTrailingChar(Name);
		if (Name == "")
		{ 
			alert("Please Enter Name");			
			theform.name.focus();
			return false;
		}
		Designation=removeLeadingAndTrailingChar(Designation);
		
		Userid=removeLeadingAndTrailingChar(Userid);
		
		Password=removeLeadingAndTrailingChar(Password);
		if (Password=="")
		{ 
		    alert("Please Enter Password");
			theform.password.focus();
			return false;		
		}
 }

function validateUserManagement(theform)
{   
        var Name = theform.name.value;
		var Designation = theform.designation.value;
		var Userid = theform.userid.value;
		var Password = theform.password.value;
		
		Name=removeLeadingAndTrailingChar(Name);
		if (Name == "")
		{ 
			alert("Please Enter Name");			
			theform.name.focus();
			return false;
		}
		Designation=removeLeadingAndTrailingChar(Designation);
		
		Userid=removeLeadingAndTrailingChar(Userid);
		if (Userid == "")
		{ 
			alert("Please Enter UserId");			
			theform.userid.focus();
			return false;
		}
		Password=removeLeadingAndTrailingChar(Password);
		if (Password=="")
		{ 
		    alert("Please Enter Password");
			theform.password.focus();
			return false;		
		}
		return true;
 }
function checkLength(obj)
{
	var limit=300;
	if(obj.value.length >=limit)
	{
		obj.value=obj.value.substring(0,limit-1);
		alert("Enter upto 300 characters only");
		return false;
	}
	return true;
}
function removeLeadingAndTrailingChar(inputString)
{	alert("test");
	var removeChar = " ";
	var returnString = inputString;
	if (removeChar.length)
   		{
		  while(''+returnString.charAt(0)==removeChar)
			{
			  returnString=returnString.substring(1,returnString.length);
			}
    	  while(''+returnString.charAt(returnString.length-1)==removeChar)
	 	    {
	  	      returnString=returnString.substring(0,returnString.length-1);
			} 
		}
		return returnString;
}
function validate(theform)
{   	
		obj = document.f1;
		theform.FirstName.value=removeLeadingAndTrailingChar(theform.FirstName.value);
		theform.LastName.value=removeLeadingAndTrailingChar(theform.LastName.value);
		theform.Email.value=removeLeadingAndTrailingChar(theform.Email.value);
		theform.OffPhone1.value=removeLeadingAndTrailingChar(theform.OffPhone1.value);
		theform.OffPhone2.value=removeLeadingAndTrailingChar(theform.OffPhone2.value);
		theform.OffPhone3.value=removeLeadingAndTrailingChar(theform.OffPhone3.value);
        var FirstName =theform.FirstName.value;
		var LastName = theform.LastName.value;
		//var Address = theform.Address.value;
		var Email = theform.Email.value;
		//var Zip = theform.Zip.value;
		var ph1= theform.OffPhone1.value;
		var ph2= theform.OffPhone2.value;
		var ph3= theform.OffPhone3.value;
		//var comments=theform.Comments.value;
		
		theform.OffPhone.value="";
		if (FirstName == "")
		{ 
			alert("Please Enter First Name");			
			theform.FirstName.focus();
			return false;
		}
		if (LastName == "")
		{ 
			alert("Please Enter Last Name");			
			theform.LastName.focus();
			return false;
		}
		if (Email == "")
		{ 
			alert("Please Enter Email");			
			theform.Email.focus();
			return false;
		}
		if (Email!="" && !validemail(theform.Email.value))
		{ 
		    alert("Please Enter Valid E-mail Id");
			theform.Email.focus();
			return false;		
		}
		if(ph1.length>=1 && ph1.length<3) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone1.focus();
			return false;
		}
		if(ph2.length>=1 && ph2.length<3) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone2.focus();
			return false;
		}
		if(ph3.length>=1 && ph3.length<4) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone3.focus();
			return false;
		}
        if(ph1.length==3 && ph2.length==3 && ph3.length==4) {
		theform.OffPhone.value="("+ph1+") "+ph2+"-"+ph3;
		}		
}
function validateNew(theform)
{   	
		obj = document.f1;
		theform.FirstName.value=removeLeadingAndTrailingChar(theform.FirstName.value);
		theform.LastName.value=removeLeadingAndTrailingChar(theform.LastName.value);
		theform.Email.value=removeLeadingAndTrailingChar(theform.Email.value);
		theform.OffPhone1.value=removeLeadingAndTrailingChar(theform.OffPhone1.value);
		theform.OffPhone2.value=removeLeadingAndTrailingChar(theform.OffPhone2.value);
		theform.OffPhone3.value=removeLeadingAndTrailingChar(theform.OffPhone3.value);
        var FirstName =theform.FirstName.value;
		var LastName = theform.LastName.value;
		//var Address = theform.Address.value;
		var Email = theform.Email.value;
		//var Zip = theform.Zip.value;
		var ph1= theform.OffPhone1.value;
		var ph2= theform.OffPhone2.value;
		var ph3= theform.OffPhone3.value;
		//var comments=theform.Comments.value;
		
		theform.OffPhone.value="";
		if (FirstName == "")
		{ 
			alert("Please Enter First Name");			
			theform.FirstName.focus();
			return false;
		}
		if (LastName == "")
		{ 
			alert("Please Enter Last Name");			
			theform.LastName.focus();
			return false;
		}
		if (Email == "")
		{ 
			alert("Please Enter Email");			
			theform.Email.focus();
			return false;
		}
		if (Email!="" && !validemail(theform.Email.value))
		{ 
		    alert("Please Enter Valid E-mail Id");
			theform.Email.focus();
			return false;		
		}
		if(ph1.length>=1 && ph1.length<3) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone1.focus();
			return false;
		}
		if(ph2.length>=1 && ph2.length<3) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone2.focus();
			return false;
		}
		if(ph3.length>=1 && ph3.length<4) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone3.focus();
			return false;
		}
        if(ph1.length==3 && ph2.length==3 && ph3.length==4) {
		theform.OffPhone.value="("+ph1+") "+ph2+"-"+ph3;
		}		
}
function validateCustNew(theform)
{   	
		obj = document.f1;
		theform.FirstName.value=removeLeadingAndTrailingChar(theform.FirstName.value);
		theform.LastName.value=removeLeadingAndTrailingChar(theform.LastName.value);
		theform.Email.value=removeLeadingAndTrailingChar(theform.Email.value);
		theform.OffPhone1.value=removeLeadingAndTrailingChar(theform.OffPhone1.value);
		theform.OffPhone2.value=removeLeadingAndTrailingChar(theform.OffPhone2.value);
		theform.OffPhone3.value=removeLeadingAndTrailingChar(theform.OffPhone3.value);
        var FirstName =theform.FirstName.value;
		var LastName = theform.LastName.value;
		//var Address = theform.Address.value;
		var Email = theform.Email.value;
		//var Zip = theform.Zip.value;
		var ph1= theform.OffPhone1.value;
		var ph2= theform.OffPhone2.value;
		var ph3= theform.OffPhone3.value;
		var streetMandatory=theform.StreetMandatory.value;
		if(streetMandatory =="YES")
		theform.Street.value=removeLeadingAndTrailingChar(theform.Street.value);		
		var zipMandatory=theform.ZipMandatory.value;
		if(zipMandatory =="YES")
		theform.Zip.value=removeLeadingAndTrailingChar(theform.Zip.value);		
		var modelMandatory=theform.ModelMandatory.value;
		if(modelMandatory =="YES")
		theform.Model.value=removeLeadingAndTrailingChar(theform.Model.value);
		var trimMandatory=theform.TrimMandatory.value;
		if(trimMandatory =="YES")
		theform.Trim.value=removeLeadingAndTrailingChar(theform.Trim.value);
		var commentsMandatory=theform.CommentsMandatory.value;
		//var comments=theform.Comments.value;
		
		theform.OffPhone.value="";
		if (FirstName == "")
		{ 
			alert("Please Enter First Name");			
			theform.FirstName.focus();
			return false;
		}
		if (LastName == "")
		{ 
			alert("Please Enter Last Name");			
			theform.LastName.focus();
			return false;
		}
		if (Email == "")
		{ 
			alert("Please Enter Email");			
			theform.Email.focus();
			return false;
		}
		if (Email!="" && !validemail(theform.Email.value))
		{ 
		    alert("Please Enter Valid E-mail Id");
			theform.Email.focus();
			return false;		
		}
		if(ph1.length>=1 && ph1.length<3) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone1.focus();
			return false;
		}
		if(ph2.length>=1 && ph2.length<3) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone2.focus();
			return false;
		}
		if(ph3.length>=1 && ph3.length<4) {
			alert("Please Enter Valid Phone");			
			theform.OffPhone3.focus();
			return false;
		}
        if(ph1.length==3 && ph2.length==3 && ph3.length==4) {
		theform.OffPhone.value="("+ph1+") "+ph2+"-"+ph3;
		}
		if(streetMandatory=="YES"){
			if (theform.Street.value == "")
			{ 
				alert("Please Enter Address");			
				theform.Street.focus();
				return false;
			}
		}
		if(zipMandatory=="YES"){
			if (theform.Zip.value == "" || theform.Zip.value.length<5)
			{ 
				alert("Please Enter ZIP code");			
				theform.Zip.focus();
				return false;
			}
		}
		if(modelMandatory=="YES"){
			if (theform.Model.value == "")
			{ 
				alert("Please Enter Model Name");			
				theform.Model.focus();
				return false;
			}
		}
		if(trimMandatory=="YES"){
			if (theform.Trim.value == "")
			{ 
				alert("Please Enter Trim Name");			
				theform.Trim.focus();
				return false;
			}
		}
		if(commentsMandatory=="YES"){
			if (obj.Comments.value == "")
			{ 
				alert("Please Enter Comments");			
				theform.Comments.focus();
				return false;
			}
		}
		
}

function imposeMaxLength(theForm, count)
{
var comments = document.getElementById('Comments').value;
if(comments.length > count)
{
alert("Comments should not exceed "+count+" characters");
document.getElementById('Comments').focus();
return false;
}
return true;
}
function removeLeadingAndTrailingChar(inputString)
{
	var removeChar = " ";
	var returnString = inputString;
	if (removeChar.length)
   		{
		  while(''+returnString.charAt(0)==removeChar)
			{
			  returnString=returnString.substring(1,returnString.length);
			}
    	  while(''+returnString.charAt(returnString.length-1)==removeChar)
	 	    {
	  	      returnString=returnString.substring(0,returnString.length-1);
			} 
		}
		return returnString;
}
