// JavaScript Document


// Script for confirm user before delete.
function calldel(m)
{
	if(!confirm(m))
	{
		return false;
	}
	else
	{
		return true;
	}
}



// Validate Add Dealer page
function dealer_validate()
{
     var  company= document.form_app.company.value;
     if ( company== "" )
     { 
         alert ("Please enter the Company Name");
         document.form_app.company.focus();
         return false;
     }
     var  owner= document.form_app.owner.value;
     if ( owner== "" )
     { 
         alert ("Please enter the Owner Name");
         document.form_app.owner.focus();
         return false;
     }     

     var  sales_mgr= document.form_app.sales_mgr.value;
     if ( sales_mgr== "" )
     { 
         alert ("Please enter the Sales Manager name");
         document.form_app.sales_mgr.focus();
         return false;
     }        

     var  state= document.form_app.state.value;
     if ( state== "" )
     { 
         alert ("Please select the state");
         document.form_app.state.focus();
         return false;
     } 
     
     var  zip= document.form_app.zip.value;
     if ( zip== "" )
     { 
         alert ("Please enter the Zip");
         document.form_app.zip.focus();
         return false;
     }      
     
     var  permitnumber= document.form_app.permitnumber.value;
     if(isNaN(permitnumber))
     { 
         alert ("'Resell Permit Number' must be numeric");
         document.form_app.permitnumber.focus();
         return false;
     }      
     
     var  p_unit= document.form_app.p_unit.value;
     if(isNaN(p_unit))
     { 
         alert ("'Cost per units' must be numeric");
         document.form_app.p_unit.focus();
         return false;
     } 
     
     var  p_month= document.form_app.p_month.value;
     if(isNaN(p_month))
     { 
         alert ("'Number of units sold per month' must be numeric");
         document.form_app.p_month.focus();
         return false;
     } 
     
     var  showstaff= document.form_app.showstaff.value;
     if(isNaN(showstaff))
     { 
         alert ("'Number of staff' must be numeric");
         document.form_app.showstaff.focus();
         return false;
     }      
     
     var txtEmail= document.form_app.email.value;
     if (txtEmail== "" )
     { 
         alert ("Please enter the Email Address");
         document.form_app.email.focus();
         return false;
     }
     if (!validateEmail(document.form_app.email.value,1,1)) 
     {
     document.form_app.email.focus();
     return false;
     }     
     if (txtEmail != document.form_app.confirmemail.value)
     { 
         alert ("Email Address should be same");
         document.form_app.confirmemail.focus();
         return false;
     }
}

function changepass_validate()
{
     var txtEmail= document.form_login.userid.value;
     if (txtEmail== "" )
     { 
         alert ("Please enter the Email Address");
         document.form_login.userid.focus();
         return false;
     }
     if (!validateEmail(document.form_login.userid.value,1,1)) 
     {
     document.form_login.userid.focus();
     return false;
     } 
     
     var password= document.form_login.password.value;
     if (password== "" )
     { 
         alert ("Please enter the Password");
         document.form_login.password.focus();
         return false;
     }

     var newPassword= document.form_login.newPassword.value;
     if (newPassword== "" )
     { 
         alert ("Please enter the new Password");
         document.form_login.newPassword.focus();
         return false;
     }
     
     var confirmPassword= document.form_login.confirmPassword.value;
     if (confirmPassword== "" )
     { 
         alert ("Please enter the confirm password");
         document.form_login.confirmPassword.focus();
         return false;
     }          
     
     if (newPassword    != confirmPassword)
     { 
         alert ("New password and confirm password should be same");
         document.form_login.newPassword.focus();
         return false;
     }     
     
}

// Valid Email Script
function validateEmail(addr,man,db) {
	if (addr == '' && man) {
	   if (db) alert('Email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('Email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("Email address contains non ascii characters.");
		  return false;
	   }
	}
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('Email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('Email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('Email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('Email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
return true;
}
