// JavaScript Document
function init()
{
	document.getElementById("make").disabled=true;		
	document.getElementById("model").disabled=true;		
	document.getElementById("location").disabled=true;		
}

function new_used_car(str)
{
	if(str=="0")
	{
		document.form1.model.disabled=true;		
document.form1.make.disabled=true;		
		document.form1.location.disabled=true;		

}else{
		document.getElementById("make").disabled=false;		
		document.getElementById("location").disabled=false;	
		
	}
}

var xmlHttp

function show_subcategory(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="get_model.php";
url=url+"?sid="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("divsubcategory").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function isNumber(str){
if(str.length==0)
{
	return false;
}
numdecs = 0;
for (i = 0; i < str.length; i++)
{
	mychar = str.charAt(i);
	if ((mychar >= "0" && mychar <= "9") || mychar == "." )
	{
		if (mychar == ".")
		numdecs++;
	}
	else 
	return false;
}
if (numdecs > 1){
		return false;
	}
return true;
}// end isNumber function


function validate_car()
{
	var dm = document.frmcar;
	var strError="";
	if(dm.make.value=="0")
		strError+="\nMake";
	if(dm.location.value=="0")
		strError+="\nLocation";

	if(!isNumber(dm.txtamount.value))
	{
		strError+="\nValid amount";
	} 
	if(strError!="")
	{
		alert("Please check the missing field(s)\n=================="+strError);	
		return false;
	}
}
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function chkvalid()
{
	var dm=document.form1;
	var strError="";
	if(dm.fname.value=="")
	strError+="\nName";
	if(dm.address.value=="")
	strError+="\nAddress";
	if ((echeck(dm.email_address.value)==false)||(dm.email_address.value==""))	
	strError+="\nEmail";
	if(dm.mobile.value=="")
	strError+="\nMobile";
  	if(dm.makemodel.value=="")
	strError+="\nCar Make and Model";
	if(dm.datepicker.value=="")
	strError+="\nDate";
	if(dm.time.value=="")
	strError+="\nTime";
	if(strError!="")
	{
		alert("Please check the missing field(s)\n============="+strError);
		return false;
	}
}

function chkvalidADMIN()
{
	var dm=document.form1;
	var strError="";
	if(dm.fname.value=="")
	strError+="\nName";
	if(dm.address.value=="")
	strError+="\nAddress";
	if ((echeck(dm.email_address.value)==false)||(dm.email_address.value==""))	
	strError+="\nEmail";
	if(dm.mobile.value=="")
	strError+="\nMobile";
	if(dm.Aname.value=="")
	strError+="\nAdmin Contact Person";
	if(dm.Anumber.value=="")
	strError+="\nContact Number";
 	if(dm.datepicker.value=="")
	strError+="\nDate";
	if(dm.time.value=="")
	strError+="\nTime";
	if(strError!="")
	{
		alert("Please check the missing field(s)\n============="+strError);
		return false;
	}
}

function chkvalidRWA()
{
	var dm=document.form1;
	var strError="";
	if(dm.fname.value=="")
	strError+="\nName";
	if(dm.address.value=="")
	strError+="\nAddress";
	if ((echeck(dm.email_address.value)==false)||(dm.email_address.value==""))	
	strError+="\nEmail";
	if(dm.mobile.value=="")
	strError+="\nMobile";
	if(dm.Rname.value=="")
	strError+="\nRWA Contact Person";
	if(dm.Rnumber.value=="")
	strError+="\nContact Number";
 	if(dm.datepicker.value=="")
	strError+="\nDate";
	if(dm.time.value=="")
	strError+="\nTime";
	if(strError!="")
	{
		alert("Please check the missing field(s)\n============="+strError);
		return false;
	}
}	
function chklogin()
{
	var dm=document.frm;
	var strError="";
	/*if(dm.uname.value=="");
	strError+="\nUsername";
	if(dm.pwd.value=="");
	strError+="\nPassword";
*/
	if(strError!="")
	{
		alert("Please check the missing field(s)\n============="+strError);
		return false;
	}
}

function testPassword(passwd)
{
var description = new Array();
description[0] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=15 width=30 bgcolor=#ff0000></td><td height=15 width=120 bgcolor=#dddddd></td></tr></table></td><td class=normal>Weakest</td></tr></table>";
description[1] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=15 width=60 bgcolor=#bb0000></td><td height=15 width=90 bgcolor=#dddddd></td></tr></table></td><td class=normal>Weak</td></tr></table>";
description[2] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=15 width=90 bgcolor=#ff9900></td><td height=15 width=60 bgcolor=#dddddd></td></tr></table></td><td class=normal>Medium</td></tr></table>";
description[3] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=15 width=120 bgcolor=#00bb00></td><td height=15 width=30 bgcolor=#dddddd></td></tr></table></td><td class=normal>Strong</td></tr></table>";
description[4] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=15 width=150 bgcolor=#00ee00></td></tr></table></td><td class=normal>Strongest</td></tr></table>";
description[5] = "<table border=0 cellpadding=0 cellspacing=0><tr><td><table cellpadding=0 cellspacing=2><tr><td height=15 width=150 bgcolor=#dddddd></td></tr></table></td><td class=normal>Begin Typing</td></tr></table>";

		var intScore   = 0
		var strVerdict = 0
		
		// PASSWORD LENGTH
		if (passwd.length==0 || !passwd.length)                         // length 0
		{
			intScore = -1
		}
		else if (passwd.length>0 && passwd.length<5) // length between 1 and 4
		{
			intScore = (intScore+3)
		}
		else if (passwd.length>4 && passwd.length<8) // length between 5 and 7
		{
			intScore = (intScore+6)
		}
		else if (passwd.length>7 && passwd.length<12)// length between 8 and 15
		{
			intScore = (intScore+12)
		}
		else if (passwd.length>11)                    // length 16 or more
		{
			intScore = (intScore+18)
		}
		
		
		// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
		if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
		{
			intScore = (intScore+1)
		}
		
		if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
		{
			intScore = (intScore+5)
		}
		
		// NUMBERS
		if (passwd.match(/\d+/))                                 // [verified] at least one number
		{
			intScore = (intScore+5)
		}
		
		if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
		{
			intScore = (intScore+5)
		}
		
		
		// SPECIAL CHAR
		if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
		{
			intScore = (intScore+5)
		}
		
																 // [verified] at least two special characters
		if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
		{
			intScore = (intScore+5)
		}
	
		
		// COMBOS
		if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
		{
			intScore = (intScore+2)
		}

		if (passwd.match(/(\d.*\D)|(\D.*\d)/))                    // [FAILED] both letters and numbers, almost works because an additional character is required
		{
			intScore = (intScore+2)
		}
 
																  // [verified] letters, numbers, and special characters
		if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
		{
			intScore = (intScore+2)
		}
	
	
		if(intScore == -1)
		{
		   strVerdict = description[5];
		}
		else if(intScore > -1 && intScore < 16)
		{
		   strVerdict = description[0];
		}
		else if (intScore > 15 && intScore < 25)
		{
		   strVerdict = description[1];
		}
		else if (intScore > 24 && intScore < 35)
		{
		   strVerdict = description[2];
		}
		else if (intScore > 34 && intScore < 45)
		{
		   strVerdict = description[3];
		}
		else
		{
		   strVerdict = description[4];
		}
	
	document.getElementById("Words").innerHTML=(strVerdict);
	
}

function validate_form()
{
	var dm = document.frmexpenses;
	var strError="";
	if(dm.oldpwd.value=="")
	strError+="\nOld Password";
	if(dm.newpwd.value=="")
	strError+="\nNew Password";
	if(dm.newcpwd.value=="")
	strError+="\nConfirm Password";
	if(!dm.newpwd.value==dm.newcpwd.value)
	strError+="\nPassword not matched";
	if(strError!="")
	{
		alert("Please check the missing field(s)\n==============="+strError);
		return false;
	}
	
}