// This function checks if the username field
// is at least 6 characters long.
// If so, it attaches class="welldone" to the 
// containing fieldset.
var carNum = 0;
var carsAdded=0;
var carMax=20;
var totalPrice=0;
var year_ini=1900;
var ClickSubmit = 0;
var today = new Date;  
var year_end=today.getFullYear()+1;
var year_mid=parseInt(((year_end-year_ini)/2).toFixed(0),10);
var carsInput='';
var carsValues=new Array();

var fieldsToValidate;
var fieldsToValidate2;
var fieldsToValidate3;


function fillArray() {
 	fieldsToValidate = document.getElementById('requiredFields').value.split(",");
	fieldsToValidate2 = document.getElementById('requiredFields').value.split(",");
} 

function fillDates(){
	var optionsStr='';
	for(i=year_end; i>=year_ini; i--){
		if (i==today.getFullYear()+1){
		optionsStr+='<option selected="selected" value="">Select a Year</option>';
		optionsStr+='<option  value="'+i+'">'+i+'</option>';	
			}else{		
		optionsStr+='<option value="'+i+'">'+i+'</option>';
			}
	}
	$('#year').html(optionsStr);
}

function isOther(element,val){
	if((val=='490' && element=='model') ||(val=='34' && element=='make')){
		document.getElementById(element+'other').value ='';
		document.getElementById(element+'other').style.display='inline-block';
	}else{
		document.getElementById(element+'other').style.display='none';
	}
}

function validateUsername(inputString,element,msg_empty,msg_used,msg_symbol,current) {
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(!document.getElementById(element).readOnly){
		if(inputString.length == 0) {
				// Is empty
				document.getElementById('loader').style.visibility='hidden';
				notValid2(element,msg_empty);
		} else {
			if(inputString.match(alphaExp)){
				document.getElementById('loader').style.visibility='visible';
				$.post("includes/existingUser.php", {queryString: ""+inputString+""}, function(data){
				// Is valid	
				isValid2(element)
				document.getElementById('loader').style.visibility='hidden';
				if(data.length >0 && data!=current) {
					// Is already taken
					notValid2(element,data+' '+msg_used);
				}});
			}else{
				notValid2(element,msg_symbol);
			}
		}
	}
}

function validateUsername_admin(inputString,element,msg_empty,msg_used,msg_symbol,current,origen) {
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(!document.getElementById(element).readOnly){
		if(inputString.length == 0) {
				// Is empty
				document.getElementById('loader').style.visibility='hidden';
				notValid_admin(element,msg_empty);
		} else {
			if(inputString.match(alphaExp)){
				document.getElementById('loader').style.visibility='visible';
				$.post("../includes/existingUser.php", {origen:origen, queryString: ""+inputString+""}, function(data){
				// Is valid	
				isValid2(element)
				document.getElementById('loader').style.visibility='hidden';
				if(data.length >0 && data!=current) {
					// Is already taken
					notValid_admin(element,data+' '+msg_used);
				}});
			}else{
				notValid_admin(element,msg_symbol);
			}
		}
	}
}

function fillMake(selPlease) {
	
				$.post("includes/car_makes.php", {queryString: " "}, function(data){
				try{

				$('#make').html('<option disabled="true" selected="true">'+selPlease+'</option>'+data);
				
				}catch(e){}
				});
}

function fillMake_admin(selPlease) {
	
				$.post("includes/car_makes.php", {queryString: " "}, function(data){
				$('#make').html('<option value="" disabled=true selected=true>'+selPlease+'</option>'+data);
				
				$('#make2').html(data);
				document.getElementById('make2').selectedIndex=0;
				fillModel_admin(document.getElementById('make2').value,'','');
				});
}

function isOther_admin(element,val){
	if(val=='34' && element=='make'){
		document.getElementById(element+'other').value ='';
		document.getElementById(element+'other').style.display='inline-block';
	}else{
		document.getElementById(element+'other').style.display='none';
	}
}

function fillModel(model,selPlease,other) {
	
				$.post("includes/car_models.php", {queryString: ""+model+""}, function(data){
					if(model=='34'){
						$('#model').html(data);
						setID('modelName',490);
						isOther('model',490);
					}else{
						$('#model').html('<option disabled=true selected=true>'+selPlease+'</option>'+data+'<option id=\'modelName490\' value=\'490\'>'+other+'</option>');
						document.getElementById('model').selectedIndex=0;
						isOther('model',model);
					}
				});
				isOther('make',model);
				
}

function fillModel_admin(model,selPlease,other) {
	
				$.post("includes/car_models_modify.php", {queryString: ""+model+""}, function(data){
					
						$('#model').html(data);
						document.getElementById('model').selectedIndex=0;
				});
			
				//isOther('make',model);
				
}

function fillRegions(msgAll) {
	
				
				$.post("includes/getRegions.php", {}, function(data){
				$('#reg_p').html('<option selected=true value="">'+msgAll+'</option>'+data);
				$('#reg_d').html('<option selected=true value="">'+msgAll+'</option>'+data);
				});
				try{
					document.getElementById('reg_p').selectedIndex = 0;
					document.getElementById('reg_d').selectedIndex = 0;
					}catch(e){
					return false;
					}
				
}

function fillRegions_admin(msgAll) {
	
				$.post("includes/getRegions_db.php", {}, function(data){
				$('#region').html('<option selected=true value="">'+msgAll+'</option>'+data);
				});
				
				$.post("includes/getRegions_db_modify.php", {}, function(data){
				
				$('#region2').html(data);
				document.getElementById('region2').selectedIndex = 0;
				fillCities_admin(document.getElementById('region2').value);
				});
				
				
}

function fillCities_admin(region) {
				$.post("includes/getCities_db_modify.php", {queryString:""+region+""}, function(data){
				
				$('#city2').html(data);
				document.getElementById('city2').selectedIndex = 0;
				});
				
				
}

function addToDB(db,value1,value2, value3, value4) {
	var valid=true;
	var msg = 'Are you sure the information is correct?';
	switch(db){
		case 'cars':
			if(value1==''){
				valid=false;
			}else if(value1==34){ //selected OTHER
				if(value3==''){
					valid=false;
				}
			}
			if(value2==''){
				valid=false;
			}
			
			break;
		case 'cities':
			if(value1=='' || value2==''){
				valid=false;
			}
			break;
		case 'cars_modify':
			if(value2=='' && value4==''){
				valid=false;
			}
			break;
		case 'cities_modify':
			if(value4==''){
				valid=false;
			}
			break;
			
	}
	
	if(valid){
		if(confirm(msg)){
			$.post("includes/addToDB.php", {db:db, val1:value1, val2:value2, val3:value3, val4:value4 }, function(data){
				if(data=='true'){
					$('#'+db+'Fb').html('<img src="../images/dbOK.gif" style="float:inherit; margin-left:0px; margin-bottom:-10px;" />');
					if(value1==34){
						
						document.getElementById('makeother').value='';
						document.getElementById('makeother').style.display='none';
					}
					
					
					if(db=='cars'){
						document.getElementById('modelother').value='';
					}else if(db=='cities'){
						document.getElementById('city').value='';
					}else if(db=='cars_modify'){
						document.getElementById('make_change').value='';
						document.getElementById('model_change').value='';
						fillMake_admin('Please select one');
						fillModel_admin(document.getElementById('make2').value,'','');
					}else if(db=='cities_modify'){
						document.getElementById('city_change').value='';
						fillCities_admin(document.getElementById('region2').value);
					}
				}else if(data=='exists'){
					$('#'+db+'Fb').html('<img src="../images/exist.gif" style="float:inherit; margin-left:0px; margin-bottom:-10px;" />');
					alert('This its already on the DB');
				}else{
					$('#'+db+'Fb').html('<img src="../images/dbNO.gif" style="float:inherit; margin-left:0px; margin-bottom:-10px;" />');
				}
			});
		}
	}else{
		alert('Please be sure there are no Empty fields');
	}
	
}



function setID(element,val){
	document.getElementById(element).value=$('#'+element+val).html();
}





