var countryCsvString="";
var onProfile=false;
var onProfileDigital=false;

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function get_radio_value(item) {
	for (var i=0; i < item.length; i++)
	{
		if (item[i].checked)
		{
			var rad_val = item[i].value;
		}
	}
	return rad_val;
}


function hide_questions() {
	if(document.forms[0].country.value=="") {
		document.getElementById("webreg_container").style.display="none";
	} else {
		off_profile_check(document.forms[0].country.value,document.forms[0].QJ101);
	}
}

function display_questions() {
	document.getElementById("webreg_container").style.display="";
}

function getXmlHttpObject() {
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function off_profile_check(myCountry,myDeliveryMethod) {
	var xmlHttp=getXmlHttpObject();
	var countryArray;
	var rad_val=get_radio_value(myDeliveryMethod);
	var journalCode=document.forms[0].jnlcode.value;
	var printArray=new Array("PRINT","WEBA1");
	var digitalArray=new Array("PDF","WEBA2");
	
	function check_print_digital() {
		if(printArray.inArray(rad_val) && onProfile==false) {
			document.getElementById("submitme").disabled=true;
			document.getElementById("off_profile_div").style.display="";
			document.getElementById("off_profile_div_digital").style.display="none";
		} else if(digitalArray.inArray(rad_val) && onProfileDigital==false) {
			document.getElementById("submitme").disabled=true;
			document.getElementById("off_profile_div").style.display="none";
			document.getElementById("off_profile_div_digital").style.display="";
		} else {
			document.getElementById("submitme").disabled=false;
			document.getElementById("off_profile_div").style.display="none";
			document.getElementById("off_profile_div_digital").style.display="none";
		}
	}
	
	function stateChanged(mode) {
		if (xmlHttp.readyState == 4) {

			countryCsvString=xmlHttp.responseText;
			countryArray=countryCsvString.split(',');
			onProfile=false;
			onProfileDigital=false;

			for (c in countryArray) {
				if(countryArray[c]==myCountry) {
					if(mode=="PRINT") {
						onProfile=true;
					} 
					if(mode=="PDF") {
						onProfileDigital=true;
					}
				}
			}

			check_print_digital();
		}
	}
	
	/* Run off profile checks based on delivery method */
	
	if(printArray.inArray(rad_val)) {
		
		/* Off profile checking for print copy.... */
		
		xmlHttp.open("GET","http://www.wdis.co.uk/websubs/IOP/onProfileList.php?journalCode="+journalCode,true);
		xmlHttp.onreadystatechange=function() {
			stateChanged("PRINT");
		}
		xmlHttp.send(null);
		
	} else if(digitalArray.inArray(rad_val)) {
		
		/* Off profile checking for digital copy.... */
		
		xmlHttp.open("GET","http://www.wdis.co.uk/websubs/IOP/onProfileList.php?mode=digital&journalCode="+journalCode,true);
		xmlHttp.onreadystatechange=function() {
			stateChanged("PDF");
		}
		xmlHttp.send(null);
		
	} else {
		/* Default action......*/
	}
}

function event_country_change(myCountry) {
	if(myCountry!="") {
		document.getElementById("submitme").disabled=false;
//		document.getElementById("webreg_container").style.display="";
		off_profile_check(document.forms[0].country.value,document.forms[0].QJ101);
	} else {
		document.getElementById("submitme").disabled=true;
//		document.getElementById("webreg_container").style.display="none";
	}
}

function noCTRL(id,e)
{
	var code = (document.all) ? event.keyCode:e.which;

	if (parseInt(code)==17) //CTRL
	{
//		document.getElementById(id).value="";
		document.getElementById(id).blur();
		return false;
	}
}