/* printing and form functions */

function printArticle(url)
{
	window.open(url,'ToyotaConnections','width=660,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no');
}

/* clears the default text in input boxes */
function clearText(element, text)
{
	if (element.value == text) {
		element.value = "";
	}
}

/* clears the default text in textarea*/
function clearTextarea(element, text)
{
	if (element.innerHTML == text) {
		element.innerHTML = "";
	}
}

/* form that links to setbuyatoyota */
function checkOffersLink()
{
	var vehicleModel = jQuery("input[name='seriesId']:checked", '#offersForm').val()
	if (vehicleModel == 0) {
		jQuery('#offersForm').attr('action', 'http://www.setbuyatoyota.com/lobby/');
	} else {
		// reset the link if it was changed previously
		jQuery('#offersForm').attr('action', 'http://www.setbuyatoyota.com/AdLandingPages/VehicleLander.aspx');
	}
}

/* wrapper for search textbox - need to set variable for validation */
var searchBoxInteraction = false;
function clearSearch(element, text)
{
	clearText(element, text);
	searchBoxInteraction = true;
}

/* validates the search to ensure the user has entered a search term */
function validateSearch(element)
{
	var searchBoxText = jQuery('#s').val();
		
	if (searchBoxInteraction == true && searchBoxText != '') {
		return true;
	} else {
		alert('Please enter a search term.');
		return false;
	}
}


// custom validators for jquery validate

// US phone number - no separators, or '-' or '.'
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})(-|\.)?[2-9]\d{2}(-|\.)?\d{4}$/);
}, "Please specify a valid phone number");

// US zip code
jQuery.validator.addMethod("zipcode", function(zip_code, element) {
    zip_code = zip_code.replace(/\s+/g, ""); 
	return this.optional(element) || zip_code.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
}, "Please specify a valid zip code");

// US VIN Number
jQuery.validator.addMethod("vin", function(vin, element) {
    vin = vin.replace(/\s+/g, ""); 
	return this.optional(element) || vin.match(/^([A-Z]{3}|\d{3})[A-Z]{2}\d{2}([A-Z]{1}|\d{1})([\d{1}|X{1})([A-Z]+\d+|\d+[A-Z]+)\d{5}$/);
}, "Please specify a valid VIN Number");

// Multiple Emails
jQuery.validator.addMethod("multiEmail", function(emails, element) {
	return this.optional(element) || validateMultipleEmails(emails);
}, "Please specify a valid email address or addresses");

function validateEmail(field) {
    var regex=/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
    return (regex.test(field)) ? true : false;
}

function validateMultipleEmails(value) {
    var result = value.split(",");
    for(var i = 0;i < result.length;i++)
    if(!validateEmail(result[i])) 
            return false;               
    return true;
}