function autoFill(value,user,camps,block) {

	if(value){ //If its checked it will fill out the form 
		document.getElementById(block).disabled=true;
		$.post("includes/autofill_info.php", {queryString: ""+user+""}, function(data){													
			fields = data.split("|");
			document.getElementById('contact'+camps).value=fields[0]; 
			document.getElementById('phone'+camps).value=fields[1]; 
			document.getElementById('mobile'+camps).value=fields[2]; 
			document.getElementById('fax'+camps).value=fields[3]; 
			document.getElementById('email'+camps).value=fields[4]; 
			document.getElementById('cname'+camps).value=fields[5]; 
			document.getElementById('caddress'+camps).value=fields[6]; 
			document.getElementById('city'+camps).value=fields[7]; 
			document.getElementById('province'+camps).value=fields[8]; 
			document.getElementById('zip'+camps).value=fields[9]; 
			document.getElementById('citycode'+camps).value=fields[10]; 
			document.getElementById('provincecode'+camps).value=fields[11]; 
			$('#contact'+camps+'Val').html(''); 
			$('#phone'+camps+'Val').html(''); 
			$('#fax'+camps+'Val').html(''); 
			$('#email'+camps+'Val').html(''); 
			$('#cname'+camps+'Val').html(''); 
			$('#caddress'+camps+'Val').html(''); 
			$('#province'+camps+'Val').html(''); 
			$('#city'+camps+'Val').html(''); 
			$('#zip'+camps+'Val').html(''); 
		});
	}else{ // If its unchecked it will clear the info
			document.getElementById(block).disabled=false;
			document.getElementById('contact'+camps).value='';
			document.getElementById('phone'+camps).value='';
			document.getElementById('mobile'+camps).value='';
			document.getElementById('fax'+camps).value='';
			document.getElementById('email'+camps).value='';
			document.getElementById('cname'+camps).value='';
			document.getElementById('caddress'+camps).value='';
			document.getElementById('province'+camps).value='';
			document.getElementById('city'+camps).value='';
			document.getElementById('zip'+camps).value='';
			$('#contact'+camps+'Val').html(''); 
			$('#phone'+camps+'Val').html(''); 
			$('#fax'+camps+'Val').html(''); 
			$('#email'+camps+'Val').html(''); 
			$('#cname'+camps+'Val').html(''); 
			$('#caddress'+camps+'Val').html(''); 
			$('#province'+camps+'Val').html(''); 
			$('#city'+camps+'Val').html(''); 
			$('#zip'+camps+'Val').html(''); 
	}
	if(document.getElementById('autofill_p').checked){
		document.getElementById('currAd_p').value=1;
	}else{
		document.getElementById('currAd_p').value=0;
	}
	
	if(document.getElementById('autofill_d').checked){
		document.getElementById('currAd_d').value=1;
	}else{
		document.getElementById('currAd_d').value=0;
	}
	
}


function autoFill_company2(id,element){	
	$.post("/includes/fillComp.php", {id: ""+id+"",element:""+element+""}, function(data){													
		eval(data);
	});
	document.getElementById('suggestions2'+element).style.display='none';
}

function autoFill_company(user,camps) {
	$.post("includes/autofill_info.php", {queryString: ""+user+""}, function(data){													
			fields = data.split("|");
			document.getElementById('contact'+camps).value=fields[0]; 
			document.getElementById('phone'+camps).value=fields[1]; 
			document.getElementById('mobile'+camps).value=fields[2]; 
			document.getElementById('fax'+camps).value=fields[3]; 
			document.getElementById('email'+camps).value=fields[4]; 
			document.getElementById('cname'+camps).value=fields[5]; 
			document.getElementById('caddress'+camps).value=fields[6]; 
			document.getElementById('city'+camps).value=fields[7]; 
			document.getElementById('province'+camps).value=fields[8]; 
			document.getElementById('zip'+camps).value=fields[9]; 
			document.getElementById('citycode'+camps).value=fields[10]; 
			document.getElementById('provincecode'+camps).value=fields[11]; 
			$('#contact'+camps+'Val').html(''); 
			$('#phone'+camps+'Val').html(''); 
			$('#fax'+camps+'Val').html(''); 
			$('#email'+camps+'Val').html(''); 
			$('#cname'+camps+'Val').html(''); 
			$('#caddress'+camps+'Val').html(''); 
			$('#province'+camps+'Val').html(''); 
			$('#city'+camps+'Val').html(''); 
			$('#zip'+camps+'Val').html(''); 
	});
	document.getElementById('suggestions2'+camps).style.display='none';	
}


function resetForm(msg1, msg2){
	if(confirm(msg1)){
		document.getElementById('addOrder').reset();
		camps='_d';
		$('#contact'+camps+'Val').html(''); 
		$('#phone'+camps+'Val').html(''); 
		$('#fax'+camps+'Val').html(''); 
		$('#email'+camps+'Val').html(''); 
		$('#cname'+camps+'Val').html(''); 
		$('#caddress'+camps+'Val').html(''); 
		$('#province'+camps+'Val').html(''); 
		$('#city'+camps+'Val').html(''); 
		$('#zip'+camps+'Val').html('');
		$('#date'+camps+'Val').html('');
		
		camps='_p';
		$('#contact'+camps+'Val').html(''); 
		$('#phone'+camps+'Val').html(''); 
		$('#fax'+camps+'Val').html(''); 
		$('#email'+camps+'Val').html(''); 
		$('#cname'+camps+'Val').html(''); 
		$('#caddress'+camps+'Val').html(''); 
		$('#province'+camps+'Val').html(''); 
		$('#city'+camps+'Val').html(''); 
		$('#zip'+camps+'Val').html('');
		$('#date'+camps+'Val').html('');
		
		clearList();
		clearCarFields(msg2);
	}
}

function resetRegForm(msg){
	if(confirm(msg)){
		document.getElementById('registerForm').reset();
		$('#nameVal').html(''); 
		$('#lastnameVal').html('');
		$('#phoneVal').html(''); 
		$('#cphoneVal').html(''); 
		$('#faxVal').html(''); 
		$('#cfaxVal').html(''); 
		$('#emailVal').html(''); 
		$('#cnameVal').html(''); 
		$('#caddressVal').html(''); 
		$('#provinceVal').html(''); 
		$('#cityVal').html(''); 
		$('#zipVal').html('');
		$('#newidVal').html('');
		$('#passwordVal').html('');
		$('#password2Val').html('');
		$('#businessVal').html('');
		$('#mTypeVal').html('');
		$('#gold').html('');
		$('#silver').html('');
		document.getElementById('agree').disabled=true;
		document.getElementById('agreement_display').style.display='none';
		document.getElementById('regions').style.display='none';
		
	}
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$ ' + num + '.' + cents);
}

function submitform(myform){
  document.getElementById(myform).submit();
}

function validateAddOrder(msg){
	var totalF = fieldsToValidate2.length;
	var valid= true; 
	
	if(document.getElementById('cname_p').value=="")
	{
		$.msgbox("Pickup Company name is required.",{"type":"alert"},function(){
			document.getElementById('cname_p').focus();
		});
	return false;	
	}
	
	if(document.getElementById('contact_p').value=="")
	{
		$.msgbox("Pickup contact name is required.",{"type":"alert"},function(){
			document.getElementById('contact_p').focus();
			valid=false;
		});
		return false;
	}
	
	if(document.getElementById('phone_p').value=="")
	{
		
		$.msgbox("Pickup phone is required.",{"type":"alert"},function(){
			document.getElementById('phone_p').focus();
			valid=false;
		});
		
	return false;
	}
	
	if(document.getElementById('fax_p').value=="")
	{
		$.msgbox("Pickup fax is required.",{"type":"alert"},function(){
			document.getElementById('fax_p').focus();
			valid=false;
		});
	return false;
	}
	
	if(document.getElementById('caddress_p').value=="")
	{
		$.msgbox("Pickup company address is required.",{"type":"alert"},function(){
			document.getElementById('caddress_p').focus();
			valid=false;
		});
		
		return false;
	}
	
	if(document.getElementById('city_p').value=="")
	{
	
	$.msgbox("Pickup city name is required.",{"type":"alert"},function(){
			document.getElementById('city_p').focus();
			valid=false;
		});
	return false;
		}else if(document.getElementById('citycode_p').value=="" && document.getElementById('city_p').value!="Other City"){
		$.msgbox("Please select a city from the list.",{"type":"alert"},function(){
			
			valid=false;
		});
		return false;
	}	



	if(document.getElementById('city_p2').value=="" && document.getElementById('city_p').value=="Other City"){
			$.msgbox("Please enter a pickup city.",{"type":"alert"},function(){
			document.getElementById('city_p2').focus();
			valid=false;
		});
		
		return false;
	}
	
	if(document.getElementById('zip_p').value=="")
	{
			$.msgbox("Pickup zip code is requerid.",{"type":"alert"},function(){
			document.getElementById('zip_p').focus();
			valid=false;
		});
		return false;
	}
	////////////
	if(document.getElementById('cname_d').value=="")
	{
		$.msgbox("Delivery Company name is requerid.",{"type":"alert"},function(){
		document.getElementById('cname_d').focus();
		valid=false;
		});
		return false;
	}
	
	if(document.getElementById('contact_d').value=="")
	{
		$.msgbox("Delivery contact name is requerid.",{"type":"alert"},function(){
		document.getElementById('contact_d').focus();
		valid=false;
		});
		return false;
		
	}
	
	if(document.getElementById('phone_d').value=="")
	{
		$.msgbox("Delivery phone is requerid.",{"type":"alert"},function(){
		document.getElementById('phone_d').focus();
		valid=false;
		});
		return false;
		
		
	}
	
	if(document.getElementById('fax_d').value=="")
	{
		$.msgbox("Delivery fax is requerid.",{"type":"alert"},function(){
		document.getElementById('fax_d').focus();
		valid=false;
		});
		return false;
	}
	
	if(document.getElementById('caddress_d').value=="")
	{
		$.msgbox("Company address is requerid.",{"type":"alert"},function(){
		document.getElementById('caddress_d').focus();
		valid=false;
		});
		return false;
	}
	
	if(document.getElementById('city_d').value=="")
	{
		$.msgbox("Delivery city is requerid.",{"type":"alert"},function(){
		document.getElementById('city_d').focus();
		valid=false;
		});
		return false;
		
		
	}else if(document.getElementById('citycode_d').value=="" && document.getElementById('city_d').value!="Other City"){
		$.msgbox("Please, select a delivery city from the list.",{"type":"alert"},function(){
		valid=false;
		});
		return false;
		
	}	
	
	if(document.getElementById('city_d2').value=="" && document.getElementById('city_d').value=="Other City"){
		
		$.msgbox("Please enter a delivery city.",{"type":"alert"},function(){
		document.getElementById('city_d2').focus();
		valid=false;
		});
		return false;
	}
	
	if(document.getElementById('zip_d').value=="")
	{
		$.msgbox("Delivery zip code is requerid.",{"type":"alert"},function(){
		document.getElementById('zip_d').focus();
		valid=false;
		});
		return false;
	}
		// Validate that there are no error on the fields
		for(i=0; i<totalF; i++){ 
			
			if (document.getElementById("email_p").value!=""){
				if($('#'+fieldsToValidate2[i]+'Val').html()!='' || document.getElementById(fieldsToValidate2[i]).value=='' && valid){
					valid=false;
					document.getElementById(fieldsToValidate2[i]).focus();
					notValid2(fieldsToValidate2[i],'');
				}
			}
		}
		
		// Validate that the delivery date is after pick up date
		if(!validDates(fieldsToValidate2[8],fieldsToValidate2[17]) && valid){
			valid=false;
			notValid2('date_d','');
			$.msgbox("The delivery date is before than the pickup date.",{"type":"alert"},function(){
			document.getElementById('date_d').focus();
			valid=false;
			});
			return false;
			}
		
		// Validate that there car's list is not empty
		if(carsAdded<1 && valid){
			valid=false;
			document.getElementById('make').focus();
			$.msgbox("Please add a vehicle.",{"type":"alert"},function(){
			valid=false;
			});
			return false;
		}
		
		var mods=document.getElementById("modsx").value;
		var cmts=document.getElementById("comments").value;
		if (mods>0 && cmts==""){
			valid=false;
			$.msgbox("Please add comments for your modified vehicles or if this have some content inside.",{"type":"alert"},function(){
			document.getElementById('comments').focus();
			valid=false;
			});
			return false;
			}
		
		
		
		
		
		
		
		if(valid){
			$.msgbox("Are you sure all the information is correct?", {
			type: "confirm",
			buttons : [
			{type: "submit", value: "Yes"},
			{type: "submit", value: "No"}  ] }, function(result) {
				if (result=="Yes"){
					submitform('addOrder');
				}
				if (result=="No"){
					return;
				}
			});
		}
}


