jQuery(function(){
    $.ajaxSetup({
        url: "/operator.php",
        dataType: "script",
        type: "POST"
    });
        
    $("#cloud-1").animate({ left: "500px", top: "90px" }, 500000, "linear")
        .animate({ opacity: "0" }, 1500);
    $("#cloud-2").animate({ left: "720px", top: "90px" }, 500000, "linear")
        .animate({ opacity: "0" }, 1500);
        
    $(".subnav-item:not(.selected-subnav-item)").hover(
        function(){ $(this).animate({ top: "2px" }, 150 )}, 
        function(){ $(this).animate({ top: "0" }, 150 )}
    );
    
    $.ajax({ data: "action=get-secure-emails&email_ids=sales,service,corporate,website" });
    
    
    $("#fade-container").css("opacity", 0.5);
    $("#google-search > input").mousedown(function(){
        $(this).val("").css("color", "#000");
    });
    
    $("#join-the-macpac-link").click(function(event){
        event.preventDefault();
        $("#sign-up-form").slideToggle().toggleClass("toggled");
        $("#fade-container").fadeToggle();
    });
    
    $("#header-right > .search-box").click(function(event){
        event.stopPropagation();
    });
    $("#sign-up-form a.submit").click(function(event){
        event.preventDefault();
        $("#sign-up-form form").submit();
    });
    $("body").click(function(){
        $("#sign-up-form.toggled").slideUp().removeClass("toggled");
        $("#fade-container").fadeOut();
    });
    
});



// subscribermail validation script
function validEmail(email) {
	invalidChars = " /:,;";

	if (email == "") {
		return false;
	}
	
	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	
	atPos = email.indexOf("@",1);
	
	if (atPos == -1) {
		return false;
	}
	
	if (email.indexOf("@",atPos+1) > -1) {
		return false;
	}
	
	periodPos = email.indexOf(".",atPos);
	
	if (periodPos == -1) {
		return false;
	}
	
	if (periodPos + 3 > email.length)	{
		return false;
	}
	return true;
}

function checkRequired(errors) {
	var reqfieldsString = "";
	var reqfields = [];
	var strErrors = "";
	if (reqfieldsString.length != 0) {
		reqfields = reqfieldsString.split(",");
	}
	
	for( i=0; i < reqfields.length; i++ ) {
		
		var tempfieldname = document.getElementById(reqfields[i]).title;
		var tempval =  document.getElementById(reqfields[i]).value;
		
		if (tempval == "") {
			errors.push(tempfieldname + " is required");
		}
	}
	if (errors.length != 0) {
		
		strErrors = errors.join("\n");
		
	}
	
	return strErrors;
}

function submitIt(subsForm) {
	var errors = [];
	var error = "";
	var this_email = document.getElementById("email");
	
	var this_email_val = this_email.value;
	
	
	if (this_email_val == "") {
		errors.push("Email address is required");
	} else if (!validEmail(this_email_val)) {
		errors.push("Invalid email address");
	}
	
	error = checkRequired(errors);
	
	if ( error.length > 0 ) {
		
		alert("The fields below are either blank or have invalid data." + "\n\n" + error);
		
		return false;
	}
	return true;
}
