jQuery("document").ready(init);

function init() {

    var submit = jQuery("input[name='_submit']");
    var form = jQuery("form#inquire");
    var name = jQuery("input[name='_name']");
    var email = jQuery("input[name='_email']");
    var inquiry = jQuery("textarea[name='_inquiry']");

    jQuery("div.error1").hide();
    jQuery("div.error2").hide();
    jQuery("div.error3").hide();

    jQuery("area").hover(function () {showCountry(this.id)});
    
    submit.click(validateForm);

    if (name.val() == '' || name.val() == 'Name') {
        name.focus(function() { name.val("")});
        //name.blur(function() { name.val("Name")});
    }

    if (email.val() == '' || email.val() == 'Email') {
        email.focus(function() { email.val("") });
        //email.blur(function() { email.val("Email") });
    }
    if (inquiry.val() == '' || inquiry.val() == 'Please enter your inquiry here.') {
        inquiry.focus(function() { inquiry.val("") });
        //inquiry.blur(function() { inquiry.val("Please enter your inquiry here.") });
    }
    if(jQuery('div.messageSent')) {
        //alert('wwwwwooooooooooo');
    }

    /* LIGHTBOX */
    jQuery("td a").lightBox();
    /* LIGHTBOX */
 

    function validateForm() {

        var errors = 0;

        if (name.val() == '' || name.val() == 'Name') {
            jQuery("div.error1").show();
            errors++;
        } else {
            jQuery("div.error1").hide();
        }
        if (email.val() == '' || email.val() == 'Email') {
            jQuery("div.error2").show();
            errors++;
        } else {
           
            if(IsValidEmail(email.val())) {
                jQuery("div.error2").hide();
            } else {
                jQuery("div.error2").show();
                errors++;
            }
        }
        if (inquiry.val() == '' || inquiry.val() == 'Please enter your inquiry here.') {
            jQuery("div.error3").show();
            errors++;
            
        } else {
            jQuery("div.error3").hide();
        }


        if(errors > 0) {
            return false;
        } else {
            return true;
        }
    }
}



function IsValidEmail(email){

	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return filter.test(email);

}



function showCountry(id) {

    //alert(isNaN(id));


    //Remove all the "hover" classes from the li's'
    for (var i=1; i <= 12; i++) {
        jQuery("li."+i).removeClass("hover");
    }

    jQuery("li."+id).addClass("hover");

    //Change the text depending on where you hover
    switch (id) {
        case "1":country = "Chile";break;
        case "2":country = "Ghana";break;
        case "3":country = "South Africa";break;
        case "4":country = "Namibia";break;
        case "5":country = "Botswana";break;
        case "6":country = "Zimbabwe";break;
        case "7":country = "Malawi";break;
        case "8":country = "Democratic Repuplic of the Congo";break;
        case "9":country = "Nigeria";break;
        case "10":country = "Qatar";break;
        case "11":country = "Poland";break;
        case "12":country = "Tanzania";break;
        default:break;
    }   

    jQuery("div#map div#country").text(country);
}