function validateRegister(msg){
	//alert(document.getElementById('citycode').value);
	if(document.getElementById('citycode').value == "" && document.getElementById('city').value != 'Other City'){
		alert('Please select your city; if your city is not listed below, please click OTHER CITY, type your city name and select a province/territory');
		document.getElementById('city').focus();
		return false;
	}
	if(document.getElementById('city').value == 'Other City')
	{
		if(document.getElementById('other_city2').value == ""){
			alert('Please type your city name');
			document.getElementById('other_city2').focus();
			return false;
		}
		if(document.getElementById('provinceCombo').value == ""){
			alert('Please select a province/territory');
			document.getElementById('provinceCombo').focus();
			return false;
		}
	}
	//return false;
	/*if(document.getElementById('citycode').value == 'Other city')
	{
		alert('Please select your city if your city is not listed below please click OTHER CITY and type your city name and select a province/territory');
	}*/
	
	var totalF = fieldsToValidate2.length;
	var valid= true; 
	
	// Validate that there are no error on the fields
	for(i=0; i<totalF; i++){ 
		if(($('#'+fieldsToValidate2[i]+'Val').html()!='' || document.getElementById(fieldsToValidate2[i]).value=='') && valid){
			valid=false;
			document.getElementById(fieldsToValidate2[i]).focus();
			notValid2(fieldsToValidate2[i],'');
		}
	}
	
	if  (document.getElementById("city").value=="Other City"){
	 if (document.getElementById("other_city2").value!=""){
		if(document.getElementById("provinceCombo").value==""){
			alert("Please select province / territory");
			document.getElementById("provinceCombo").focus();
			valid=false;
		}
	 }
	}
	
	if(document.getElementById("email2").value == '' && document.getElementById("email").value != ''){
		alert("Please confirm your email");
		document.getElementById("email2").focus();	
		valid=false;
		return false;	
	}else{
		if(document.getElementById("email").value != document.getElementById("email2").value){
				alert("Please confirm your email");
				document.getElementById("email2").focus();
				valid=false;
				return false;				
		}
	}
	
	//Validate a business type is choosen
	if(!(document.getElementById('business').value==1 || document.getElementById('business').value==2 || document.getElementById('business').value==3) && valid){ 
		valid=false;
		notValid2('business','');
		document.getElementById('business').focus();
	}
	
	
	
	
	//Validate a membership type is choosen
	if((!document.getElementById('mType1').checked && !document.getElementById('mType2').checked) && valid){ 
		valid=false;
		notValid2('mType',''); 
		document.getElementById('mType').focus();
	}
		
	//Validate Agreement is checked
	if(!document.getElementById('agree').checked  && valid) {
		valid=false;
		notValid2('agree','');
		document.getElementById('agree').focus();
	}
	
	
	
	
	//validate regions
	if(document.getElementById("business").value==2){		
		z=0;
		for(x=1;x<=13;x++){
			if(document.forms["registerForm"].elements["reg-"+x].checked==true){
			z++;
			}
		}
		if(z==0){
			 alert("Please select at least one province that you wish to do business in.");
			 valid=false;
			 return false;
		} 
	}
	
	
	//Validate the code image
	$.post("/rcodei.php", {rcodei:document.getElementById('rcodei').value}, function(data){
			if(data!="ok"){
				document.getElementById("rcodes").style.display="block";
				alert("PLEASE ENTER THE IMAGE CODE ON THE LEFT");
				return;				
			}else{
				
				if(valid){
					ClickSubmit++;
					//document.getElementById('SubmitReg').disabled = true;
					//document.getElementById('SubmitReg').value = 'Waiting...';
					if(ClickSubmit == 1){
						if(confirm(msg)){			
							submitform('registerForm');
						}else{
							document.getElementById('SubmitReg').disabled = false;
							document.getElementById('SubmitReg').value = 'Submit Form';
							ClickSubmit = 0;
						}
					}
				}				
			}
			
	});

}

function unsetArrow(){
	document.getElementById("rcodes").style.display="none";
	document.getElementById('rcodei').value="";
	
	}


function validateRegister_admin(msg){
	
	var totalF = fieldsToValidate2.length;
	var valid= true; 
	
	// Validate that there are no error on the fields
	for(i=0; i<totalF; i++){ 
		if(($('#'+fieldsToValidate2[i]+'Val').html()!='' || document.getElementById(fieldsToValidate2[i]).value=='') && valid){
			valid=false;
			document.getElementById(fieldsToValidate2[i]).focus();
			notValid_admin(fieldsToValidate2[i],'');
		}
	}
	
	if(valid){
		if(confirm(msg)){
			submitform('registerForm');
		}
	}

}
function validateRegister_adminE(msg){
	var totalF = fieldsToValidate2.length;
	var valid= true; 
	
	// Validate that there are no error on the fields
	for(i=0; i<totalF; i++){ 		
		if(($('#'+'E'+fieldsToValidate2[i]+'Val').html()!='' || document.getElementById('E'+fieldsToValidate2[i]).value=='') && valid){			
			alert(document.getElementById('E'+fieldsToValidate2[i]).value)
			valid=false;
			document.getElementById('E'+fieldsToValidate2[i]).focus();
			notValid_admin('E'+fieldsToValidate2[i],'');
		}
	}
	
	if(valid){
		if(confirm(msg)){
			submitform('registerFormEdit');
		}
	}

}

function validModifyOrder(msg,date1,date2,tcars){
	if(confirm(msg)){
		var valid=true;
		var err=1;
		for(i=1; i<=tcars; i++){
			validatePrice(document.getElementById('uPrice_'+i).value, 'uPrice_'+i, 'No empty', 'Only Numbers');
			if($('#uPrice_'+i+'Val').html() != '' && valid){
				valid=false;
				err=i;
			}
		}
		if(!valid){
			document.getElementById('uPrice_'+err).focus();
		}else if(!validDates(date1,date2)){
			alert('The delivery date is before or the same than the pick up date');
			document.getElementById('date_p').focus();
		}else{
			submitform('modifOrder');
		}
	}
}

