// ***********************************************
//  ONLOAD FORM FIELD FOCUS
// ***********************************************
//  focuses first form element
// ***********************************************
jQuery(document).ready(function() {
	if (!jQuery("#first_field")) return false;
	jQuery("#first_field").focus();
});


/***************************************************************/
/******** Select() all inputs *********/
/***************************************************************/
jQuery(document).ready(function() {

	var frmFields = jQuery('input');
	frmFields.focus( function() { this.select(); } );
	
});


/***************************************************************/
/******** Select() all inputs *********/
/***************************************************************/
jQuery(document).ready(function() {
	var owedField = jQuery('#owe_tradein');
	
	if ( owedField ) { 
		owedField.attr("value", function() { return this.value + ('$'); });
	} else {
		//frmFields.focus( function() { this.select(); } );
	}
});


/***************************************************************/
/******** CHARACTER COUNTER AND AUTOEXPANDING TEXTAREA *********/
/***************************************************************/
var mce = {};

mce.NUMBER_OF_CONTROL_ROWS = 3;
mce.MAX_NUMBER_OF_CONTROL_ROWS = 12;

mce.init = function() {};

mce.counter = function(boxType,thisField,cntfield,maxlimit)
{	
	switch(boxType) {
		case "multiText":
			var textPercentBox = (maxlimit)*.95; // this converts to 5% characters left
			cntfield.style.visibility='visible';
			
			thisField.onblur = function() {
				cntfield.style.visibility='hidden';	
				jQuery(this).removeClass('focus_field');
			}
			
			// colorize 5 percent warning
			if (thisField.value.length >= textPercentBox) {
				cntfield.style.color='#C60000';
				cntfield.style.fontWeight='bold';
			} else if (thisField.value.length <= textPercentBox)  {
				cntfield.style.color='';	
				cntfield.style.fontWeight='';	
			}
			// char counter
			if (thisField.value.length > maxlimit) {
				thisField.value = thisField.value.substring(0, maxlimit); // if too long...trim it!
			} else {
				cntfield.value = maxlimit - thisField.value.length; // otherwise, update 'characters left' counter
			}
			mce.autoExpand(thisField, mce.NUMBER_OF_CONTROL_ROWS);
		break;
		case "inputText":
			var inputPercentBox	= (maxlimit)*.95; // this converts to 5% characters left
			cntfield.style.visibility='visible';
			
			thisField.onblur = function() {
				cntfield.style.visibility='hidden';	
				jQuery(this).removeClass('focus_field');

			}
			
			// colorize 5 percent warning
			if (thisField.value.length >= inputPercentBox) {
				cntfield.style.color='#C60000';
				cntfield.style.fontWeight='bold';
			} else if (thisField.value.length <= inputPercentBox)  {
				cntfield.style.color='';
				cntfield.style.fontWeight='';
			}
			// char counter
			if (thisField.value.length > maxlimit) {
				thisField.value = thisField.value.substring(0, maxlimit); // if too long...trim it!
			} else {
				cntfield.value = maxlimit - thisField.value.length; // otherwise, update 'characters left' counter
			} 		
		break;
	}
}


mce.autoExpand = function(t,minrows) 
{
	a = t.value.split('\n');
	b = 0;
	
	for (x=0;x < a.length; x++) {
		if (a[x].length >= t.cols) b += Math.floor(a[x].length/t.cols);
	}
	 
	b += a.length;
	//grow the box up to 13 rows if content increases
	if (t.rows < mce.MAX_NUMBER_OF_CONTROL_ROWS) {
		//jQuery(t).addClass('noscroll');
		for (i=0;i < t.clientHeight; i++) {
			if (t.scrollHeight > t.clientHeight) {
				t.rows++;
				t.scrollBottom = 0;
			}
			if (t.rows >= mce.MAX_NUMBER_OF_CONTROL_ROWS){ // fixes bug where scrollbar doesn't appear after pasting more lines than the max # of rows
				t.rows = mce.MAX_NUMBER_OF_CONTROL_ROWS;
				
			}
		}
	}	
	/*if (t.rows == mce.MAX_NUMBER_OF_CONTROL_ROWS) {
		this was used to show scrollbar in IE -- jQuery(t).removeClass('noscroll');
	}*/
	
	// do not shrink textarea more than the default textarea attribute "minrows"
	if (b < minrows) {
		t.rows = minrows;
		return false;
	}
	//decrease rows as you delete content rows
	for (i=0;i < t.clientHeight; i++) {
		if (b < t.rows) {
			t.rows = t.rows - 1;
		}
	}
}

jQuery(document).ready( mce.init )


/***************************************************************/
/********* INQUIRY FORM *********/
/***************************************************************/
var inquiryForm = {};

