// Home Functions

function validateZipHome()
{
    var zipHome = document.getElementById('fmZipHome').value;
    clearAllFieldsHome();
	
    
    if(isNaN(zipHome))
    {
        document.getElementById('formFieldHomeTD').style.background = "url('images/bg_form_home_error.gif') 0 8px no-repeat";
        document.getElementById('searchValidHome').innerHTML = "Please enter a ZIP Code using only numbers.";
        document.getElementById('searchValidHome').style.display = "block";
    }
    else if(zipHome.length < 5)
    {
        document.getElementById('formFieldHomeTD').style.background = "url('images/bg_form_home_error.gif') 0 8px no-repeat";
        document.getElementById('searchValidHome').innerHTML = "Please enter your 5-digit ZIP code.";
        document.getElementById('searchValidHome').style.display = "block";
    }
    else
    {
        window.location = "application/results.html";
    }
	
}

function clearAllFieldsHome()
{
    document.getElementById('formFieldHomeTD').style.background = "url('images/bg_form_home.gif') 0 8px no-repeat";
    document.getElementById('searchValidHome').style.display = "none";
}


// Main Functions
function loadSearchGel()
{
  document.getElementById('searchGel').innerHTML = 
    '<form name="zipSubmit" method="post" action="/cgi-bin2/agentdirectory.exe" style="margin:0;padding:0;">' +
		'<span class="gelHdr">Find a Local <span class="headerSm">Insurance Agent Instantly</span></span>' +
			'<table cellpadding="0" cellspacing="0" border="1">' +
			   ' <tr>' +
					'<td class="formLabel">ZIP Code:</td>' +
					'<td class="formField" id="formFieldTD"><input name="GarageZip" id="GarageZip"  type="text" maxlength="5" /></td>' +
				   ' <td class="formBtn"><input name="action=new_user&" id="StartSubmit" type="image" src="../images/spacer.gif" width="56" height="52" alt="Go" /></td>' +
				'</tr>' +
			'</table>' +
		'<span id="searchValid" class="validator"></span>'
    '</form>';
    
    clearAllFields();
}
function validateZip(valuePassed)
{
    var zip = document.getElementById('fmZip').value;
    clearAllFields(valuePassed);
    
    if(isNaN(zip))
    {
        if(valuePassed == "result")
            document.getElementById('formFieldTD').style.background = "url('../images/bg_form_result_on.gif') no-repeat";
        else
            document.getElementById('formFieldTD').style.background = "url('../images/bg_form_gel_on.gif') 0 3px no-repeat";
        document.getElementById('searchValid').innerHTML = "Please enter a ZIP Code using only numbers.";
        document.getElementById('searchValid').style.display = "block";
    }
    else if(zip.length < 5)
    {
        if(valuePassed == "result")
            document.getElementById('formFieldTD').style.background = "url('../images/bg_form_result_on.gif') no-repeat";
        else
            document.getElementById('formFieldTD').style.background = "url('../images/bg_form_gel_on.gif') 0 3px no-repeat";
        document.getElementById('searchValid').innerHTML = "Please enter your 5-digit ZIP code.";
        document.getElementById('searchValid').style.display = "block";
    }
    else
    {
        alert("no error");
    }
}

function clearAllFields(pageValue)
{
    if(pageValue == "result")
        document.getElementById('formFieldTD').style.background = "url('../images/bg_form_result_off.gif') no-repeat";
    else
        document.getElementById('formFieldTD').style.background = "url('../images/bg_form_gel_off.gif') 0 3px no-repeat";
    document.getElementById('searchValid').style.display = "none";
}

// enlarge map
function mapMaximize()
{
    document.getElementById('mapBig').style.display = "block";
    document.getElementById('mapSmall').style.display = "none";
}

function mapMinimize()
{
    document.getElementById('mapBig').style.display = "none";
    document.getElementById('mapSmall').style.display = "block";
}