function validDates(date1,date2){
	
	var yr1=parseInt((document.getElementById(date1).value).substring(6,8),10);
	var mo1=parseInt((document.getElementById(date1).value).substring(3,5),10);
	var da1=parseInt((document.getElementById(date1).value).substring(0,2),10);
	var yr2=parseInt((document.getElementById(date2).value).substring(6,8),10);
	var mo2=parseInt((document.getElementById(date2).value).substring(3,5),10);
	var da2=parseInt((document.getElementById(date2).value).substring(0,2),10);
	//alert('yr1='+yr1+' mo1='+mo1+' da1='+da1+' : yr2='+yr2+' mo2='+mo2+' da2='+da2);
	if(yr1 == yr2){
		if(mo1 == mo2){
			if(da1<= da2){
				return true;
			}else{
				return false;
			}
		}else if(mo1 < mo2){
			return true;
		}else{
			return false;
		}
	}else if(yr1 < yr2){
		return true;	
	}else{
		return false;
	}
}
function money(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function addCar(make,makeid,makeother,model,modelid,modelother,year,vin,price,selPlease,msgMissing,msgMaximum,msgVin,engine,content){
	
	
	/////////CHECK FIRST FOR PRICE
	var netPrice=document.getElementById("avrg_price").value;
	var hprice=netPrice*1.20;
	var lprice=netPrice*0.80;
	
	if (price<lprice){
	alert("Please make sure that your price is higher than  $" + money(lprice)+".");// + " and $" + money(hprice));	
	document.getElementById("price").value="";
	document.getElementById("price").focus();
	return;
    }
		
	/////////////////////////////////
	
	var alphaExp = /^[0-9]+$/;
	var vinRepeated = false;
	for(i=1; i<carsValues.length; i++){
		if(carsValues[i][3]==vin){
			vinRepeated = true;
		}
	}
	
	if(make=='' || make==selPlease || makeid==''||model==''|| model== selPlease || modelid==''||year==''|| !year.match(alphaExp) || vin==''||price==''||(makeid=='34' && (makeother=='' || modelother==''))||(modelid=='490' && modelother=='')||($('#vinVal').html()!='')||($('#priceVal').html()!='')){
		
		//alert(msgMissing);
		if(make==''|| make==selPlease|| makeid=='' ){
			notValid2('make','No empty');notValid2('make','No empty');
		}else if(model==''){
			notValid2('model','No empty');
		}else if(year==''||!year.match(alphaExp)){
			notValid2('year','No empty');
		}else if(vin==''){
			notValid2('vin','No empty');
		}else if(price==''){
			notValid2('price','No empty');
		}else if((makeid=='34' && (makeother=='' || modelother==''))){
			notValid2('makeother','No empty');
		}else if((modelid=='490' && modelother=='')){
			notValid2('modelother','No empty');
		}else if(($('#vinVal').html()!='')||($('#priceVal').html()!='')){
			alert(msgMissing);
		}
		
		
	}else if(vinRepeated){
		alert(msgVin);
	}else{
		if(makeid=='34'){
			make=makeother;
		}
		if(modelid=='490'){
			model=modelother;
		}
		
		
		
		

	    if(document.getElementById("en").checked==false && document.getElementById("en2").checked==false){
		alert("Please select the Engine of the vehicle.");
		return;
		}
		
	
		if(document.getElementById("running").checked==false && document.getElementById("running2").checked==false){
		alert("Please select if the vehicle is running or not.");
		return;
		}
		
	
		if(document.getElementById("mod").checked==false && document.getElementById("mod2").checked==false){
		alert("Please select if the vehicle has been modified.");
		return;
		}
		
		
		if(document.getElementById("cnt").checked==false && document.getElementById("cnt2").checked==false){
		alert("Please select if there any content inside the vehicle.");
		return;
		}
		

		
		
		price = price.replace(/\$|\,/g,'');
		var prevTable=$('#vehiclesList').html();
		
		
		var running = document.getElementById("running").checked;
		var modified = document.getElementById("mod").checked;
		
		
		if (running){
			running="yes";
			}else{
				running="not";
				}
		
		if (modified){
			modified="not";
			}else{
				modified="yes";
			}
		
		if(content){
			cnt="yes";
			}else{
				cnt="not";
				}
		
		
		var cnt=document.getElementById("cnt2").checked;
				
		if(cnt){
			cnt="yes";
			}else{
				cnt="not";
				}
		
		if(carsAdded<carMax){
			carsAdded++;
			carNum++;
			carsValues[carNum]=new Array(make,model,year,vin,price,makeother,modelother,makeid,modelid,engine,running,modified,cnt);
	 		refreshCarList(price);
			document.getElementById("running").checked=false;
			document.getElementById("mod").checked=false;
			document.getElementById("en").checked=false;
			document.getElementById("cnt").checked=false;
			document.getElementById("running2").checked=false;
			document.getElementById("mod2").checked=false;
			document.getElementById("en2").checked=false;
			document.getElementById("cnt2").checked=false;

			//document.getElementById('upd_price').style.display = 'inline-block';
			clearCarFields(selPlease);
		}else{
			alert(msgMaximum+' '+carMax);
			
		}
	}
}

function clearList(){
	carsAdded=0;
	carNum=0;
	totalPrice=0;
	autoFill(false,'','_p','autofill_d');
	var carsValues=new Array();
	$('#totalPrice').html('');
	$('#vehiclesList').html('<tr id="default"><td colspan="4"></td></tr>');
}

function deleteCar(car,price){
	$.msgbox("Are you sure you want to remove this vehicle?", {
			type: "confirm",
			buttons : [
			{type: "submit", value: "Yes"},
			{type: "submit", value: "No"}  ] }, function(result) {
				if (result=="Yes"){
					carsAdded--;
					carsValues[car]='deleted';
					refreshCarList(price);
				}
				if (result=="No"){
					return;
				}
			});
}

function refreshCarList(price){
    document.getElementById("modsx").value=0;
	var j=1;
	var tableCars='';
	var pTotal=0;
	var mdx="";
	brf="";
	for(i=1; i<carsValues.length; i++){
		if(carsValues[i] != 'deleted'){
			var fuelTitle='';
			if(carsValues[i][9]=='g'){
				fuelTitle='Gas Engine';
			}else{
				fuelTitle='Diesel Engine';
			}
			

			if (carsValues[i][11]!="not"){
				document.getElementById("modsx").value=parseInt(document.getElementById("modsx").value)+1;
				mdx='<img src="/images/mod.gif" style="float:inherit; width:16px; margin-bottom:-2px; margin-left:5px;" title="Modified Car">';
				}

			
			if (carsValues[i][12]=="yes"){
					document.getElementById("cnts").value=parseInt(document.getElementById("cnts").value)+1;
					brf='<img src="/images/brief.jpg" style="float:inherit; width:16px; margin-bottom:-2px; margin-left:5px;" title="Vehicle with content inside.">';
				}
			
			var xPrice=0;
			var prc=0;
			prc=document.getElementById("percent").value;
			xPrice=carsValues[i][4]*prc;
			
			if (carsValues[i][10]!="yes"){
			tableCars += '<tr style="color: #F00" id="carNum'+j+'"><td title="Not Running">'+carsValues[i][2]+' '+carsValues[i][0].toUpperCase()+' '+carsValues[i][1]+' <img src="images/fuel'+carsValues[i][9]+'.gif" style="float:inherit; width:16px; margin-bottom:-2px; margin-left:5px;" title="'+fuelTitle+'"/>'+mdx+brf+'</td><td>'+carsValues[i][3].toUpperCase()+'</td><td>'+formatCurrency(xPrice)+'</td><td><a style="cursor:pointer;" onclick="deleteCar('+i+','+price+');"><img src="images/delete.gif"/></a><input type="hidden" name="make'+j+'" value="'+carsValues[i][7].toUpperCase()+'" /><input type="hidden" name="model'+j+'" value="'+carsValues[i][8]+'" /><input type="hidden" name="year'+j+'" value="'+carsValues[i][2]+'" /><input type="hidden" name="price'+j+'" value="'+parseFloat(xPrice)+'" /><input type="hidden" name="vin'+j+'" value="'+carsValues[i][3]+'" /><input type="hidden" name="makeOther'+j+'" value="'+carsValues[i][5]+'" /><input type="hidden" name="modelOther'+j+'" value="'+carsValues[i][6]+'" /><input type="hidden" name="engine'+j+'" value="'+carsValues[i][9].toUpperCase()+'" /><input type="hidden" name="running'+j+'" value="'+carsValues[i][10]+'" /><input type="hidden" name="modified'+j+'" value="'+carsValues[i][11]+'" /><input type="hidden" name="cnt'+j+'" value="'+carsValues[i][12]+'" /><input type="hidden" name="real_price'+j+'" value="'+carsValues[i][4]+'" /></td></tr>';
			}else{
				tableCars += '<tr  id="carNum'+j+'"><td >'+carsValues[i][2]+' '+carsValues[i][0].toUpperCase()+' '+carsValues[i][1]+' <img src="images/fuel'+carsValues[i][9]+'.gif" style="float:inherit; width:16px; margin-bottom:-2px; margin-left:5px;" title="'+fuelTitle+'"/>'+mdx+brf+'</td><td>'+carsValues[i][3].toUpperCase()+'</td><td>'+formatCurrency(xPrice)+'</td><td><a style="cursor:pointer;" onclick="deleteCar('+i+','+price+');"><img src="images/delete.gif"/></a><input type="hidden" name="make'+j+'" value="'+carsValues[i][7].toUpperCase()+'" /><input type="hidden" name="model'+j+'" value="'+carsValues[i][8]+'" /><input type="hidden" name="year'+j+'" value="'+carsValues[i][2]+'" /><input type="hidden" name="price'+j+'" value="'+parseFloat(xPrice)+'" /><input type="hidden" name="vin'+j+'" value="'+carsValues[i][3]+'" /><input type="hidden" name="makeOther'+j+'" value="'+carsValues[i][5]+'" /><input type="hidden" name="modelOther'+j+'" value="'+carsValues[i][6]+'" /><input type="hidden" name="engine'+j+'" value="'+carsValues[i][9].toUpperCase()+'" /><input type="hidden" name="running'+j+'" value="'+carsValues[i][10]+'" /><input type="hidden" name="modified'+j+'" value="'+carsValues[i][11]+'" /><input type="hidden" name="cnt'+j+'" value="'+carsValues[i][12]+'" /><input type="hidden" name="real_price'+j+'" value="'+carsValues[i][4]+'" /></td></tr>';
			}
			brf="";
			mdx="";
			pTotal+=parseFloat(xPrice);
			j++;
		}
	}
	if(carsAdded>0){
		$('#vehiclesList').html(tableCars);
	}else{
		$('#vehiclesList').html('<tr id="default"><td colspan="4"></td></tr>');
	}
	totalPrice=parseFloat(pTotal);
	$('#totalPrice').html(formatCurrency(totalPrice)+'<input type="hidden" value="'+parseFloat(totalPrice)+'" name="totalPrice" /><input type="hidden" value="'+parseFloat(price)+'" name="unitPrice" /><input type="hidden" value="'+carsAdded+'" name="carsAdded" />');
}

function clearCarFields(selPlease){
	fillMake(selPlease);
	document.getElementById('makeother').style.display='none';
	document.getElementById('modelother').style.display='none';
	if(carsAdded < 1){
		document.getElementById('upd_price').style.display='none';
		
	}
	document.getElementById('price').value='';
	document.getElementById('year').selectedIndex=0;
	document.getElementById('vin').value='';
	document.getElementById("avgprice").style.displan="none";
	
	/*document.getElementById('running').value="Yes";
	document.getElementById('mod').value="Not";*/
	
	$('#model').html('');
}

function warningBtn(element,msg){
	$('#'+element+'Val').html('<a style=\'color:#C00;\' href=\'javascript:hideWarning2("'+element+'");\'><img src=\'images/arrow.jpg\' class=\'warning_img\' /><br /><div style="padding:0px 0px 0px 0px; float:left; line-height:13px; background:#fff; cursor:pointer; width:auto; height:auto; margin:0px; border:none; text-transform:uppercase; font-size:13px; text-align:left;" id=\''+element+'War\' class=\'effect'+element+'\'>'+msg+'</div></a>');
	//$('.effect'+element).animate({width:235}, 500);
	//document.getElementById(element).focus();
}

function warningBtn_admin(element,msg){
	$('#'+element+'Val').html('<a style=\'color:#C00;\' href=\'javascript:hideWarning2("'+element+'");\'><img src=\'../images/arrow.jpg\' class=\'warning_img\' /><br /><div style="padding:0px 0px 0px 0px; float:left; line-height:13px; background:#fff; cursor:pointer; width:auto; height:auto; margin:0px; border:none; text-transform:uppercase; font-size:13px; text-align:left;" id=\''+element+'War\' class=\'effect'+element+'\'>'+msg+'</div></a>');
	//$('.effect'+element).animate({width:235}, 500);
	//document.getElementById(element).focus();
}

function displayWarning(element,msg){
	$('#'+element+'Val').html('<br /><a style=\'color:#000;\' href=\'javascript:hideWarning("'+element+'");document.getElementById("'+element+'").focus();\'><div style="padding:5px 10px 5px 10px; float:left; line-height:14px; position:absolute; left:auto; top:auto;  background:#FFCC00; cursor:pointer; opacity:0.9; width:0px; height:auto; margin:0px; border:none; text-transform:uppercase; font-size:13px; text-align:center;" id=\''+element+'War\' class=\'effect'+element+'\'>'+msg+'</div></a>');
	$('.effect'+element).animate({width:235}, 500);
}

function hideWarning(element){
	//$('.effect'+element).animate({width:0, height:0, padding:0}, 500);
	//$('#'+element+'War').html('');
	$('#'+element+'Val').html('');
	//document.getElementById(element).focus();
}

function hideWarning2(element){
	//$('.effect'+element).animate({width:0, height:0, padding:0}, 500);
	//$('#'+element+'War').html('');
	$('#'+element+'Val').html('');
	document.getElementById(element).focus();
}

function isChecked(element,msg){
	if(!document.getElementById(element).checked){
		notValid(element,msg);
	}
}

function loadAgreement(type,agreement1,gold1,silver1,agreement2,gold2,silver2){
	if(type==0){
		document.getElementById('agreement_display').style.display='none';
		//$('#agreement').html('');
		$('#gold').html('');
		$('#silver').html('');
		notValid('business','');
		return;
		}

	if(type==1){
		//$('#agreement').html(agreement1);
		//document.getElementById("agreement").innerHTML='<object style="width:500px; height=50px; overflow=hidden;" id="foo" name="foo" type="text/html" data="agreement.html"><\/object>';
		$('#gold').html(gold1);
		$('#silver').html(silver1);
		isValid2('business');
		document.getElementById('agreement_display').style.display='block';
		document.getElementById('regions').style.display='none';
	}else if(type==2){
		//$('#agreement').html(agreement2);
		$('#gold').html(gold2);
		$('#silver').html(silver2);
		document.getElementById('agreement_display').style.display='block';
		document.getElementById('regions').style.display='block';
		
		isValid2('business');
	}else if(type==3){
		$('#gold').html(gold1);
		$('#silver').html(silver1);
		isValid2('business');
		document.getElementById('agreement_display').style.display='block';
		document.getElementById('regions').style.display='none';
		}else{
		$('#agreement').html('');
		$('#gold').html('');
		$('#silver').html('');
		notValid('business','');
		document.getElementById('agreement_display').style.display='none';
				document.getElementById('regions').style.display='none';
	}
}

function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("includes/rpc.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
} // lookup

function lookup3(object,element) {
	inputString=object.value;
	
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions'+element).hide();
		} else {
			$.post("includes/rpc_order.php", {queryString: ""+inputString+"",element: ""+element+""}, function(data){
				if(data.length!="" ) {
					$('#suggestions'+element).show();
					$('#autoSuggestionsList'+element).html(data);
				}
			});
		}
	}
} // lookup


