/**
 * Copyright 2008 Mac-Clair Mortgage Corporation. All rights reserved
 */

// jQuery page load functions
$(document).ready(function(){
	// Fix Equal Housing Lender and Better Business Bureau logos in IE6
	jQuery.each(jQuery.browser, function(i, val) {
  		if (i == "msie" && jQuery.browser.version == "6.0") {
			document.getElementById('footerehl').src = 'images/ehl.gif';
            document.getElementById('footerbbb').src = 'images/bbb.gif';
		}	
	});
	// Preload rollover images
	$.preloadImages("images/top-nav_hl1.png","images/top-nav_hl2.png","images/top-nav_hl3.png","images/applyover.png","images/lofindover.png","images/submit.png","images/submitdown.png","images/submitover.png");
	// Fade between images on front page
	$('#billboard').innerfade({ 
		speed: 'slow', 
		timeout: 12000, 
		type: 'sequence', 
		containerheight: '350px' 
	});
	// spam countermeasures
    $.get("includes/token.php",function(txt){
  		$("#applyform").append('<input type="hidden" name="ts" value="'+txt+'" />');
		$("#rmsform").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
	    
	$("#workingyes,#workingno").ready(function(){
		if ($("input[name='working']:checked").val() == 'yes') {
			$("#workingloname").show();
		} else {
			$("#workingloname").hide();
		}
	});	
		
    // Mouseover and animation handling
    $("#top-nav-1").hover(
        function(){
            $("#top-nav-image").attr('src','images/top-nav_hl1.png');
        },
        function(){
            $("#top-nav-image").attr('src','images/top-nav.png');
        }
    );
    $("#top-nav-2").hover(
        function(){
            $("#top-nav-image").attr('src','images/top-nav_hl2.png');
        },
        function(){
            $("#top-nav-image").attr('src','images/top-nav.png');
        }
    );
    $("#top-nav-3").hover(
        function(){
            $("#top-nav-image").attr('src','images/top-nav_hl3.png');
        },
        function(){
            $("#top-nav-image").attr('src','images/top-nav.png');
        }
    );
    $("#apply-image").hover(
        function(){
            $("#apply-image").attr('src','images/applyover.png');
        },
        function(){
            $("#apply-image").attr('src','images/apply.png');
        }
    );
    $("#lofind-image").hover(
        function(){
            $("#lofind-image").attr('src','images/lofindover.png');
        },
        function(){
            $("#lofind-image").attr('src','images/lofind.png');
        }
    );
    $(".showlofind").click(function(){
        $("#lofindform").slideToggle("normal");
    });
    $("#lo_submit").hover(
        function(){
            $("#lo_submit").attr('src','images/submitover.png');
        },
        function(){
            $("#lo_submit").attr('src','images/submit.png');
        }
    );
    $("#lo_submit").mousedown(function(){
        $("#lo_submit").attr('src','images/submitdown.png');
    });
    $("#lo_submit").mouseup(function(){
        $("#lo_submit").attr('src','images/submitover.png');
    });
    $("#showbranchlist").click(function(){
        $("#branchlist").toggle("normal");
    });
	$("#addloanofficertoggle").click(function(){
        $("#addloanofficer").slideToggle("normal");
    });
	$("#addbranchtoggle").click(function(){
        $("#addbranch").slideToggle("normal");
    });
	$("#addquotetoggle").click(function(){
        $("#addquote").slideToggle("normal");
    });
    
	//Apply Online LO Name box
	$("#workingyes,#workingno").click(function(){
		if ($("input[name='working']:checked").val() == 'yes') {
			$("#workingloname").show();
		} else {
			$("#workingloname").hide();
		}
	});
	
	//Submit LO Finder on Enter
	$(".lofindenter").keyup(function(event){
  		if(event.keyCode == 13){
    		lofindSubmit();
  		}
	});
});

// Apply Form Submit
function applySubmit(){
	nameregex = /^.{2,}$/;
	emailregex = /^.+@[^\.].*\.[a-z]{2,}$/;
	phoneregex = /^\(?[0-9]{3}\)?-?[0-9]{3}-?[0-9]{4}$/
	if (nameregex.test(document.getElementById("applyform").name.value) == false) {
		document.getElementById("submiterror").innerHTML = "Please enter a valid name.";
		return 0;
	}
	if (emailregex.test(document.getElementById("applyform").email.value)==false) {
		document.getElementById("submiterror").innerHTML="Please enter a valid email address.";
		return 0;
	}
	if ((phoneregex.test(document.getElementById("applyform").dayphone.value)==false) && (phoneregex.test(document.getElementById("applyform").eveningphone.value)==false)) {
		document.getElementById("submiterror").innerHTML="Please enter a valid daytime or evening phone number.";
		return 0;
	}
	// submit form if all information validates
	document.getElementById("applyform").submit();
}

// Request Information Form Submit
function rmsSubmit(){
	nameregex = /^.{2,}$/;
	emailregex = /^.+@[^\.].*\.[a-z]{2,}$/;
	phoneregex = /^\(?[0-9]{3}\)?-?[0-9]{3}-?[0-9]{4}$/
	if (nameregex.test(document.getElementById("rmsform").name.value) == false) {
		document.getElementById("submiterror").innerHTML = "Please enter a valid name.";
		return 0;
	}
	if (phoneregex.test(document.getElementById("rmsform").phone.value)==false) {
		document.getElementById("submiterror").innerHTML="Please enter a valid phone number.";
		return 0;
	}
	if (emailregex.test(document.getElementById("rmsform").email.value)==false) {
		document.getElementById("submiterror").innerHTML="Please enter a valid email address.";
		return 0;
	}
	// submit form if all information validates
	document.getElementById("rmsform").submit();
}

function lofindSubmit(){
	nameregex = /^.{1,}$/;
	zipregex = /(^\d{5}$)/;
	if ((nameregex.test(document.getElementById("lofindform").name.value)==false) && (document.getElementById("lo_name").value.length > 0)) {
		document.getElementById("lofinderror").innerHTML="Enter valid Name";
		return 0;
	}
	if ((zipregex.test(document.getElementById("lofindform").zip.value)==false) && (document.getElementById("lo_zip").value.length > 0)) {
		document.getElementById("lofinderror").innerHTML="Enter valid Zip Code";
		return 0;
	}
	if ((document.getElementById("lo_zip").value.length == 0) && (document.getElementById("lo_name").value.length == 0)) {
		document.getElementById("lofinderror").innerHTML="Enter a Search Term";
		return 0;
	}
	// submit form if all information validates
	document.getElementById("lofindform").submit();
}

function confirmDelete(deletepage, deletename, deleteid) {
	var confirmAlert=confirm("Are you sure you want to delete "+deletename+"?");
	if (confirmAlert) {
		window.location=deletepage+"?delete="+deleteid;
	}
}