inquiryForm.init = function () {
	inquiryForm.toggles();
	inquiryForm.validateForm();
}

inquiryForm.toggles = function() {
  	jQuery(".refer_toggle").hide();
  	jQuery("#tradein-info").css("display","none");
	  // Add onclick handler to checkbox w/id checkme
	 jQuery("#referYes").click(function() {
	  // If checked
		  //show the hidden div
			jQuery(".refer_toggle").show();
	  }
	  );
	 jQuery("#referNo").click(function() {
	  // If checked
		  //show the hidden div
			jQuery(".refer_toggle").hide();
	  }
	);
	
	 jQuery("#tradeYes").click(function() {
	  // If checked
		  //show the hidden div
			jQuery("#tradein-info").css("display","block");
	  }
	  );
	 jQuery("#tradeNo").click(function() {
	  // If checked
		  //show the hidden div
			jQuery("#tradein-info").css("display","none");
	  }
	);
	/***************************************************************/
	/********* EMAIL VALIDATOR *********/
	/***************************************************************/
	inquiryForm.validateForm = function(){
		jQuery("#submit_test_drive").click(function(){					   				   
			jQuery(".error").hide();
			var hasError = false;
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			
			var nameVal = jQuery("#name").val();
			if(nameVal == '') {
				jQuery("#name").after('<span class="error">Enter your full name.</span>');
				hasError = true;
			}
			var emailFromVal = jQuery("#email").val();
			if(emailFromVal == '') {
				jQuery("#email").after('<span class="error">Email address cannot be blank.</span>');
				hasError = true;
			} else if (!emailReg.test(emailFromVal)) {	
				jQuery("#email").after('<span class="error">Enter a valid email address to contact you about your test drive.</span>');
				hasError = true;
			}
			
			var phoneVal = jQuery("#phone").val();
			if(phoneVal == '') {
				jQuery("#phone").after('<span class="error">Enter phone number to call you about your test drive.</span>');
				hasError = true;
			}
			
			var zipVal = jQuery("#zipcode").val();
			if (zipVal == '') {
				jQuery("#zipcode").after('<span class="error">Enter a valid zipcode.</span>');
				hasError = true;
			}
			
			var subjectVal = nameVal;
			
			var carVal = jQuery("#vehicle").val();
			if(carVal == '') {
				jQuery("#vehicle").after('<span class="error">What car would you like to test drive?</span>');
				hasError = true;
			}
			
			
			if(hasError == false) {
				jQuery(this).hide();
				jQuery("#test_drive p.buttons").append('<img src="/images/template/loading.gif" alt="Loading" id="loading" /><div style="width: 10px; background-color: gray;">loading</div>');
				
				jQuery.post("http://www.diamond-motors.com/financing/test_drive_submittal.php",
					{ emailTo: emailToVal, emailFrom: emailFromVal, subject: subjectVal, message: carVal },
						function(data){
							jQuery("#test_drive").slideUp("normal", function() {				   
								jQuery("#test_drive").before('<h1>Success</h1><p>Your email was sent.</p>');											
							});
						}
					 );
			}
			
			return false;
		});						   
	};

	 
  };
jQuery(document).ready( inquiryForm.init )





/***************************************************************/
/******** First (555) 222-3333 Formatting *********/
/***************************************************************/
<!-- Begin
var n;
var p;
var p1;
function ValidatePhone(){
p=p1.value
if(p.length==3){
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+")";
	}
	document.inquiry_form.mainphone.value="";
	document.inquiry_form.mainphone.value=pp;
}
if(p.length>3){
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1){
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+")"
		p31=p.substring(4,l30);
		pp=p30+p31;
		document.inquiry_form.mainphone.value="";
		document.inquiry_form.mainphone.value=pp;
	}
	}
if(p.length>5){
	p11=p.substring(d1+1,d2);
	if(p11.length>3){
	p12=p11;
	l12=p12.length;
	l15=p.length
	p13=p11.substring(0,3);
	p14=p11.substring(3,l12);
	p15=p.substring(d2+1,l15);
	document.inquiry_form.mainphone.value="";
	pp="("+p13+")"+p14+p15;
	document.inquiry_form.mainphone.value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('')==-1){
		p17=p.substring(d2+1,d2+4);
		p18=p.substring(d2+4,l16);
		p19=p.substring(0,d2+1);
	pp=p19+p17+"-"+p18;
	document.inquiry_form.mainphone.value="";
	document.inquiry_form.mainphone.value=pp;
	}
}
//}
setTimeout(ValidatePhone,100)
}

function getMainPhone(m) {
	n=m.name;
	p1=m
	ValidatePhone()
}