function lookup4_vehicle(object,element) {
	inputString=object.value;
	var canada=document.getElementById("country"+element).checked;
		
	if (canada){
		var country="1";
		}else{
			var country="2";
			}
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions'+element).hide();
		} else {
			$.post("includes/rpc_order2_vehicle.php", {queryString: ""+inputString+"",element: ""+element+"","country":country}, function(data){
				//alert(data);
				if(data.length!="" ) {
					$('#suggestions'+element).show();
					$('#autoSuggestionsList'+element).html(data);
				}
			});
		}
	}
} // lookup

function lookup4(object,element) {
	inputString=object.value;
	var canada=document.getElementById("country"+element).checked;
	
		
	if (canada){
		var country="1";
		}else{
			var country="2";
			}
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions'+element).hide();
		} else {
			$.post("includes/rpc_order2.php", {queryString: ""+inputString+"",element: ""+element+"","country":country}, function(data){
				//alert(data);
				if(data.length!="" ) {
					$('#suggestions'+element).show();
					$('#autoSuggestionsList'+element).html(data);
				}
			});
		}
	}
} // lookup



//// OTHER DEALERS    ///////////
function lookupCityOthers(object,element) {
	inputString=object.value;
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions'+element).hide();
		} else {
			$.post("../includes/rpc_order.php", {queryString: ""+inputString+"",element: ""+element+""}, function(data){
				if(data.length!="" ) {
					$('#suggestions'+element).show();
					$('#autoSuggestionsList'+element).html(data);
				}
			});
		}
	}
} // lookup
///////////////////////////








function lookup_register(object,element) {
	inputString=object.value;
	
	var canada=document.getElementById("country").checked;
	
	if (canada){
		var country="1";
		}else{
			var country="2";
			}
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions'+element).hide();
		} else {
			$.post("includes/rpc.php", {queryString: ""+inputString+"",element: ""+element+"", "country":country}, function(data){
				if(data.length!="" ) {
					$('#suggestions'+element).show();
					$('#autoSuggestionsList'+element).html(data);
				}
			});
		}
	}
} // lookup


function lookCompany(object,element) {
	var data="";
	if(object==''){
		inputString=='';
	}else{
		inputString=object.value;
	}
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions2'+element).hide();
		} else {
			var info="";
			$.post("includes/lookCompany.php", {"queryString": inputString, "element": element}, function(data){
				
                info="-"+data;
							
//				alert(data);
				if(info!="-undefined") {
					$('#suggestions2'+element).show();
					$('#autoSuggestionsList2'+element).html(data);
				}else{
					$('#suggestions2'+element).hide();
					}
			});
		}
	}
} // lookup




function getCompany(object,element,hidden) {
		document.getElementById('autoSuggestionsList3').style.display='block';
	document.getElementById('autoSuggestionsList2').style.display='block';
	var data="";
	if(object==''){
		inputString=='';
	}else{
		inputString=object.value;
	}

	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			$('#suggestions2').hide();
			$('#suggestions3').hide();			
		} else {
			var info="";
			$.post("includes/getCompany.php", {"queryString": inputString, "element": element, "hidden":hidden}, function(data){
				
                info="-"+data;
			
				
				if(info!="-undefined") {
					$('#suggestions2').show();
					$('#suggestions3').show();					
					$('#autoSuggestionsList2').html(data);
					$('#autoSuggestionsList3').html(data);					
				}else{
					$('#suggestions2').hide();
					$('#suggestions3').hide();					
					}
			});
		}
	}
}    

