function GetXmlHttpObject(){var xmlHttp = null;	try {xmlHttp = new XMLHttpRequest();} catch (e) {try{xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}

window.onload = function () {
    createSwftype(".swfTypeHeading1", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading2", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading3", "flash/SwftypeArialRounded.swf", "css/swftype.css", 240, 27, false, "height", false);
	createSwftype(".swfTypeHeading4", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading5", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading6", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading6a", "flash/SwftypeArialRounded.swf", "css/swftype.css", 242, 27, false, "height", false);
	createSwftype(".swfTypeHeading7", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading8", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading9", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeHeading10", "flash/SwftypeArialRounded.swf", "css/swftype.css", 240, 27, false, "height", false);
	createSwftype(".swfTypeHeading11", "flash/SwftypeArialRounded.swf", "css/swftype.css", 240, 27, false, "height", false);
	createSwftype(".swfTypeText1", "flash/SwftypeArialRounded.swf", "css/swftype.css", 563, 27, false, "height", false);
	createSwftype(".swfTypeResultText1", "flash/SwftypeArialRounded.swf", "css/swftype.css", 240, 27, false, "all", false);
	createSwftype(".swfTypeStep", "flash/SwftypeArialRounded.swf", "css/swftype.css", 32, 28, false, "height", false);
	createSwftype(".swfTypeHeading12", "flash/SwftypeArialRounded.swf", "css/swftype.css", 470, 27, false, "height", false);
}

// Set a radio button group's label to be marked as 'complete' (change css class)
function setComplete(span)
{
    document.getElementById(span).className = "step";
}

// Validate user input
function validate() 
{
    var isValid = true;

    // Check postcode
    isValid = validatePostcode();
    
    // Check terms and conditions checkbox
    if (document.getElementById('chkTermsConditions').checked == false)
    {
        isValid = false;
        document.getElementById('errorMsg_requiredFields').innerHTML = "You must agree to the terms and conditions to proceed.";
        window.scrollTo(0, 0);
    }
    else
    {
        document.getElementById('errorMsg_requiredFields').innerHTML = "";
    }

    // Check radio button groups to make sure selection is made for each
    var ageChecked = isSelectionMade(document.mainform.rbAge);    
    var genderChecked = isSelectionMade(document.mainform.rbGender);
    var ethnicChecked = isSelectionMade(document.mainform.rbEthnicity);
    var birthChecked = isSelectionMade(document.mainform.rbBirth);
    var familyChecked = isSelectionMade(document.mainform.rbFamily);
    var examinationChecked = isSelectionMade(document.mainform.rbExamination);
    var medicationChecked = isSelectionMade(document.mainform.rbBlood);
    var tobaccoChecked = isSelectionMade(document.mainform.rbTobacco);
    var foodChecked = isSelectionMade(document.mainform.rbFood);
    var exerciseChecked = isSelectionMade(document.mainform.rbActivity);
    var waistChecked = isSelectionMade(document.mainform.rbWaist);

    if (!ageChecked)
        document.getElementById('step2').className = "stepBad";
    else
        document.getElementById('step2').className = "step";

    if (!genderChecked)
        document.getElementById('step3').className = "stepBad";
    else
        document.getElementById('step3').className = "step";

    if (!ethnicChecked)
        document.getElementById('step4').className = "stepBad";
    else
        document.getElementById('step4').className = "step";

    if (!birthChecked)
        document.getElementById('step5').className = "stepBad";
    else
        document.getElementById('step5').className = "step";

    if (!familyChecked)
        document.getElementById('step6').className = "stepBad";
    else
        document.getElementById('step6').className = "step";

    if (!examinationChecked)
        document.getElementById('step7').className = "stepBad";
    else
        document.getElementById('step7').className = "step";

    if (!medicationChecked)
        document.getElementById('step8').className = "stepBad";
    else
        document.getElementById('step8').className = "step";

    if (!tobaccoChecked)
        document.getElementById('step9').className = "stepBad";
    else
        document.getElementById('step9').className = "step";

    if (!foodChecked)
        document.getElementById('step10').className = "stepBad";
    else
        document.getElementById('step10').className = "step";

    if (!exerciseChecked)
        document.getElementById('step11').className = "stepBad";
    else
        document.getElementById('step11').className = "step";

    if (!waistChecked)
        document.getElementById('step12').className = "stepBad";
    else
        document.getElementById('step12').className = "step";

    if (ageChecked == false || genderChecked == false || ethnicChecked == false || birthChecked == false || familyChecked == false || examinationChecked == false ||
        medicationChecked == false || tobaccoChecked == false || foodChecked == false || exerciseChecked == false || waistChecked == false) 
    {
        document.getElementById('errorMsg_requiredFields').innerHTML = "Please complete all questions (incomplete questions marked red).";
        isValid = false;
        window.scrollTo(0, 0);
    }
    
    return isValid;
}

// This function validates the postcode separately
function validatePostcode()
{
    var isValid;

    // Check postcode validity
    var postcode = document.getElementById('txtPostCode').value;
    if (isNaN(postcode) || postcode < 0 || postcode == "" || postcode.length != 4) 
    {
        document.getElementById('errorMsg_postcode').innerHTML = "Please enter 4 digit postcode.";
        document.getElementById('step1').className = "stepBad";
        window.scrollTo(0, 0);
        isValid = false;
    }
    else
    {
        document.getElementById('step1').className = "step";
        document.getElementById('errorMsg_postcode').innerHTML = ""; // reset error message
        isValid = true;
    }

    return isValid;
}

// This function checks a group of radio buttons to see if a selection has been made
function isSelectionMade(buttonGroup)
{
    for (var i = 0; i < buttonGroup.length; i++) 
    {
        if (buttonGroup[i].checked)
            return true;
    }

    return false;
}

function navOver(imgTag)
{        
	var theImage = imgTag;	
	if(theImage.src.indexOf("_off.") != -1)	
	{
		theImage.src = theImage.src.replace("_off.","_on.");		
	}
	else	
	{
		theImage.src = theImage.src.replace("_on.","_off.");	
	}
}

function setColorActive(elem)
{
	elem.style.color = "#000000";
}
function setColorInactive(elem)
{
	elem.style.color = "#C4C8CA";
}

function closeTerms()
{
	var overlay = document.getElementById("overlay");
	var overlayContainer = document.getElementById("overlayContainer");
	overlay.style.display = "none";
	overlayContainer.style.display = "none";
}
function openTerms()
{
	var overlay = document.getElementById("overlay");
	var overlayContainer = document.getElementById("overlayContainer");
	overlay.style.display = "block";
	overlayContainer.style.display = "block";
	
	var sizeatt = getPageSize();
	var scrollatt = getPageScroll();
	var fullHeight = "";
	fullHeight = sizeatt[1];
	
	overlay.style.height = fullHeight + "px";	
	
	if(navigator.userAgent.indexOf("MSIE") != -1)	
		overlay.style.width = (sizeatt[0]) + "px";			
	else
		overlay.style.width = (sizeatt[0] - 17) + "px";	
		
	overlayContainer.style.top = scrollatt[1] + 150 + "px";
}

function getPageScroll()
{
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
		if(navigator.appName.indexOf("Opera") != -1)
		{
			pageHeight = parseInt(document.getElementsByClassName("headerWrapper")[0].offsetHeight) +
				parseInt(document.getElementsByClassName("contentOuter")[0].offsetHeight)
		}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;

}

var gender = "";
var isNative = "";
var RadioButtons = {
	Init: function()
	{
		document.getElementById('radio1').style.display = "none";
		document.getElementById('radio2').style.display = "none";
		document.getElementById('radio3').style.display = "none";
		document.getElementById('radio4').style.display = "none";
		document.getElementById('defaultMessage').style.display = "none";
		RadioButtons.Eval();
	},
	Eval: function()
	{
		//alert(gender + " " + isNative);
		var radio1 = document.getElementById('radio1');
		var radio2 = document.getElementById('radio2');
		var radio3 = document.getElementById('radio3');
		var radio4 = document.getElementById('radio4');
		var theDefault = document.getElementById('defaultMessage');
		if (gender == "" || isNative == "")
		{
			theDefault.style.display = "block";
		}
		else 
		{
			theDefault.style.display = "none";
			if (gender == "male" && isNative == "yes")
				radio1.style.display = "block";
			if (gender == "female" && isNative == "yes")
				radio2.style.display = "block";
			if (gender == "male" && isNative == "no")
				radio3.style.display = "block";
			if (gender == "female" && isNative == "no")
				radio4.style.display = "block";
		}
	},
	SetGender: function(val)
	{
	    // Reset radio buttons
	    for (i = 0; i < document.mainform.rbWaist.length; i++) 
        {
	        document.mainform.rbWaist[i].checked = false;
	    }

        gender = val;
                
		RadioButtons.Init();
	},
	SetNative: function(val)
	{
	    // Reset radio buttons
	    for (i = 0; i < document.mainform.rbWaist.length; i++) 
        {
	        document.mainform.rbWaist[i].checked = false;
	    }

	    var isAsian = false;
	    if (document.getElementById('birth_Asia').checked == true)
	        isAsian = true;

	    if (val == 'no')
        {
            if (document.getElementById('ethnicity_No').checked == true && !isAsian)
                isNative = val;
        }
        else
            isNative = val;
                    
        RadioButtons.Init();
	}
}

function emailUser()
{
	var name = document.getElementById('txtName').Text;
	var email = document.getElementById('txtEmail').Text;
	
	saveNotexhr = GetXmlHttpObject();
	var url = "ajax/Contact.aspx";
	var queryString = "";
	queryString += "&name=" + name;
	queryString += "&email=" + email;
	
	saveNotexhr.open("POST",url,true);
	saveNotexhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	saveNotexhr.setRequestHeader("Content-length", queryString.length);
	saveNotexhr.setRequestHeader("Connection", "close");
	saveNotexhr.send(queryString);
}

var address = "";
function doRedirect(theAddress)
{
	address = theAddress;
	var overlay;
	var bodyTag = document.getElementsByTagName("body")[0];
	if (!document.getElementById("overlay"))
	{
		var newOverlay = document.createElement("div");
		newOverlay.id = "overlay";
		bodyTag.appendChild(newOverlay);
	}
	overlay = document.getElementById("overlay");
	var overlayContainer = document.createElement("div");
	overlayContainer.id = "redirectContainer";
	overlayContainer.className = "overlayContainer";
	bodyTag.appendChild(overlayContainer);
	overlay.style.display = "block";
	overlayContainer.style.display = "block";
	
	var popupContent = document.createElement("div");
	popupContent.className = "redirectMessage";
	popupContent.innerHTML = "<p>Diabetes Australia Queensland is currently in the process of rebuilding our website.</p><p>&nbsp;</p><p>Please wait while you are re-directed to our old website.</p>";
	var popupImg = document.createElement("img");
	popupImg.src = "images/ajax-loader.gif";
	popupContent.appendChild(popupImg);
	overlayContainer.appendChild(popupContent);
	
	var sizeatt = getPageSize();
	var scrollatt = getPageScroll();
	var fullHeight = "";
	fullHeight = sizeatt[1];
	
	overlay.style.height = fullHeight + "px";	
	
	if(navigator.userAgent.indexOf("MSIE") != -1)	
		overlay.style.width = (sizeatt[0]) + "px";			
	else
		overlay.style.width = (sizeatt[0] - 17) + "px";	
		
	overlayContainer.style.top = scrollatt[1] + 150 + "px";
	setTimeout('goToUrl()', 3000);
}

function goToUrl()
{
	var overlay = document.getElementById("overlay");
	var overlayContainer = document.getElementById("redirectContainer");
	overlay.parentNode.removeChild(overlay);
	overlayContainer.parentNode.removeChild(overlayContainer);
	document.location = address;
}
function evalSearch()
{
	var searchUrl = "http://www.diabetesqld.org.au/index1.php?&ref=MTEwMQ==&redir=index1.php&selectedmenu=";
	var searchValue = document.getElementById("searchValue").value;
	searchUrl += "&search=" + searchValue;
	doRedirect(searchUrl);
}
function fileSelected()
{
	var fileName = document.getElementById("theFileName");
	var textField = document.getElementById("fileUpload");
	fileName.value = textField.value;
}


