var field={
	// this.src='images/button_submit_disable.jpg';this.disabled=true;if(Submitting()){this.form.submit()}else{this.disabled=false;this.src='images/button_submit.jpg';}
	automotive: function(){
		//$p('submit').src= "images/button_submit.jpg";
		$p('submit').src= "images/button_submit_disable.jpg";
		$p('submit').disabled= true;
		var car_new= $p('car_new');
		var car_year= $p('car_year');
		var car_year_purchased= $p('car_year_purchased');
		var car_purchased_price= $p('car_purchased_price');
		var car_engine_type= $p('car_engine_type');
		var car_wd= $p('car_wd');
		var car_turbo= $p('car_turbo');
		var car_cylinders= $p('car_cylinders');

		result= 0;
		result= field.common();

		result+= field.check(car_new, "isempty_sel");
		result+= field.check(car_year, "isempty_sel");
		//result+= field.check(car_year_purchased, "isempty_sel");
		//result+= field.check(car_purchased_price, "isnumeric");
		//result+= field.check(car_engine_type, "isempty_sel");
		//result+= field.check(car_wd, "isempty_sel");
		//result+= field.check(car_turbo, "isempty_sel");
		//result+= field.check(car_cylinders, "isempty_sel");

		if(result==0){
			var form= $p('usedForm');
			form.submit();
		}else{
			alert("The red highlighted fields are incorrect.");
			$p('submit').src= "images/button_submit.jpg";
			$p('submit').disabled= false;
		}
	},

	tax: function(){
		var tax_lien_yes= $p('tax_lien_yes');
		var tax_lien_no= $p('tax_lien_no');

		result= 0;
		result= field.common();
		if(tax_lien_yes.checked== false && tax_lien_no.checked== false){
			field.showError(tax_lien_yes, 'error');
			result++;
		}else{
			field.showError(tax_lien_yes, 'good');
			result+= 0;
		}

		if(result==0){
			var form= $p('loanform');
			form.submit();
		}else{
			alert("The red highlighted fields are incorrect.");
		}
	},

	check: function(ele, con, length){
		result= 0;
		switch (con) {
			case "isempty":
    			if(validate.isEmpty(ele.value)){
					field.showError(ele, 'error');
					result= 1;
				}else{
					field.showError(ele, 'good');
					result= 0;
				}

            break;

            case "isempty_sel":
    			if(validate.isEmpty(getSelectedValueObj(ele))){
					field.showError(ele, 'error');
					result= 1;
				}else{
					field.showError(ele, 'good');
					result= 0;
				}

            break;

            case "isnumeric_length":
            	if(!validate.isNumeric(ele.value) || ele.value.length != length){
					field.showError(ele, 'error');
					result= 1;
				}else{
					field.showError(ele, 'good');
					result= 0;
				}

			break;

            case "isnumeric":
            	if(!validate.isNumeric(ele.value)){
					field.showError(ele, 'error');
					result= 1;
				}else{
					field.showError(ele, 'good');
					result= 0;
				}

			break;

            case "ismail":
            	if(!validate.isMail(ele.value)){
					field.showError(ele, 'error');
					result= 1;
				}else{
					field.showError(ele, 'good');
					result= 0;
				}

			break;

			default:
            	return "child 2";

            	break;
		}

		return result;
	},

	showError: function(ele,act){
		switch (act) {
			case "error":
				ele.style.background= "#FF0000";

	        break;

	        case "good":
				ele.style.background= "#FFF";

	        break;

	        default:

	        break;
		}
	},

	setModel: function(){
		// Add used car makes
	    var makeSel = document.usedForm.usedMakeSelect;
	    makeSel.options.length = 0;
	    var optionNum = 0;
	    for (ii in makesById){
	        var makeRecord = makesById[ii];
	        var option = new Option(makeRecord[1], makeRecord[1]);
	        makeSel.options[optionNum] = option;
	        optionNum++;
	    }

	    selectUsedMake(makeSel.value);
	},

	common: function(){
		var name= $p('name');
		var lastname= $p('lastname');
		var emailaddress= $p('emailaddress');
		var address= $p('address');
		var city= $p('city');
		var st= $p('st');
		var zip= $p('zip');
		var homephone_area= $p('homephone_area');
		var homephone_pref= $p('homephone_pref');
		var homephone_suff= $p('homephone_suff');
		var best_time_to_call= $p('best_time_to_call');

		result= 0;
		result+= field.check(name, "isempty");
		result+= field.check(lastname, "isempty");
		result+= field.check(emailaddress, "ismail");
		result+= field.check(address, "isempty");
		result+= field.check(city, "isempty");
		result+= field.check(st, "isempty_sel");
		result+= field.check(zip, "isnumeric_length", 5);

    	if((!validate.isNumeric(homephone_area.value) || homephone_area.value.length != 3) || (!validate.isNumeric(homephone_pref.value) || homephone_pref.value.length != 3) || (!validate.isNumeric(homephone_suff.value) || homephone_suff.value.length != 4)){
			field.showError(homephone_area, 'error');
			result+= 1;
		}else{
			field.showError(homephone_area, 'good');
			result+= 0;
		}

		return result;
	},

	contact: function(){
		var name= $p('name');
		var lastname= $p('lastname');
		var email= $p('email');
		var comment= $p('comment');

		result= 0;
		result+= field.check(name, "isempty");
		result+= field.check(lastname, "isempty");
		result+= field.check(email, "ismail");
		result+= field.check(comment, "isempty");

		if(result==0){
			var form= $p('contact');
			form.submit();
		}else{
			alert("The red highlighted fields are incorrect.");
		}
	},

	homefinance: function(){
		var state= $p('state');
		/*var loanpurpose= $p('loanpurpose');
		var propertytype= $p('propertytype');
		var currentvalue= $p('currentvalue');
		var first_balance= $p('first_balance');
		var rate= $p('rate');
		var add_cash= $p('add_cash');*/

		result= 0;
		//result= field.common();

		result+= field.check(state, "isempty_sel");
		/*result+= field.check(loanpurpose, "isempty_sel");
		result+= field.check(propertytype, "isempty_sel");
		result+= field.check(currentvalue, "isempty_sel");
		result+= field.check(first_balance, "isempty_sel");
		result+= field.check(rate, "isempty_sel");
		result+= field.check(add_cash, "isempty_sel");
		*/
		if(result==0){
			var form= $p('usedForm');
			form.submit();
		}else{
			alert("The red highlighted fields are incorrect.");
		}
	},

	debt: function(){
		$p('submit').disabled= true;
		var result= 0;
		result+= field.check($p('debt_amount'), "isempty_sel");
		result+= field.check($p('behind'), "isempty_sel");
		result+= field.check($p('name'), "isempty");
		result+= field.check($p('lastname'), "isempty");
		result+= field.check($p('zip'), "isnumeric_length", 5);
		result+= field.check($p('emailaddress'), "ismail");
		result+= field.check($p('st'), "isempty_sel");
		// Home phone
		result+= field.check($p('homephone_area'), "isnumeric_length", 3);
		result+= field.check($p('homephone_pref'), "isnumeric_length", 3);
		result+= field.check($p('homephone_suff'), "isnumeric_length", 4);
		if(result==0){
			var form= $p('creditform');
			form.submit();

			return true;
		}else{
			$p('submit').disabled= false;
			return false;
		}
	},

	warranty: function(){
		$p('submit_but').disabled= true;
	    var result= 0;
	    result+= field.check($p('name'), "isempty");
	    result+= field.check($p('lastname'), "isempty");
	    result+= field.check($p('address'), "isempty");
	    result+= field.check($p('city'), "isempty");
	    result+= field.check($p('st'), "isempty_sel");
	    result+= field.check($p('zip'), "isnumeric_length", 5);
	    // Home phone
	    result+= field.check($p('homephone_area'), "isnumeric_length", 3);
	    result+= field.check($p('homephone_pref'), "isnumeric_length", 3);
	    result+= field.check($p('homephone_suff'), "isnumeric_length", 4);

	    result+= field.check($p('emailaddress'), "ismail");
	    result+= field.check($p('usedMakeSelect'), "isempty_sel");
	    result+= field.check($p('usedModelSelect'), "isempty_sel");
	    result+= field.check($p('car_year'), "isempty_sel");
	    result+= field.check($p('car_mile'), "isempty");
	    result+= field.check($p('car_turbo'), "isempty_sel");
	    result+= field.check($p('car_engine_type'), "isempty_sel");
	    result+= field.check($p('car_wd'), "isempty_sel");
	    result+= field.check($p('factory_warranty'), "isempty_sel");

	    if(result==0){
	        $p('usedForm').submit();

	        return true;
	    }else{
	    	$p('submit_but').disabled= false;
	        return false;
	    }
	},

	clearPhone: function(action){
	    switch (action){
	        case "home":
	            $p('homephone_area').value= "";
	            $p('homephone_pref').value= "";
	            $p('homephone_suff').value= "";
	            $p('homephone_area').focus();
	        break;

	        case "alt":
	            $p('workphone_area').value= "";
	            $p('workphone_pref').value= "";
	            $p('workphone_suff').value= "";
	            $p('workphone_ex').value= "";
	            $p('workphone_area').focus();
	        break;
	    }
	},
	prepostLead: function(ele, leadtype){
		ele.src='images/button_submit_disable.jpg';
		ele.disabled=true;

		var form= ele.form;
		var parameters= {
        };
		var inputs= form.getElementsByTagName("input");
		for(i= 0; i< inputs.length; i++){
			var ch= inputs[i];
			if(ch.type=='text' || ch.name=='vendorid'){
				ch.className= "normalfield";
				parameters[ch.name]= ch.value;
			}
			if(ch.type=='radio' && ch.checked==true)
				parameters[ch.name]= ch.value;
			if(ch.type=='checkbox' && ch.checked==true)
				parameters[ch.name]= ch.value;
		}
		var select= form.getElementsByTagName("select");
		for(k= 0; k< select.length; k++){
			var ch= select[k];
			ch.className= "normalfield";
			parameters[ch.name]= getSelectedValueObj(ch);
		}

		if(parameters['homephone_area'] && parameters['homephone_pref'] && parameters['homephone_suff'])
			parameters['homephone']= parameters['homephone_area']+parameters['homephone_pref']+parameters['homephone_suff'];
		if(parameters['workphone_area'] && parameters['workphone_pref'] && parameters['workphone_suff'] && parameters['workphone_ext'])
			parameters['workphone']= parameters['workphone_area']+parameters['workphone_pref']+parameters['workphone_suff']+' '+parameters['workphone_ext'];
		if(parameters['BirthDay'] && parameters['BirthMonth'] && parameters['BirthYear'])
			parameters['BirthDay']= parameters['BirthYear']+'-'+parameters['BirthMonth']+'-'+parameters['BirthDay'];
        parameters['leadtype']= leadtype;
    	https.get_object({url: "modules/checkpost.php?action=home", method: "post", parameters: parameters, arguments: [ele,form], callback: field.prepostLeadResult});
		//ele.form.submit();*/
	},
	prepostLeadResult: function(result, ele, form){
		if (result === null) {  //  error
            alert("Erorr!");
        } else {
        	if(result.result=='error'){
        		alert("Your Vendor ID is Not Valid.");
        		ele.src='images/button_submit.jpg';
				ele.disabled=false;
        	}
        	var count= 0;
        	for(i= 0; i< result.fields.length; i++){
        		var ch= result.fields[i];
        		if(form[ch.name]){
        			var e= form[ch.name];
        			e.className= "errorfield";
        			count++;
        		}else{
        			if(ch.name=='homephone'){
        				if(form.homephone_area) form.homephone_area.className= "errorfield";
        				if(form.homephone_pref) form.homephone_pref.className= "errorfield";
        				if(form.homephone_suff) form.homephone_suff.className= "errorfield";
        				count++;
        			}
        			if(ch.name=='workphone'){
        				if(form.workphone_area) form.workphone_area.className= "errorfield";
        				if(form.workphone_pref) form.workphone_pref.className= "errorfield";
        				if(form.workphone_suff) form.workphone_suff.className= "errorfield";
        				count++;
        			}
        		}
        	}
        	if(count==0){
        		form.submit();
        	}else{
        		alert("There are error(s). ");
        		ele.src='images/button_submit.jpg';
				ele.disabled=false;
        	}
        }
	},
	checkLoanmod: function(ele, call){
		ele.disabled= true;
		if(call){
			$p('call').value= 'yes';
		}
		result= 0;
		result+= field.check($p('currentvalue'), "isempty_sel");
		result+= field.check($p('first_balance'), "isempty_sel");
		result+= field.check($p('address'), "isempty");
		result+= field.check($p('city'), "isempty");
		result+= field.check($p('zip'), "isnumeric_length", 5);
		result+= field.check($p('st'), "isempty_sel");
		result+= field.check($p('loantype'), "isempty_sel");
		result+= field.check($p('behind'), "isempty_sel");
		result+= field.check($p('hardship'), "isempty_sel");

		result+= field.check($p('name'), "isempty");
		result+= field.check($p('lastname'), "isempty");
		result+= field.check($p('homephone_area'), "isnumeric_length", 3);
		result+= field.check($p('homephone_pref'), "isnumeric_length", 3);
		result+= field.check($p('homephone_suff'), "isnumeric_length", 4);
		result+= field.check($p('emailaddress'), "ismail");
		if(result>0){
			ele.disabled= false;
	        alert("There is incorrect field(s).");
		}else{
			var form= getForm(ele);
			if(form){
				form.submit();
			}
			else alert('Form erorr.');
		}
		return false;
	},
	foreclosure: function(ele){
		$p('imageField').disabled= true;
		result= 0;
		result+= field.check($p('currentvalue'), "isempty_sel");
		result+= field.check($p('first_balance'), "isempty_sel");
		result+= field.check($p('st'), "isempty_sel");
		result+= field.check($p('loantype'), "isempty_sel");
		result+= field.check($p('behind'), "isempty_sel");

		result+= field.check($p('name'), "isempty");
		result+= field.check($p('lastname'), "isempty");
		result+= field.check($p('homephone_area'), "isnumeric_length", 3);
		result+= field.check($p('homephone_pref'), "isnumeric_length", 3);
		result+= field.check($p('homephone_suff'), "isnumeric_length", 4);
		result+= field.check($p('emailaddress'), "ismail");
		if(result>0){
			$p('imageField').disabled= false;
	        alert("There is incorrect field(s).");
		}else
			ele.form.submit();
	},
	checkTax2: function(ele){
		$p('imageField').disabled= true;
		result= 0;
		result+= field.check($p('name'), "isempty");
		result+= field.check($p('lastname'), "isempty");
		result+= field.check($p('address'), "isempty");
		result+= field.check($p('zip'), "isnumeric_length", 5);
		result+= field.check($p('city'), "isempty");
		result+= field.check($p('homephone_area'), "isnumeric_length", 3);
		result+= field.check($p('homephone_pref'), "isnumeric_length", 3);
		result+= field.check($p('homephone_suff'), "isnumeric_length", 4);
		result+= field.check($p('emailaddress'), "ismail");
		if(result>0){
			$p('imageField').disabled= false;
	        alert("There is incorrect field(s).");
		}else
			ele.form.submit();
	},
	loanmod: function(ele){
		//$p('imageField').src= "images/button_submit_disable.jpg";
		$p('imageField').disabled= true;
		$p('imageField').value= 'Posting...';
		var eles= $p('loancontent').getElementsByTagName('input');
		for(var i= 0; i< eles.length; i++){
			var ch= eles[i];
			if(ch.type!='button')
				ch.style.backgroundColor= "#FFFFFF";
		}
		var eles= $p('loancontent').getElementsByTagName('select');
		for(var i= 0; i< eles.length; i++){
			var ch= eles[i];
			ch.style.backgroundColor= "#FFFFFF";
		}
		var notice_default= ($p('notice_default_yes').checked== true ? 'yes' : 'no');
		var bankruptcy= ($p('bankruptcy_yes').checked== true ? 'yes' : 'no');
		var parameters = {
			vendorid: $p('vendorid').value,
			currentvalue: getSelectedValueObj($p('currentvalue')),
			first_balance: getSelectedValueObj($p('first_balance')),
			address: $p('address').value,
			city: $p('city').value,
			zip: $p('zip').value,
			st: getSelectedValueObj($p('st')),
			loantype: getSelectedValueObj($p('loantype')),
			behind: getSelectedValueObj($p('behind')),
			notice_default: notice_default,
			hardship: getSelectedValueObj($p('hardship')),
			bankruptcy: bankruptcy,
			name: $p('name').value,
			lastname: $p('lastname').value,
			homephone: $p('homephone_area').value+$p('homephone_pref').value+$p('homephone_suff').value,
			workphone: $p('workphone_area').value+$p('workphone_pref').value+$p('workphone_suff').value,
			emailaddress: $p('emailaddress').value,
			comments: $p('comments').value,
			leadtype: $p('leadtype').value
        };
    	https.get_object({url: "lib/ajaxfields.php", parameters: parameters, arguments: [ele], callback: field.loanmodResult});
	},
	loanmodResult: function(result, ele){
		if (result === null) {  //  error
            alert("Erorr!");
        } else {
        	if(result.wrong_fields.length>0){
        		var message= "There are incorrect field(s):\n\r";
	        	for(var i= 0; i< result.wrong_fields.length; i++){
	        		var ch= result.wrong_fields[i];
					if($p(ch.name)){
						$p(ch.name).style.background= "#FF0000";
						message+= "Field: "+ch.name+"\t Response: "+ch.response+"\n\r";
					}
					if(ch.name=='homephone' || ch.name=='workphone'){
						$p(ch.name+"_area").style.background= "#FF0000";
						$p(ch.name+"_pref").style.background= "#FF0000";
						$p(ch.name+"_suff").style.background= "#FF0000";
					}
	        	}
	        	$p('imageField').value= '';
	        	$p('imageField').disabled= false;
	        	alert(message);
	        }else{
	        	/*if($p('counterform') && $p('vendorid').value=='6000'){
	        		$p('counterform').submit();
	        	}*/
	        	ele.form.submit();
	        }
        }
	}
}