function getCompany2(object,element,hidden) {
	document.getElementById('autoSuggestionsList3').style.display='block';
	var data="";
	if(object==''){
		inputString=='';
	}else{
		inputString=object.value;
	}

	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions3').hide();
		} else {
			var info="";
			$.post("includes/getCompany.php", {"queryString": inputString, "element": element, "hidden":hidden}, function(data){
				
                info="-"+data;
			
				alert(".");
				if(info!="-undefined") {
					$('#suggestions3').show();
					$('#autoSuggestionsList3').html(data);
				}else{
					
					$('#suggestions3').hide();
					}
			});
		}
	}
}

// lookup

//////////dealers autocomplet search section
/*
function getCompany_haulers(object,element,hidden) {
	document.getElementById('autoSuggestionsList2').style.display='block';
	var data="";
	if(object==''){
		inputString=='';
	}else{
		inputString=object.value;
	}

	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions2').hide();
		} else {
			var info="";
			$.post("includes/getCompany.php", {"queryString": inputString, "element": element, "hidden":hidden}, function(data){	
                info="-"+data;
				if(info!="-undefined") {
					$('#suggestions2').show();
					$('#autoSuggestionsList2').html(data);
				}else{
					$('#suggestions2').hide();
				}
			});
		}
	}
}
////////////////
/////takecompany for haulers
function takeCompany2(element, value, hidden)
{
	if(hidden == 0){
		document.getElementById(element).value = value;
	}else{
		document.getElementById(element).value = value;
		//document.getElementById('H_'+element).value = hidden;	
		//document.getElementById('BtnAssignH').disabled=false;	
	}
	document.getElementById('autoSuggestionsList2').style.display='none';
}
*/
/////////
function takeCompany(element, value, hidden)
{
	if(hidden == 0){
		document.getElementById(element).value = value;
	}else{
		document.getElementById(element).value = value;
		document.getElementById('H_'+element).value = hidden;	
		document.getElementById('BtnAssignH').disabled=false;	
	}
	document.getElementById('autoSuggestionsList2').style.display='none';
}

function lookCompany_admin(object,element,field) {
	if(object==''){
		inputString=='';
	}else{
		inputString=object.value;
	}
	
	if(!document.getElementById(object.name).readOnly){
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions2'+element).hide();
		} else {
			$.post("includes/lookCompany.php", {queryString: ""+inputString+"",element: ""+element+"",field: ""+field+""}, function(data){
				if(data.length >0) {
					$('#suggestions2'+element).show();
					$('#autoSuggestionsList2'+element).html(data);
				}
			});
		}
	}
} 

function clearSuggBox(element){
	//$('#suggestions2'+element).hide();
	var funcion="destroyBox('"+element+"');"
	setInterval(funcion,2000);
	//setTimeout($('#suggestions2'+element).hide(),2000);
}

function destroyBox(element){
	$('#suggestions2'+element).hide();
}

function setCompany_admin(val, field, element){
	var thisValue = val.replace(/\:/g,"'"); 
	document.getElementById(field).value=thisValue;
	$('#suggestions2'+element).hide();
	$('#autoSuggestionsList2'+element).html('');
}


	
function fill(val1, secondVal, regid, city_id,element) {
		var thisValue = val1.replace(/\:/g,"'"); 
		$('#city'+element).val(thisValue);
		$('#province'+element).val(secondVal);
		$('#citycode'+element).val(city_id);
		$('#provincecode'+element).val(regid);
		$('#suggestions'+element).hide();
		try{
		document.getElementById('zip'+element).focus();
		}catch(e){
			
			}
		isValid2('city');
		isValid2('province');
		$('#other_city').hide();
	    $('#provinceCombo').hide();
	    $('#province').show();
		
}


function clearme(){
$('#city').val("");	
$('#province').val("");	
$('#other_city2').val("");
$('#other_city').hide();
$('#citycode').val("");
$('#city').val("");
$('#provinceCombo').val("");
$('#provinceCombo').hide();
$('#province').show();
}


function other(){
	$('#suggestions').hide();
	$('#other_city').show();
	$('#other_city2').val("");
	$('#provinceCombo').show();
	$('#province').hide();
	$('#city').val("Other City");
	document.getElementById("other_city2").focus();
	}


function lookup2(inputString,element,element2,element3) {
		
		if(inputString.length == 0) {
			// Hide the suggestion box.
			//$('#'+element+'Box').html('');
			//hideSuggestionBox(element);
			$('#suggestions_list').html('');
			displaySuggestionBox(element);
		} else {
						
			$.post("includes/rpc2.php", {queryString: ""+inputString+"",field: ""+element+"",field2: ""+element2+"",field3: ""+element3+""}, function(data){
				if(data.length >0) {
					//alert(data);
					animateBox();
					$('#suggestions_list').html(data);
				}
			});
		}
} // lookup
	
function fill2(thisValue, secondVal, regid, city_id, element1,element2,element3) {
		$('#'+element1).val(thisValue);
		$('#'+element2).val(secondVal);
		//$('#'+element1+'code').val(city_id);
		//$('#'+element2+'code').val(regid);
		//isValid2(element1);
		//isValid2(element2);
		hideSuggestionBox(element1);
		//setTimeout('hideSuggestionBox();',200);
		document.getElementById(element3).focus();
		//setTimeout('$(\'#Box\').hide();', 500);
}
//_suggestions por Box, effect por box, autosuggestionlist por suggestionList
function displaySuggestionBox(element){
	$('#'+element+'Box').html('<div style="position: absolute; left: inherit; top: inherit; margin: 5px 0px 0px 0px; width: 0px; height: auto; background-color:#333333; border: 0px solid #000; color: #fff;" id="Box" class="Sbox"><div class="suggestionList" id="suggestions_list">&nbsp;</div></div>');
}

function animateBox(){
	$('.Sbox').animate({width:250, border:1, height:'auto'}, 1000);
}

function hideSuggestionBox(element){

	$('#suggestions'+element).hide();					
	//$('#suggestions_list').html('');
	
	//$('.Sbox').animate({width:0, height:0, padding:0}, 500);
	//setTimeout('$(\'#'+element+'Box\').html(\'\');',500);
	
	//$('#'+element+'War').html('');
	//$('#'+element+'Val').html('');
	//document.getElementById(element).focus();
}

function validateLength(whatYouTyped,length,element,msgNo,msgEmpty) {

	var txt = whatYouTyped.value;
	//username_msg.html('hola');
	if (txt.length > length) {
		//fieldset.className = "welldone";
		isValid(element);
	}
	else {
		//fieldset.className = "";
		if(txt.length==0){
			notValid(element,msgEmpty);
		}else{
			notValid(element,msgNo);
		}
	}
}

function validateLength2(whatYouTyped,length,element,msgNo,msgEmpty) {
	var txt = whatYouTyped.value;
	//username_msg.html('hola');
	if (txt.length >= length) {
		//fieldset.className = "welldone";
		isValid2(element);
	}
	else {
		//fieldset.className = "";
		if(txt.length==0){
			notValid2(element,msgEmpty);
		}else{
			notValid2(element,msgNo);
		}
	}
}

function validateLength_admin(whatYouTyped,length,element,msgNo,msgEmpty) {
	var txt = whatYouTyped.value;
	//username_msg.html('hola');
	if (txt.length >= length) {
		//fieldset.className = "welldone";
		isValid2(element);
	}
	else {
		//fieldset.className = "";
		if(txt.length==0){
			notValid_admin(element,msgEmpty);
		}else{
			notValid_admin(element,msgNo);
		}
	}
}

function validateVIN(whatYouTyped,length,element,msgNo,msgEmpty,msgSymbol) {
	var txt = whatYouTyped.value;
	//username_msg.html('hola');
	if(txt.length==0){
		notValid2(element,msgEmpty);
	}else{
		if(validateSymbols(txt)){
			if (txt.length >= length) {
				//fieldset.className = "welldone";
				isValid2(element);
			}
			else {
				//fieldset.className = "";
				notValid2(element,msgNo);
			}
		}else{
			notValid2(element,msgSymbol);
		}
	}
}

function validateEmpty(whatYouTyped,element,msgEmpty) {
	var txt = whatYouTyped.value.trim();
	
	if(txt.length==0){
		notValid2(element,msgEmpty);
	}else{
		isValid2(element);
	}
	
}

function validateEmpty_admin(whatYouTyped,element,msgEmpty) {
	
	var txt = whatYouTyped.value;
		if(txt.length==0){
			notValid_admin(element,msgEmpty);
		}else{
			isValid2(element);
		}
	
}

