﻿
$(document).ready(function() {
   
   
   $("#ie8Entry input.button").click(function(){
        return submitForm();
    });
    $("input[type='text']").keypress(function(e){

        if (e.keyCode == 13){  
            $("#ie8Entry input.button").get(0).click();
        } 
    });
   
}); 

function populateAlert(str){
    $("#iAlertList").html(str);
    $("#iAlert").animate({"opacity": "show"}, "slow", "linear");
    $(".iError").css("display","none");
}
function submitForm(){
    // grab values of fields and update iAlert ul

    var st = "";
    if ($(".strAbout").val() === ""){
        st = "<li>Empty field: don't you want to tell us about it a bit?</li>";
    }
    if (String($.trim($(".strAbout").val())).length > 300) {
    //if (String($.trim($("#strAbout").val())).split(/\s+/gi).length > 25){
   
        st += "<li>Can you tell us about it in less than 300 characters?</li>";
    }
    var re = /^(http:\/\/)[\S]+$/i;
    if ($(".strLink").val() != "" && !re.test($(".strLink").val())) {
    	st += "<li>Invalid link format.</li>";
    }

    var bI = false;
    var emptySt = "";
    $("input[type='text']").each(function(){
        if ($(this).val() === "") {
            if (bI){emptySt += ", ";}
            emptySt += $(this).attr("title");
            bI = true;
        }
    });
	if (emptySt !== ""){
        st += "<li>Missing fields: " + emptySt + "</li>";

	}
	 // check email format 
    var re = /^[\w\-]+(?:\.[\w\-]+)*@(?:[\w\-]+\.)+[a-zA-Z]{2,7}$/i;
    if ($(".strEmail").val() != "" && !re.test($(".strEmail").val())) {
        st += "<li>Invalid email format</li>";
    }
    
	 // check checkbox
   	if (!$(".chkAgree input:checkbox").attr("checked")){
		st += "<li>You must agree to the terms and conditions to proceed.</li>";
	}
	
	
    if (st !== ""){
        populateAlert(st);
        return false;
    }
	$("#iAlert").css("display","none");
    return true;
}       

