﻿function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function checkemail(emailstr)
{
	var emailFilter=/^(([a-zA-Z0-9_\-]+)|(([a-zA-Z0-9_\-]+)([\.]{1,1}[a-zA-Z0-9_\-]{1,}))+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!(emailFilter.test(emailstr))) 
		return false;
	return true;
}


jQuery(document).ready(function(){
    jQuery("#btnSubmit").click(function() {
        if (jQuery("#captchacode").val() != '') 
        {
            jQuery.getJSON("/CaptchaImage.aspx?validateCaptchaCode=" +  jQuery("#captchacode").val() + "&format=json&jsoncallback=?", function(data){
	
		        if (data.status == "1")
		        {
		        
		            if (checkemail(jQuery("#email").val()))
		            {
    			        jQuery("#contactForm").submit();
    	           	    result = true;
    	           	}
    	           	else
    	           	    alert('Please enter a valid email address.');
		        }
			    else
			    {
				    if (data.session == "0")
					    RefreshImage("imgCaptcha");
    				
				    alert("Please key in the security key correctly.");
				    jQuery("#captchacode").focus();
    		
				    result = false;
			    }
		    });
        }
        else
            alert('Please key in the security key correctly.');
    });
    jQuery("#btnSubmit").css({
        "display" : "block"
    });
});