function validateSymbols(whatYouTyped){
	var alphaExp = /^[0-9a-zA-Z\s]+$/;
	if(whatYouTyped.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function validatePrice(whatYouTyped,element, msgEmpty, msgSymbol){
	var alphaExp = /^[0-9$.]+$/;
	if(whatYouTyped.length==0){
			notValid2(element,msgEmpty);
	}else{
		if(whatYouTyped.match(alphaExp)){
			isValid2(element);
		}else{
			notValid2(element,msgSymbol);
		}
	}
}


function validateZip(whatYouTyped,element){
	var alphaExp = /^[0-9a-zA-Z\s]+$/;
	var text=whatYouTyped.value;
	
	if(text.match(alphaExp)){
		if(text.substring(3,4)!=' '){
			var p1=text.substring(0,3);
			var p2=text.substring(3,6);
			document.getElementById(element).value='';
			document.getElementById(element).value=p1+' '+p2;
		}else{
			var p1=text.substring(0,3);
			var p2=text.substring(4,7);
			document.getElementById(element).value='';
			document.getElementById(element).value=p1+' '+p2;
		}
	}else{
		document.getElementById(element).value='';
	}
}

function validateNumber(whatYouTyped,element,msgNo){
	var alphaExp = /^[0-9]+$/;
	var text=whatYouTyped.value;
	
	if(text.match(alphaExp)){
		isValid2(element)
	}else{
		notValid2(element,msgNo);
	}
}

function validatePassword(whatYouTyped,element,msgNo) {
	if(whatYouTyped==document.getElementById('password').value){
		isValid2(element);
	}else{
		notValid2(element,msgNo);
	}
}

function validatePassword_admin(whatYouTyped,element,msgNo) {
	if(whatYouTyped==document.getElementById('password').value){
		isValid2(element);
	}else{
		notValid_admin(element,msgNo);
	}
}

function validatePassword_adminEdit(whatYouTyped,element,msgNo) {
	if(whatYouTyped==document.getElementById('Epassword').value){
		isValid2(element);
	}else{
		notValid_admin(element,msgNo);
	}
}


function validatePassword_change(whatYouTyped,element,msgNo) {
	if(whatYouTyped==document.getElementById('password_new').value){
		isValid2(element);
	}else{
		notValid2(element,msgNo);
	}
}

function check(element){
	isValid(element);
}

function checkEmail(whatYouTyped,element,msgNo) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		isValid2(element);
	} else {
		notValid2(element,msgNo);
	}
}

function existingEmail(whatYouTyped,element,msgNo,msg_used,current) {
	if ($("#email2").attr("readonly")==false){
		var fieldset = whatYouTyped.parentNode;
		var txt = whatYouTyped.value;
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
			$.post("includes/existingEmail.php", {queryString: ""+txt+""}, function(data){
			// Is valid	
			isValid2(element)
			// data != current is for the edit current information
			if(data.length >0 && data != current) {
				// Is already taken
				notValid2(element,data+' '+msg_used);
			}});
		} else {
			notValid2(element,msgNo);
		}
	}
}

function confirmEmail(whatYouTyped,element,msgNo,msg_used,current) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (document.getElementById('email').value != txt) {
		notValid2(element,msgNo);
	}else{
		isValid2(element);
	}
}

function existingEmail_admin(whatYouTyped,element,msgNo,msg_used,current) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		$.post("../includes/existingEmail.php", {queryString: ""+txt+""}, function(data){
		// Is valid	
		isValid2(element)
		// data != current is for the edit current information
		if(data.length >0 && data != current) {
			// Is already taken
			notValid_admin(element,msg_used);
		}});
	} else {
		notValid_admin(element,msgNo);
	}
}

function checkEmail2(whatYouTyped,element,msgNo) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		isValid2(element);
	} else {
		notValid2(element,msgNo);
	}
}



function checkall(){
	//while(fieldsToValidate.length > 0){
	//alert (fieldsToValidate.shift());
	//}
	var validAll=true;
	var num = fieldsToValidate.length;
	for(i=0; i<num; i++){
		if (fieldsToValidate[i] != 'x' && fieldsToValidate[i] != undefined){
			//alert(fieldsToValidate[i]);
			validAll=false;
		}
	}
	if(validAll&&document.getElementById('agree').checked){
		document.getElementById('smbtn').disabled=false;
	}else{
		document.getElementById('smbtn').disabled=true;
	}
}

function notValid(element,message){
	document.getElementById(element).value=message;
	document.getElementById(element).checked=false;
	document.getElementById(element).style.color='#CC0000';
	
	var exist=false;
	var num = fieldsToValidate.length;
	for(i=0; i<num; i++){
		if (fieldsToValidate[i] == element){
			exist = true;
		}
	}
	if(!exist){
		fieldsToValidate[num+1]=element;
	}
	checkall();
}

function isValid(element,msg){
	if(msg){
		document.getElementById(element).value=msg;
	}else{
		document.getElementById(element).value='';
	}
	document.getElementById(element).style.color='#009900';
	document.getElementById(element).checked=true;
	
	var num = fieldsToValidate.length;
	for(i=0; i<num; i++){
		if (fieldsToValidate[i] == element){
			fieldsToValidate[i]='x';
		}
	}
	checkall();
}

function isValid2(element,msg){
	hideWarning(element);
}

function notValid2(element,message){
	warningBtn(element,message);
}

function notValid_admin(element,message){
	warningBtn_admin(element,message);
}





