var timerID = 0;

$(function() {
	
    //Overlay popup
	$(".confirm_form .closeBtn").click(function()
    {
	    $("#popupOverlay").fadeOut();
	    $(".confirm_form").hide();
    });
	
	$(".confirm_form #ctl00_btnConfirm").click(function()
    {
	    $("#popupOverlay").fadeOut();
	    $(".confirm_form").hide();
        ServerRequest("confirm");
	    return false;
    });
	
	$(".confirm_form #ctl00_btnCancel").click(function()
    {
	    $("#popupOverlay").fadeOut();
	    $(".confirm_form").hide();
	    return false;
    });

    //FAQs
	$(".faq .title").click(function(){
		var liclass = $(this).parent().attr("class");
		if(liclass == "expand"){
			$(this).parent().animate({ backgroundColor: "#ffffff", borderColor: "#ffffff"}, 200);
			$(this).siblings("p").slideUp(200, function(){
				$(this).parent().removeClass().addClass("collapse");
			});
		}else if(liclass == "collapse"){
			$('.faq .expand').animate({ backgroundColor: "#ffffff", borderColor: "#ffffff"}, 200);
			$('.faq .expand p').slideUp(200, function(){
				$(this).parent().removeClass().addClass("collapse");
			});
			$(this).parent().animate({ backgroundColor: "#fff8fb", borderColor: "#ffedf4"}, 200);
			$(this).siblings("p").slideDown(200, function(){
				$(this).parent().removeClass().addClass("expand");
			});
		}
	});
	
});

window.onload = function()
{
    if (timerID == 0)
    {
        //timeOut();
    }

    document.getElementById("ctl00_Content_CustomerControl1_btnTractionSubmit").onmousedown = function ()
    {
        FormValidate();
    }
}

window.alert = function(text)
{
    if(typeof(text) == "string")
    {
        text = text.replace(/-/g, "<br />&#149;");
    }
    
    radalert(text, "auto", null, "Unilever Health");
}

function timeOut()
{
    ServerRequest("");
    timerID = setTimeout("timeOut()", 1500);
}

function ServerRequest(value)
{
    CallServer(value, "");
}

function ServerResponse(value)
{
    //alert(value);
}

function confirmOverlay()
{
	$("#popupOverlay").css({width:$(document).width(), height:$(document).height(), "filter":"alpha( opacity=70 )"}).fadeIn();

	var window_width = $(window).width();
	var window_height = $(window).height();
	var popup_width = $(".confirm_form").width();
	var popup_height = $(".confirm_form").height();
	var top = (window_height - popup_height) / 2  + $(window).scrollTop();
	var left = (window_width - popup_width) / 2;
	$(".confirm_form").css({top:top, left:left}).show();
}

function FormValidate()
{
    var message = "";
    
    var chkPerspective = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction009");
    var chkVitality = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction010");
    var chkNutrition = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction011");
    var IsNewsletters = chkPerspective.checked || chkVitality.checked || chkNutrition.checked;
    
    var chkCholesterol = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_0");
    var chkTea = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_1");
    var chkChildren = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_2");
    var chkFamily = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_3");
    var chkResearch = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_4");
    var chkOther = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_5");
    var chkSalt = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_6");
    var chkPatient = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_7");
    var chkRecipes = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_8");
    var chkNew = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_9");
    var chkProduct = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction013_10");
    var IsInterest = chkCholesterol.checked || chkTea.checked || chkChildren.checked || chkFamily.checked || chkResearch.checked || chkOther.checked || chkSalt.checked || chkPatient.checked || chkRecipes.checked || chkNew.checked || chkProduct.checked;
    
    if(!IsNewsletters && !IsInterest)
    {
        message += "- Please select at least one newsletter or area of nutrition interest";
    }
    
    var chkPost = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction016");
    var chkEmail = document.getElementById("ctl00_Content_CustomerControl1_ctlEmailOptStatus");
    
    if(!chkPost.checked && !chkEmail.checked)
    {
        message += "- Please select your preferred mode(s) of receiving material";
    }
    
    var txtAddress = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction003");
    var txtSuburb = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction005");
    var ddlState = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction006");
    var txtPostcode = document.getElementById("ctl00_Content_CustomerControl1_ctlTraction007");
    
    if(chkPost.checked)
    {
        if(txtAddress.value == "")
        {
            message += "- Line 1 is required."
        }
        if(txtSuburb.value == "")
        {
            message += "- Suburb is required."
        }
        if(ddlState.value == "")
        {
            message += "- State is required."
        }
        if(txtPostcode.value == "")
        {
            message += "- Postcode is required."
        }
    }
   
    if(message != "")
    {
        alert("Please correct the following errors:" + message);
    }
}
