/******************************************
Name:			smartforms.js
Description:	Temporary hide referral option select boxes and texfields
Date:			24/07/2009
Author:			Andrei Vais

******************************************/

function hideFormElements()
{
    //build a list of all fields that we want to hide
    var elmsToHide = new Array ("ConsumerPress", "NewsPapers", "OtherReferralDetails");

    //using them we can then find the parent  so we cna hide the label as well
    for (var i = 0; i < elmsToHide.length; i++)
    {
        //check if the element exists
        if(!document.getElementById) return false;
	    if(!document.getElementById("Referral")) return false;
		if(!document.getElementById(elmsToHide[i])) return false;
		
		//alert(elmsToHide[i]);
	    document.getElementById(elmsToHide[i]).parentNode.style.display = "none";
    }
    
	if(!document.getElementById("Referral")) return false;
    var referral = document.getElementById("Referral").options[document.getElementById("Referral").selectedIndex].text;
    switch(referral)
    {
        case "Magazine":
	        document.getElementById("ConsumerPress").parentNode.style.display = "inline";
            break;
        case "Newspaper":
	        document.getElementById("NewsPapers").parentNode.style.display = "inline";
            break;
        case "Other":
	        document.getElementById("OtherReferralDetails").parentNode.style.display = "inline";
            break;
    }

}


function prepareReferralField()
{
    if(!document.getElementById) return false;
	if(!document.getElementById("Referral")) return false;
	
	//alert("Referral field exist");
	document.getElementById("Referral").onchange = function()
	{
	    var referralValue = document.getElementById("Referral").options[document.getElementById("Referral").selectedIndex].text;
	    displayHTMLElement(referralValue);
	}
}

function displayHTMLElement(pValue)
{
    hideFormElements();     //call this to reset the form in case the user changes their answer to tthe referral question
    
    //alert(pValue);
    switch(pValue)
    {
        case "Magazine":
            if(!document.getElementById) return false;
	        if(!document.getElementById("ConsumerPress")) return false;
	        document.getElementById("ConsumerPress").parentNode.style.display = "inline";
            break;
        case "Newspaper":
            if(!document.getElementById) return false;
	        if(!document.getElementById("NewsPapers")) return false;
	        document.getElementById("NewsPapers").parentNode.style.display = "inline";
            break;
        case "Other":
            if(!document.getElementById) return false;
	        if(!document.getElementById("OtherReferralDetails")) return false;
	        document.getElementById("OtherReferralDetails").parentNode.style.display = "inline";
            break;
    }
}

addLoadEvent(hideFormElements);
addLoadEvent(prepareReferralField);