// Email Agent Submission
function eAgentSubmit()
{
    var error = false;
    var name = document.getElementById('eAgentName').value;
    var email = document.getElementById('eAgentEmail').value;
    var phone1 = document.getElementById('eAgentPhone1').value;
    var phone2 = document.getElementById('eAgentPhone2').value;
    var phone3 = document.getElementById('eAgentPhone3').value;
    var phone = phone1 + phone2 + phone3;
    var hasAt = false;
    var hasDot = false;
        
    clearEAgentFields();
    
    // Validate Name
    if(name.length < 1)
    {
        error = true;
        document.getElementById('eAgentNameTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
        document.getElementById('eAgentNameValid').style.display = "block";
        document.getElementById('eAgentNameValid').innerHTML = "Please enter a valid name.";
    }
    else if(!isNaN(name))
    {
        error = true;
        document.getElementById('eAgentNameTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
        document.getElementById('eAgentNameValid').style.display = "block";
        document.getElementById('eAgentNameValid').innerHTML = "Please enter a valid name.";
    }
    else
    {
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_"; 
        
        for (var i = 0; i < name.length; i++)
        {
            if (iChars.indexOf(name.charAt(i)) != -1)
            {
                error = true;
                document.getElementById('eAgentNameTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
                document.getElementById('eAgentNameValid').style.display = "block";
                document.getElementById('eAgentNameValid').innerHTML = "Please enter a name using letters only.";
            }
        }
    }
        
    // Validate Email
    if(email.length < 1)
    {
        error = true;
        document.getElementById('eAgentEmailTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
        document.getElementById('eAgentEmailValid').style.display = "block";
        document.getElementById('eAgentEmailValid').innerHTML = "Please enter a valid email address.";
    }
    
    else
    {
        
        for (var i = 0; i < email.length; i++)
        {   
            if (email.charAt(i) == "@")
            {
                hasAt = true;
            }
            if (email.charAt(i) == ".")
            {
                hasDot = true;
            }
        }
        
        if((hasAt == false) || (hasDot == false))
        {
            error = true;
            document.getElementById('eAgentEmailTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
            document.getElementById('eAgentEmailValid').style.display = "block";
            document.getElementById('eAgentEmailValid').innerHTML = "Please enter a valid email address.";
        }
    }
    
    // Validate Phone
    if(phone1.length < 3 || phone2.length < 3 || phone3.length < 4)
    {
        error = true;
        document.getElementById('eAgentPhone1TD').style.background = "url('../images/bg_form_popup_sm_error.gif') no-repeat";
        document.getElementById('eAgentPhone2TD').style.background = "url('../images/bg_form_popup_sm_error.gif') no-repeat";
        document.getElementById('eAgentPhone3TD').style.background = "url('../images/bg_form_popup_mid_error.gif') no-repeat";
        document.getElementById('eAgentPhoneValid').style.display = "block";
        document.getElementById('eAgentPhoneValid').style.clear = "both";
        document.getElementById('eAgentPhoneValid').innerHTML = "Please enter a valid phone number.";
    }
    else if(isNaN(phone1))
    {
        error = true;
        document.getElementById('eAgentPhone1TD').style.background = "url('../images/bg_form_popup_sm_error.gif') no-repeat";
        document.getElementById('eAgentPhone2TD').style.background = "url('../images/bg_form_popup_sm_error.gif') no-repeat";
        document.getElementById('eAgentPhone3TD').style.background = "url('../images/bg_form_popup_mid_error.gif') no-repeat";
        document.getElementById('eAgentPhoneValid').style.display = "block";
        document.getElementById('eAgentPhoneValid').style.clear = "both";
        document.getElementById('eAgentPhoneValid').innerHTML = "Please enter a phone number using numbers only.";
    }
    
    if(error == false)
    {
        alert("no error");
    }
    
}

// Email A Friend Submission
function eFriendSubmit()
{
    var error = false;
    var name = document.getElementById('eFriendName').value;
    var email = document.getElementById('eFriendEmail').value;
    var hasAt = false;
    var hasDot = false;
        
    clearEFriendFields();
    
    // Validate Name
    if(name.length < 1)
    {
        error = true;
        document.getElementById('eFriendNameTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
        document.getElementById('eFriendNameValid').style.display = "block";
        document.getElementById('eFriendNameValid').innerHTML = "Please enter a valid name.";
    }
    else if(!isNaN(name))
    {
        error = true;
        document.getElementById('eFriendNameTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
        document.getElementById('eFriendNameValid').style.display = "block";
        document.getElementById('eFriendNameValid').innerHTML = "Please enter a valid name.";
    }
    else
    {
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_"; 
        
        for (var i = 0; i < name.length; i++)
        {
            if (iChars.indexOf(name.charAt(i)) != -1)
            {
                error = true;
                document.getElementById('eFriendNameTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
                document.getElementById('eFriendNameValid').style.display = "block";
                document.getElementById('eFriendNameValid').innerHTML = "Please enter a name using letters only.";
            }
        }
    }
        
    // Validate Email
    if(email.length < 1)
    {
        error = true;
        document.getElementById('eFriendEmailTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
        document.getElementById('eFriendEmailValid').style.display = "block";
        document.getElementById('eFriendEmailValid').innerHTML = "Please enter a valid email address.";
    }
    
    else
    {
        
        for (var i = 0; i < email.length; i++)
        {   
            if (email.charAt(i) == "@")
            {
                hasAt = true;
            }
            if (email.charAt(i) == ".")
            {
                hasDot = true;
            }
        }
        
        if((hasAt == false) || (hasDot == false))
        {
            error = true;
            document.getElementById('eFriendEmailTD').style.background = "url('../images/bg_form_popup_error.gif') no-repeat";
            document.getElementById('eFriendEmailValid').style.display = "block";
            document.getElementById('eFriendEmailValid').innerHTML = "Please enter a valid email address.";
        }
    }
    
    if(error == false)
    {
        alert("no error");
    }
    
}

function clearEAgentFields()
{
    document.getElementById('eAgentNameValid').style.display = "none";
    document.getElementById('eAgentEmailValid').style.display = "none";
    document.getElementById('eAgentPhoneValid').style.display = "none";
    
    document.getElementById('eAgentNameTD').style.background = "url('../images/bg_form_popup.gif') no-repeat";
    document.getElementById('eAgentEmailTD').style.background = "url('../images/bg_form_popup.gif') no-repeat";
    document.getElementById('eAgentPhone1TD').style.background = "url('../images/bg_form_popup_sm.gif') no-repeat";
    document.getElementById('eAgentPhone2TD').style.background = "url('../images/bg_form_popup_sm.gif') no-repeat";
    document.getElementById('eAgentPhone3TD').style.background = "url('../images/bg_form_popup_mid.gif') no-repeat";
}

function clearEFriendFields()
{
    document.getElementById('eFriendNameValid').style.display = "none";
    document.getElementById('eFriendEmailValid').style.display = "none";
    
    document.getElementById('eFriendNameTD').style.background = "url('../images/bg_form_popup.gif') no-repeat";
    document.getElementById('eFriendEmailTD').style.background = "url('../images/bg_form_popup.gif') no-repeat";
}