$(document).ready( function() {

/**
 * Customize this function as you wish!
     * Add your validation below the email if.
     *
     * Change the behaviour of the plugin by editing this line:
     *  $(this).blur( function() { validateField(this) } );
     * To:
     *      $(this).keyup( function() { validateField(this) } );
     *
     *  You can find it around the line number 64.
 */
function validateField(field) { 
var error = false;
var errorMessage = "";
var bolPhoneError = false;
var tempErrorID = $(field).attr("id"); 
var strErrorWidth = 200;
// required fields
if ($(field).attr("class").indexOf("requiredField") != -1 && !error) { 
if (!$(field).val().length){
error = true;
errorMessage = errorMessage + "Ce champ doit &#234;tre renseign&#233;.<br />";
} 
}
// numeric fields
if ($(field).attr("class").indexOf("numericField") != -1 && !error) {
if (!/^[0-9]*$/.test($(field).val())){
error = true;
errorMessage = errorMessage + "Only numbers are allowed in this field.<br />";
} 
}
// phone numeric fields
if ($(field).attr("class").indexOf("phoneField") != -1 && !error) {
if($(field).attr("id").indexOf("phone2") > -1){
tempErrorID = "phone2Number";
tempcheckID = "phone2";
}else{
tempErrorID = "phoneNumber";
tempcheckID = "phone"; 
}

bolPhoneError = true;
bolNumberStarted = false;
if($("#"+tempcheckID+"AreaCode").val().length > 0 || $("#"+tempcheckID+"Prefix").val().length > 0 || $("#"+tempcheckID+"Number").val().length > 0){
bolNumberStarted = true;
}
if (!/^[0-9]*$/.test($("#"+tempcheckID+"AreaCode").val()) || ($("#"+tempcheckID+"AreaCode").val().length < 3 && bolNumberStarted)){
error = true; 
$("#"+tempcheckID+"AreaCode").addClass("formError"); 
}else{
$("#"+tempcheckID+"AreaCode").removeClass("formError");
}
if (!/^[0-9]*$/.test($("#"+tempcheckID+"Prefix").val()) || ($("#"+tempcheckID+"Prefix").val().length < 3 && bolNumberStarted)){
error = true; 
$("#"+tempcheckID+"Prefix").addClass("formError"); 
}else{
$("#"+tempcheckID+"Prefix").removeClass("formError");
}
if (!/^[0-9]*$/.test($("#"+tempcheckID+"Number").val()) || ($("#"+tempcheckID+"Number").val().length < 4 && bolNumberStarted)){
error = true; 
$("#"+tempcheckID+"Number").addClass("formError"); 
}else{
$("#"+tempcheckID+"Number").removeClass("formError");
}
if (error){
errorMessage = errorMessage + "Veuillez saisir un num&#233;ro de t&#233;l&#233;phone valide.<br />";
} 
}
// required phone numeric fields
if ($(field).attr("class").indexOf("reqPhoneField") != -1 && !error) {
bolPhoneError = true;
if($(field).attr("id").indexOf("phone2") > -1){
tempErrorID = "phone2Number";
tempcheckID = "phone2";
}else{
tempErrorID = "phoneNumber";
tempcheckID = "phone"; 
}
if (!/^[0-9]*$/.test($("#"+tempcheckID+"AreaCode").val()) || $("#"+tempcheckID+"AreaCode").val().length < 3){
error = true; 
$("#"+tempcheckID+"AreaCode").addClass("formError"); 
}else{
$("#"+tempcheckID+"AreaCode").removeClass("formError");
}
if (!/^[0-9]*$/.test($("#"+tempcheckID+"Prefix").val()) || $("#"+tempcheckID+"Prefix").val().length < 3){
error = true; 
$("#"+tempcheckID+"Prefix").addClass("formError"); 
}else{
$("#"+tempcheckID+"Prefix").removeClass("formError");
}
if (!/^[0-9]*$/.test($("#"+tempcheckID+"Number").val()) || $("#"+tempcheckID+"Number").val().length < 4){
error = true; 
$("#"+tempcheckID+"Number").addClass("formError"); 
}else{
$("#"+tempcheckID+"Number").removeClass("formError");
}
if (error){
errorMessage = errorMessage + "Veuillez saisir un num&#233;ro de t&#233;l&#233;phone valide.<br />";
} 
}
// characters (letters)
if ($(field).attr("class").indexOf("characterField") != -1 && !error) {
if (!/^[a-zA-Z&#246;&#214;&#228;&#196;&#229;&#197;]*$/.test($(field).val())){
error = true;
errorMessage = errorMessage + "Only letters are allowed in this field.<br />";
} 
}
// emails
if ($(field).attr("class").indexOf("emailField") != -1 && !error) {
if (!/^[a-zA-Z0-9']{1}([\._a-zA-Z0-9-']+)(\.[_a-zA-Z0-9-']+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test($(field).val())){
error = true;
errorMessage = errorMessage + $(field).val() +" n'est pas une adresse de courriel valide.<br />";
} 
}
// select box
if ($(field).attr("class").indexOf("reqSelected") != -1 && !error) {
if ($(field).val() == "null"){
error = true;
errorMessage = errorMessage + "Ce champ doit &#234;tre renseign&#233;.<br />";
} 
}

if (error) { 
if($("#"+tempErrorID+"errorBox").length > 0){
$("#"+tempErrorID+"errorBox>p").html(errorMessage);
}else{ 
if(bolPhoneError){
$("#"+tempErrorID).after("<span id=\""+ tempErrorID +"errorBox\" class=\"formErrorBox\"><p>"+ errorMessage +"</p></span>");
$("#"+tempErrorID+"errorBox").css("width",strErrorWidth);
}else{
$(field).after("<span id=\""+ tempErrorID +"errorBox\" class=\"formErrorBox\"><p>"+ errorMessage +"</p></span>");
$("#"+tempErrorID+"errorBox").css("width",$(field).width());
$(field).addClass("formError"); 
} 
}
} else {
$("#"+tempErrorID+"errorBox").remove();
if(!bolPhoneError)$(field).removeClass("formError");
} 
return !error;

}

$("form#contactForm").each( function() {
// handle submissions without filling any field
$(this).submit(function () { 
var validationError = false;
// for each field test it
$("input, select, textarea", this).each( function() {
if ($(this).attr("class") && $(this).attr("class").indexOf('notrequiredField') == -1) {
if (!validateField(this)) 
validationError = true;
}
}); 
if(validationError){
alert('Il y a une ou plusiers erreurs dans le formulaire sur cette page.\nVeuillez revoir les elements saisis.');
} 
return !validationError;
});

// handle changes on the fly
$("input, select, textarea", this).each( function() {
if ($(this).attr("class")) {
$(this).blur( function() { validateField(this) } );
// add event to policy number or address to be required
if($('div.customerService').length && ($(this).parent('div').attr('class') == 'policyNumber' || $(this).parent('div').attr('class') == 'addressBox' || $(this).parent('div').attr('class') == 'stateBox' || $(this).parent('div').attr('class') == 'zipBox')){
$(this).bind('blur',function(){
setPolicyHolderReq();
});

};
     }
});


});

$("form#doorHangerForm").each( function() {
// handle submissions without filling any field
$(this).submit(function () { 
var validationError = false;
// for each field test it
$("input, select, textarea", this).each( function() {
if ($(this).attr("class")) {
if (!validateField(this)) 
validationError = true;
}
}); 
if(validationError){
alert('Il y a une ou plusiers erreurs dans le formulaire sur cette page.\nVeuillez revoir les elements saisis.');
} 
return !validationError;
});

// handle changes on the fly
$("input, select, textarea", this).each( function() {
if ($(this).attr("class")) {
$(this).blur( function() { validateField(this) } );
     }
});
});


});

function setRequiredField(ctl){
if(ctl === "Other"){
$("div.comments").find('label').addClass("requiredCopy");
$("div.comments").find('textarea').addClass("requiredField");
}else{
$("div.comments").find('label').removeClass("requiredCopy");
$("div.comments").find('textarea').removeClass("requiredField");
}
};
function setPolicyHolderReq(){ 
var bolPolicyNumber = false;
var bolAddress = false;
$("form#contactForm").find('input, select').each( function() { 
if ($(this).parent('div').attr('class') == 'policyNumber' && $(this).val() != ''){
bolPolicyNumber = true;
}
if (($(this).parent('div').attr('class') == 'addressBox' || $(this).parent('div').attr('class') == 'zipBox') && $(this).attr('id').indexOf('address2') == -1 && $(this).val() != ''){
bolAddress = true;
}
if ($(this).parent('div').attr('class') == 'stateBox' && $(this).val() != 'null'){
bolAddress = true;
}

});
$("form#contactForm").find('input, select').each( function() { 
if(!bolPolicyNumber && !bolAddress){ 
if (($(this).parent('div').attr('class') == 'policyNumber' || $(this).parent('div').attr('class') == 'addressBox' || $(this).parent('div').attr('class') == 'zipBox') && $(this).attr('id').indexOf('address2') == -1){
$(this).prev('label').addClass("requiredCopy");
$(this).addClass("requiredField");
$(this).removeClass("notrequiredField");
}
if ($(this).parent('div').attr('class') == 'stateBox'){
$(this).prev('label').addClass("requiredCopy");
$(this).addClass("reqSelected");
$(this).removeClass("notrequiredField");
} 
$("#"+$(this).attr('id')+"errorBox").remove();
$(this).removeClass("formError");
} 
if(bolPolicyNumber && !bolAddress){ 
if ($(this).parent('div').attr('class') == 'policyNumber'){
$(this).prev('label').addClass("requiredCopy");
$(this).addClass("requiredField");
$(this).removeClass("notrequiredField");
} 
if (($(this).parent('div').attr('class') == 'addressBox' || $(this).parent('div').attr('class') == 'zipBox') && $(this).attr('id').indexOf('address2') == -1){
$(this).prev('label').removeClass("requiredCopy");
$(this).removeClass("requiredField");
$(this).addClass("notrequiredField");
$("#"+$(this).attr('id')+"errorBox").remove();
$(this).removeClass("formError");
}
if ($(this).parent('div').attr('class') == 'stateBox'){
$(this).prev('label').removeClass("requiredCopy");
$(this).removeClass("reqSelected");
$(this).addClass("notrequiredField");
$("#"+$(this).attr('id')+"errorBox").remove();
$(this).removeClass("formError");
} 
} 
if(!bolPolicyNumber && bolAddress){ 
if ($(this).parent('div').attr('class') == 'policyNumber'){
$(this).prev('label').removeClass("requiredCopy");
$(this).removeClass("requiredField");
$(this).addClass("notrequiredField");
$("#"+$(this).attr('id')+"errorBox").remove();
$(this).removeClass("formError");
} 
if (($(this).parent('div').attr('class') == 'addressBox' || $(this).parent('div').attr('class') == 'zipBox') && $(this).attr('id').indexOf('address2') == -1){
$(this).prev('label').addClass("requiredCopy");
$(this).addClass("requiredField");
$(this).removeClass("notrequiredField");

}
if ($(this).parent('div').attr('class') == 'stateBox'){
$(this).prev('label').addClass("requiredCopy");
$(this).addClass("reqSelected");
$(this).removeClass("notrequiredField"); 
} 
} 
});
}

function textCounter(field, maxlimit) {
//alert(field.value.length);
if (field.value.length > maxlimit) 
field.value = field.value.substring(0, maxlimit);
}