function testphone(obj1){
	p=obj1.value
	//alert(p)
	p=p.replace("(","")
	p=p.replace(")","")
	p=p.replace("-","")
	p=p.replace("-","")
	//alert(isNaN(p))
	if (isNaN(p)==true){
		alert("Check phone");
		return false;
	}
}
//  End -->


/***************************************************************/
/******** Second (555) 222-3333 Formatting *********/
/***************************************************************/
<!-- Begin
var nc;
var pc;
var pc1;
function ValidateMobilePhone(){
pc=pc1.value
if(pc.length==3){
	pcp=pc;
	d4=pc.indexOf('(')
	d5=pc.indexOf(')')
	if(d4==-1){
		pcp="("+pcp;
	}
	if(d5==-1){
		pcp=pcp+")";
	}
	document.inquiry_form.mobilephone.value="";
	document.inquiry_form.mobilephone.value=pcp;
}
if(pc.length>3){
	d1=pc.indexOf('(')
	d2=pc.indexOf(')')
	if (d2==-1){
		l30=pc.length;
		pc30=pc.substring(0,4);
		pc30=pc30+")"
		pc31=pc.substring(4,l30);
		pcp=pc30+pc31;
		document.inquiry_form.mobilephone.value="";
		document.inquiry_form.mobilephone.value=pcp;
	}
	}
if(pc.length>5){
	pc11=pc.substring(d1+1,d2);
	if(pc11.length>3){
	pc12=pc11;
	l12=pc12.length;
	l15=pc.length
	pc13=pc11.substring(0,3);
	pc14=pc11.substring(3,l12);
	pc15=pc.substring(d2+1,l15);
	document.inquiry_form.mobilephone.value="";
	pcp="("+pc13+")"+pc14+pc15;
	document.inquiry_form.mobilephone.value=pcp;
	}
	l16=pc.length;
	pc16=pc.substring(d2+1,l16);
	l17=pc16.length;
	if(l17>3&&pc16.indexOf('')==-1){
		pc17=pc.substring(d2+1,d2+4);
		pc18=pc.substring(d2+4,l16);
		pc19=pc.substring(0,d2+1);
	pcp=pc19+pc17+"-"+pc18;
	document.inquiry_form.mobilephone.value="";
	document.inquiry_form.mobilephone.value=pcp;
	}
}
//}
setTimeout(ValidateMobilePhone,100)
}

function getMobilePhone(mc) {
	nc=mc.name;
	pc1=mc
	ValidateMobilePhone()
}

//  End -->


/***************************************************************/
/******** Third (555) 222-3333 Formatting *********/
/***************************************************************/
<!-- Begin
var nb;
var pb;
var pb1;
function ValidateWorkPhone(){
pb=pb1.value
if(pb.length==3){
	pbp=pb;
	d4=pb.indexOf('(')
	d5=pb.indexOf(')')
	if(d4==-1){
		pbp="("+pbp;
	}
	if(d5==-1){
		pbp=pbp+")";
	}
	document.inquiry_form.workphone.value="";
	document.inquiry_form.workphone.value=pbp;
}
if(pb.length>3){
	d1=pb.indexOf('(')
	d2=pb.indexOf(')')
	if (d2==-1){
		l30=pb.length;
		pb30=pb.substring(0,4);
		pb30=pb30+")"
		pb31=pb.substring(4,l30);
		pbp=pb30+pb31;
		document.inquiry_form.workphone.value="";
		document.inquiry_form.workphone.value=pbp;
	}
	}
if(pb.length>5){
	pb11=pb.substring(d1+1,d2);
	if(pb11.length>3){
	pb12=pb11;
	l12=pb12.length;
	l15=pb.length
	pb13=pb11.substring(0,3);
	pb14=pb11.substring(3,l12);
	pb15=pb.substring(d2+1,l15);
	document.inquiry_form.workphone.value="";
	pbp="("+pb13+")"+pb14+pb15;
	document.inquiry_form.workphone.value=pbp;
	}
	l16=pb.length;
	pb16=pb.substring(d2+1,l16);
	l17=pb16.length;
	if(l17>3&&pb16.indexOf('')==-1){
		pb17=pb.substring(d2+1,d2+4);
		pb18=pb.substring(d2+4,l16);
		pb19=pb.substring(0,d2+1);
	pbp=pb19+pb17+"-"+pb18;
	document.inquiry_form.workphone.value="";
	document.inquiry_form.workphone.value=pbp;
	}
}
//}
setTimeout(ValidateWorkPhone,100)
}

function getWorkPhone(mb) {
	nb=mb.name;
	pb1=mb
	ValidateWorkPhone()
}

//  End -->
