function validate(type, input, output, required) {
	var valid_email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
	var valid_number = /^\+?[0-9 ()-]+[0-9]$/ ;

if (type=='number') {
	if (!valid_number.test(input.value)) { 
		style = "error" ;
		message("Invalid number.", output, style) ;
	}
	if (valid_number.test(input.value)) {
		style = "success" ;
		message("Ok!", output, style) ;
	}
}

if (type=="email") {
	if (!valid_email.test(input.value)) {
		style= "error" ;
		message("Invalid e-mail.", output, style) ;
	}
	if (valid_email.test(input.value)) {
		style = "success" ;
		message("Ok!", output, style) ;
	}
}
return true;
}

function message(message, output, style) {
	var element = document.getElementById(output) ;
	element.firstChild.nodeValue = message;
	element.className = style;

// document.getElementById(output).firstChild.nodeValue = message ;
// document.getElementById(output).firstChild.className = style ;

	return true;
}

function validate_onsubmit() {
errors = "There were some errors:\n"; 
num_errors = 0;
var valid_email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
var valid_number = /^\+?[0-9 ()-]+[0-9]$/ ;
if (!document.forms.get_quote.name.value) { errors+='You did not enter a name.\n'; num_errors +=1 ; }
if (!valid_email.test(document.forms.get_quote.email.value)) { errors+='E-mail address is invalid.\n'; num_errors += 1 ;}
if (!valid_number.test(document.forms.get_quote.number.value)) { errors+='Phone number is invalid.\n'; num_errors += 1 ;}
 if (num_errors != 0) { 
	alert(errors); 
 }
return (num_errors==0) ;

}

    function checkForZero(field) {
        if (field.value == 0 || field.value.length == 0) {
            alert ("This field can't be 0!");
            field.focus(); }
        else
	    calculatePayment(field.form);
    }

    function cmdCalc_Click(form) {
        if (form.price.value == 0 || form.price.value.length == 0) {
            alert ("The Price field can't be 0!");
            form.price.focus(); }
        else if (form.ir.value == 0 || form.ir.value.length == 0) {
            alert ("The Interest Rate field can't be 0!");
            form.ir.focus(); }
        else if (form.term.value == 0 || form.term.value.length == 0) {
            alert ("The Term field can't be 0!");
            form.term.focus(); }
        else
            calculatePayment(form);
    }

    function calculatePayment(form) {
        princ = form.price.value - form.dp.value;
        intRate = (form.ir.value/100) / 12;
        months = form.term.value * 12;
	document.getElementById('principle').firstChild.nodeValue = "$" + princ;
	document.getElementById('payments').firstChild.nodeValue = months;
	document.getElementById('pmt').firstChild.nodeValue = "$" + Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
//	form.payments.value = months;
    }


function select_detail() {
if (document.getElementById('get_quote').detail[0].checked) {
	document.getElementById('choose').style.visibility = 'hidden'; 
	document.getElementById('container_purpose').style.visibility = 'hidden'; 
	document.getElementById('refinance').style.visibility = 'hidden'; 
	document.getElementById('buy').style.visibility = 'hidden'; 
	document.getElementById('low_detail_submit').style.visibility = 'visible'; 
	document.getElementById('get_quote_content').style.height = 220 + 'px' ;
}

if (document.getElementById('get_quote').detail[1].checked) {
	document.getElementById('choose').style.visibility = 'visible'; 
	document.getElementById('container_purpose').style.visibility = 'visible'; 
	document.getElementById('get_quote').loan_purpose.options[0].selected = true; 
	document.getElementById('low_detail_submit').style.visibility = 'hidden'; 
	document.getElementById('get_quote_content').style.height = 250 + 'px' ;
}
}

function select_purpose() {
if (document.getElementById('get_quote').loan_purpose.options[document.getElementById('get_quote').loan_purpose.selectedIndex].value == 'Refinance') {
	document.getElementById('refinance').style.visibility = 'hidden';
	document.getElementById('buy').style.visibility = 'hidden';
	document.getElementById('refinance').style.visibility = 'visible';
	document.getElementById('choose').style.visibility = 'hidden';
	document.getElementById('get_quote_content').style.height = 366 + 'px' ;
}

if (document.getElementById('get_quote').loan_purpose.options[document.getElementById('get_quote').loan_purpose.selectedIndex].value == 'Buy') {
	document.getElementById('refinance').style.visibility = 'hidden';
	document.getElementById('buy').style.visibility = 'hidden';
	document.getElementById('buy').style.visibility = 'visible';
	document.getElementById('choose').style.visibility = 'hidden';
	document.getElementById('get_quote_content').style.height = 326 + 'px' ;
}


if (document.getElementById('get_quote').loan_purpose.options[document.getElementById('get_quote').loan_purpose.selectedIndex].value == 'Purpose of loan') {
	document.getElementById('choose').style.visibility = 'visible';
	document.getElementById('refinance').style.visibility = 'hidden';
	document.getElementById('buy').style.visibility = 'hidden';
	document.getElementById('get_quote_content').style.height = 250 + 'px' ;
}
}