function editFields2(element,fields,save,cancel,edit,tdTitle,msg,companyMsg,uID){
	fieldsToChange = fields.split(",");
	var num = fieldsToChange.length;
	
	for(i=0; i<num; i++){
		document.getElementById(fieldsToChange[i]).className = 'editable';
		if(fieldsToChange[i] != 'province'){
			document.getElementById(fieldsToChange[i]).readOnly = false;
		}
	}
	
	if(element=='Company'){
		enableDisableCompany(false);

		
	}else if(element == 'Account'){
		document.getElementById('password').value = '';
		document.getElementById('n1').style.display= 'none';
		document.getElementById('n2').style.display= 'inline';
		enableDisableAccount(false);		


	/*	$('#extra'+element).html('<tr><td align="right">'+tdTitle+'</td><td><input class="editable" tabindex="16" type="password" id="password_new" name="password_new" onblur="validateEmpty(this,this.name,\'This field cant be empty\');" /><font id="password_newVal"></font></td><tr><tr><td align="right">Repeat New Password: </td><td><input class="editable" tabindex="16" type="password" id="password_rep" name="password_rep" onblur="validatePassword_change(this.value,this.name,\''+msg+'\');" /><font id="password_repVal"></font></td><tr>');*/
		
	}else if(element == 'SecurityQuestion'){
		enableDisableSecurity(false);
/*		document.getElementById('Question').disabled = false;
		$('#extra'+element).html('<tr><td align="right">'+tdTitle+'</td><td><input type="text" name="SqAnswer" id="SqAnswer" value="" style="width:355px" ></td></tr><tr><td align="right">Type your Password:</td><td><input type="text" name="SqPass" id="SqPass" value="" style="width:355px" ></td></tr>');*/
	}else if(element == 'Contact'){
		enableDisableContact(false);		
		
	}

	$('#edit'+element).html('<input type="button" class="saveBtn" name="save" id="save" onclick="saveFieldsAjax(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+save+'" /><input type="button" class="cancelBtn" name="cancel" id="cancel" onclick="cancelChanges2(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+cancel+'" />');
}







function editFields(element,fields,save,cancel,edit,tdTitle,msg,companyMsg,uID){
	fieldsToChange = fields.split(",");
	var num = fieldsToChange.length;
	
	for(i=0; i<num; i++){
		document.getElementById(fieldsToChange[i]).className = 'editable';
		if(fieldsToChange[i] != 'province'){
			document.getElementById(fieldsToChange[i]).readOnly = false;
		}
	}
	
	if(element=='Company'){
		$('#extra'+element).html('<tr><td>&nbsp;</td><td>&nbsp;</td><td style="font-weight:bolder; font-size:16px; background-color:#E9E9E9">&nbsp;</td><td valign="top" align="right"><b>'+tdTitle+'</b></td><td><textarea tabindex="13" name="reason" id="reason" cols="20" onblur="validateEmpty(this,this.name,\''+msg+'\');"></textarea><font id="reasonVal"></font></td></tr>');
		
	}else if(element == 'Account'){
		document.getElementById('password').value = '';
		document.getElementById('n1').style.display= 'none';
		document.getElementById('n2').style.display= 'inline';
		$('#extra'+element).html('<tr><td align="right" width="217"><b>'+tdTitle+'</b></td><td><input style="width:120px" class="editable" tabindex="16" type="password" id="password_new" name="password_new" onblur="validateEmpty(this,this.name,\'This field cant be empty\');" /><font id="password_newVal"></font></td><tr><tr><td align="right" ><b>Repeat New Password: </b></td><td><input style="width:120px" class="editable" tabindex="16" type="password" id="password_rep" name="password_rep" onblur="validatePassword_change(this.value,this.name,\''+msg+'\');" /><font id="password_repVal"></font></td><tr>');
		
	}else if(element == 'SecurityQuestion'){
		document.getElementById('Question').disabled = false;
		$('#extra'+element).html('<tr><td style="font-weight:bolder; font-size:16px; background-color:#E9E9E9"></td><td align="right" width="140"><b>'+tdTitle+'</b></td><td><input style="width:248px" type="text" name="SqAnswer" id="SqAnswer" value=""  ></td></tr><tr><td style="font-weight:bolder; font-size:16px; background-color:#E9E9E9"></td><td align="right"><b>Type your Password:</b></td><td><input style="width:248px" type="text" name="SqPass" id="SqPass" value=""  ></td></tr>');
	}

	$('#edit'+element).html('<input type="button" class="saveBtn" name="save" id="save" onclick="saveFields(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+save+'" /><input type="button" class="cancelBtn" name="cancel" id="cancel" onclick="cancelChanges(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+cancel+'" />');
}
 
function saveFields(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID){ 	
	fieldsToChange = fields.split(",");
	var num = fieldsToChange.length;
	if(element == 'Account'){
		fieldsToChange[num] = 'password_new';
		num = fieldsToChange.length;
		fieldsToChange[num] = 'password_rep';
		num = fieldsToChange.length;
	}
	if(element == 'Company'){
		fieldsToChange[num] = 'reason';
		num = fieldsToChange.length;
	}
	if(element == 'SecurityQuestion'){
		fieldsToChange[num] = 'SqAnswer';
		num = fieldsToChange.length;
		fieldsToChange[num] = 'SqPass';
		num = fieldsToChange.length;
				
	}
	var valid=true;
	for(i=0; i<num; i++){
		
		if (document.getElementById("mobile").value!=""){
			if( $('#'+fieldsToChange[i]+'Val').html() !='' || document.getElementById(fieldsToChange[i]).value == '' && valid){
				valid = false;
				notValid2(fieldsToChange[i],'');
				document.getElementById(fieldsToChange[i]).focus();
			} 
		}
		
		if(element=='Account'){
			if(document.getElementById('password_new').value!=document.getElementById('password_rep').value){
				valid = false;
				notValid2('password_rep',msg);
				document.getElementById('password_rep').focus();
			}
		}
		
		if (element=='Company'){
			if( $('#'+fieldsToChange[i]+'Val').html() !='' || document.getElementById(fieldsToChange[i]).value == '' && valid){
				valid = false;
				//notValid2('password_rep',msg);
				//document.getElementById('password_rep').focus();
			}
		}
		
		
	}
	if(element == 'SecurityQuestion'){
		valid=true;
		if(document.getElementById('SqPass').value == ''){
			valid = false;
			notValid2('SqPass',msg);
			document.getElementById('SqPass').focus();
		}
		if(document.getElementById('SqAnswer').value == ''){
			valid = false;
			notValid2('SqAnswer',msg);
			document.getElementById('SqAnswer').focus();
		}		
	}
	
	//alert(element + " - " + valid);
	if(element=='Contact' && valid){
		$.post("includes/modifyContact.php", {name: ""+document.getElementById(fieldsToChange[0]).value+"",lastname: ""+document.getElementById(fieldsToChange[1]).value+"",phone: ""+document.getElementById(fieldsToChange[2]).value+"",mobile: ""+document.getElementById(fieldsToChange[3]).value+"",email: ""+document.getElementById(fieldsToChange[4]).value+"", email2: ""+document.getElementById(fieldsToChange[5]).value+"", uID: uID}, function(data){
			//if(data!='true'){
				//alert('There was an error saving this info2');
				//cancelChanges(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID);
				//valid=false;
			//}else{
				for(i=0; i<num; i++){
					document.getElementById(fieldsToChange[i]).className = 'notEditable';
					document.getElementById(fieldsToChange[i]).readOnly = true;
				}
				
				$.post("notifyContactModification.php", {cname:""+document.getElementById('cname').value+"", user:uID}, function(data){});
				$.msgbox("The information has been updated.",{"type":"info"},function(info){
				$('#edit'+element).html('<input type="button" class="editBtn" name="edit" id="edit" onclick="editFields(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+edit+'" />');
				});
			//}
		});
	}else if(element=='Company' && valid){
		$.post("modifyCompany.php", {cname: ""+document.getElementById('cname').value+"",caddress: ""+document.getElementById('caddress').value+"",city_id: ""+document.getElementById('citycode').value+"",province: ""+document.getElementById('provincecode').value+"",zip: ""+document.getElementById('zip').value+"",cphone: ""+document.getElementById('cphone').value+"",cfax: ""+document.getElementById('cfax').value+"",creason: ""+document.getElementById('reason').value+"", uID: uID}, function(data){
			 //alert(data);
			 //return false;
			if(data!='true'){
				valid=false;
				$.msgbox("There was an error saving this info.",{"type":"info"},function(info){});
				//cancelChanges(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID);
			}else{
				for(i=0; i<num; i++){
					document.getElementById(fieldsToChange[i]).className = 'notEditable';
					document.getElementById(fieldsToChange[i]).readOnly = true;
				}
				
				$('#extra'+element).html('<tr><td></td><td></td></tr>');
				

				$.msgbox(companyMsg,{"type":"info"},function(info){
					$('#edit'+element).html('<input type="button" class="editBtn" name="edit" id="edit" onclick="editFields(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+edit+'" />');
				});
			}
		});
	}else if(element=='Account' && valid){
		$.post("includes/modifyAccount.php", {"username": document.getElementById('newid').value, "password": document.getElementById('password_new').value, "uID": uID, "oldP": document.getElementById('password').value, "Action":"1"}, function(data){	
																																																											
			if(data=='true'){
				$.msgbox("The information has been updated",{"type":"info"},function(info){
				for(i=0; i<num; i++){
					document.getElementById(fieldsToChange[i]).className = 'notEditable';
					document.getElementById(fieldsToChange[i]).readOnly = true;
					document.getElementById('n2').style.display= 'none';
					document.getElementById('n1').style.display= 'inline';
				}
					
				$('#extra'+element).html('<tr><td></td><td></td></tr>');
				$.post("notifyAccountModification.php", {cname:""+document.getElementById('cname').value+"", user:uID}, function(data){});
				$('#edit'+element).html('<input type="button" class="editBtn" name="edit" id="edit" onclick="editFields(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+edit+'" />');
				});
			}else{
				if(data=='wrongPass'){
					$.msgbox("The old password is not correct.",{"type":"info"},function(info){
						notValid2('password','');
						document.getElementById('password').focus();
					});
				}
				//cancelChanges(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID);
			}
		});
	}else if(element=='SecurityQuestion' && valid){
		$.post("includes/modifyAccount.php", {"Question": document.getElementById('Question').value, "SqAnswer": document.getElementById('SqAnswer').value, "SqPass":document.getElementById('SqPass').value, "uID": uID, "Action":"2"}, function(data){
			if(data!='true'){
				valid=false;
				$.msgbox("There was an error saving this info.",{"type":"info"},function(info){});
				//cancelChanges(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID);
			}else{
					$.msgbox("The changes will appear the next time that you log.",{"type":"info"},function(info){
				document.getElementById('Question').disabled = true;
				$('#extra'+element).html('<td></td><td></td>');
				
				$('#edit'+element).html('<input type="button" class="editBtn" name="edit" id="edit" onclick="editFields(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+edit+'" />');
				});
				
			}
		});
	}
	
	//alert(element+'  '+valid)
}

function cancelChanges(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID){
	fieldsToChange = fields.split(",");
	var num = fieldsToChange.length;
	
	for(i=0; i<num; i++){
		document.getElementById(fieldsToChange[i]).className = 'notEditable';
		document.getElementById(fieldsToChange[i]).readOnly = true;
		document.getElementById(fieldsToChange[i]).value=document.getElementById(fieldsToChange[i]).defaultValue;
		document.getElementById('n2').style.display= 'none';
		document.getElementById('n1').style.display= 'inline';
		isValid2(fieldsToChange[i]);
		
	}
	if(element=='Company' || element == 'Account'){
		$('#extra'+element).html('<td></td><td></td>');
	}
	if(element == 'SecurityQuestion'){
		document.getElementById('Question').disabled = true;
		$('#extra'+element).html('<td></td><td></td>');
	}
	$('#edit'+element).html('<input type="button" class="editBtn" name="edit" id="edit" onclick="editFields(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+edit+'" />');
}

function cancelChanges2(element,fields,save,cancel,edit,tdTitle,msg,companyMsg, uID){
	if (element == 'Contact'){
		enableDisableContact(true);
	}else if(element == 'Company'){
		enableDisableCompany(true);
	
	}else if(element == 'Account'){
		enableDisableAccount(true);
	
	}else if(element == 'Security'){
		enableDisableSecurity(true);
	}
		

	fieldsToChange = fields.split(",");
	var num = fieldsToChange.length;
	
	for(i=0; i<num; i++){
		document.getElementById(fieldsToChange[i]).className = 'notEditable';
		document.getElementById(fieldsToChange[i]).readOnly = true;
		document.getElementById(fieldsToChange[i]).value=document.getElementById(fieldsToChange[i]).defaultValue;
		document.getElementById('n2').style.display= 'none';
		document.getElementById('n1').style.display= 'inline';
		isValid2(fieldsToChange[i]);
		
	}
	if(element=='Company' || element == 'Account'){
		$('#extra'+element).html('<td></td><td></td>');
	}
	if(element == 'SecurityQuestion'){
		document.getElementById('Question').disabled = true;
		$('#extra'+element).html('<td></td><td></td>');
	}
	$('#edit'+element).html('<input type="button" class="editBtn" name="edit" id="edit" onclick="editFields2(\''+element+'\',\''+fields+'\',\''+save+'\',\''+cancel+'\',\''+edit+'\',\''+tdTitle+'\',\''+msg+'\',\''+companyMsg+'\',\''+uID+'\');" value="'+edit+'" />');
}
