﻿function trim(s) {
    return s.replace(/^\s+|\s+$/, '');
}
function validateFirstName(value) {
    var error = "";
    if (value == "" || value.length < 1||value=="-First Name-") {        
        error = "You have entered invalid information in the First Name field.\n";
    }    
    return error;
}
function validateLastName(value) {
    var error = "";
    if (value == "" || value.length < 1|| value=="-Last Name-") {        
        error = "You have entered invalid information in the Last Name field.\n";
    }   
    return error;
}
function validatePhone(value) {
    var error = "";
    var filter = /^((\(\d{3}\) ?|\d{3})|(\d{3}-?))?\d{3}-?\d{4}$/;
    if (value == "" || value.length < 10 || value.length >= 20 || !value.match(filter)) {
        error = "You have entered invalid information in the Phone field.\n";        
    } 
    return error;
}
function validateRequirePhone(value)
{
    var error = "";
    if (value == "" || value.length < 1|| value=="-Phone-") {        
        error = "You have entered invalid information in the Phone field.\n";
    }   
    return error;
}
function validateAddress(value) {
    var error = "";
    if (value == "" || value.length < 1) {
        error = "You have entered invalid information in the Street Address 1 field.\n";
    }
    return error;
}
function validateCity(value) {
    var error = "";
    if (value == "" || value.length < 1) {
        error = "You have entered invalid information in the City field.\n";
    }
    return error;
}
function validateState(value) {
    var error = "";
    if (value == "" || value.length < 1) {
        error = "You have entered invalid information in the State field.\n";
    }
    return error;
}
function validatePostaCode(value) {
    var error = "";
    if (value == "" || value.length < 1) {
        error = "You have entered invalid information in the Posta Code field.\n";
    }
    return error;
}

function validateUsername(value) {
    var error = "";
    var filter = /^[0-9a-zA-Z]+$/;
    if (value == "" || value.length < 3 || value.length >= 10 || !value.match(filter)) {
        error = "You have entered invalid information in the User Name field.\n";
    }
    return error;
}
function validatePassword(value) {
    var error = "";
    if (value == "" || value.length < 3) {
        error = "You have entered invalid information in the Password field.\n";
    }
    return error;
}
function validateReTypePassword(value, password) {
    var error = "";
    if (value == "" || value.length < 3 || value != password) {
        error = "Retype Password not match.\n";
    }
    return error;
}
function formatPhone(value) {
    var phone = value;
    phone = phone.replace(/[\-]/g, '');
    phone = phone.replace(/[\)]/g, '');
    phone = phone.replace(/[\(]/g, '');
    phone = phone.replace(/[\s]/g, '');
    return phone;
}

function validateEmail(value) {
    var error = "";
    var tfld = trim(value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/; 
    if (value == "" || !emailFilter.test(tfld) || value.match(illegalChars)) {        
        error = "You have entered invalid information in the Email field.\n";       
    } 
    return error;
}
function validateCountry(value) {
    var error = "";
    if (value == "" || value.length < 1) {
        error = "You have entered invalid information in the Country field.\n";
    }
    return error;
}

function checkAffiliateForm() {
    var firstName = true;
    var lastName = true;
    var email = true;
    var phone = true;
    var country = true;
    var address = true;
    var city = true;
    var state = true;
    var postCode = true;
    var error = "";
    if (validateFirstName($("#Contact0FirstName").val()) != "") {
        firstName = false;
        error += validateFirstName($("#Contact0FirstName").val());
    }
    if (validateLastName($("#Contact0LastName").val()) != "") {
        lastName = false;
        error += validateLastName($("#Contact0LastName").val());
    }
    if (validateEmail($("#Contact0Email").val()) != "") {
        error += validateEmail($("#Contact0Email").val());
        email = false;
    }
    if(validateRequirePhone($("#Contact0Phone1").val())!=""){
        error += validateRequirePhone($("#Contact0Phone1").val());
        phone = false;
    }
    if (validateCountry($("#Contact0Country").val()) != "") {
        error += validateCountry($("#Contact0Country").val());
        country = false;
    }
    if (validateAddress($("#Contact0StreetAddress1").val()) != "") {
        error += validateAddress($("#Contact0StreetAddress1").val());
        address = false;
    }
    if (validateCity($("#Contact0City").val()) != "") {
        error += validateCity($("#Contact0City").val());
        city = false;
    }
    if (validateState($("#Contact0State").val()) != "") {
        error += validateState($("#Contact0State").val());
        state = false;
    }
    if (validatePostaCode($("#Contact0PostalCode").val()) != "") {
        error += validatePostaCode($("#Contact0PostalCode").val());
        postCode = false;
    }
    if (error == "") {
        if ($("#Checkbox0").attr("checked") == true) {
            var frm = document.forms[0];
            frm.method = 'POST';
            frm.action = 'https://dynamic.infusionsoft.com/AddForms/processFormSecure.jsp';
            frm.submit();
        }
        else {
            alert("You must check the Affiliate Agreement checkbox to continue.");
            window.location.href = "affiliates-signup.aspx#check";
        }
    }
    else {
        alert(error);
        if (!firstName)
            $("#Contact0FirstName").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0FirstName").attr("style", "");
        if (!lastName)
            $("#Contact0LastName").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0LastName").attr("style", "");
        if (!email)
            $("#Contact0Email").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0Email").attr("style", "");
        if(!phone)
            $("#Contact0Phone1").attr("style","background-color:#F4DB41;");
        else
            $("#Contact0Phone1").arttr("style","");
        if(!country)
            $("#Contact0Country").attr("style","background-color:#F4DB41;");
        else
            $("#Contact0Country").arttr("style","");
        if (!address)
            $("#Contact0StreetAddress1").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0StreetAddress1").attr("style", "");

        if (!city)
            $("#Contact0City").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0City").attr("style", "");

        if (!state)
            $("#Contact0State").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0State").attr("style", "");

        if (!postCode)
            $("#Contact0PostalCode").attr("style", "background-color:#F4DB41;");
        else
            $("#Contact0PostalCode").attr("style", "");

        window.location.href = "?p=affiliates#form";
    }
}