// JavaScript Document

var xmlHttp;
var blankImage = new Image();
blankImage.src = 'blank.gif';

var locks = new Object();
locks.amount = 0;
locks.table = new Array();

function logcheck()
{

	var msg  = '';
	
	if(document.form1.loginname.value == '')
	{
		alert("Please enter a username.");
		return false;
	}
	
	
	if(document.form1.loginname.value.length < 6)
	{
		alert("Username must be 6 characters or longer.");
		return false;	
	}
	
	
	var url="usernamecheck.php?un=" + document.form1.loginname.value;
	xmlHttp=GetXmlHttpObject(usercheck);
	xmlHttp.open("GET", url , true);
	//window.open(url);
	xmlHttp.send(null);

	
}

function usercheck() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	var resp = xmlHttp.responseXML; 
	var stat = resp.getElementsByTagName('stat')[0].firstChild.data;
	
		if (stat == "TAKEN")
		{
			alert("This username has already been taken.");
			document.form1.loginname.value = '';
		}
	} 
}

function validateform(theform)
{

	var msg = '';

	if(document.form1.loginname.value == '')
	{
		msg = msg + 'Please enter a username\n';
	}
	
	
	if(document.form1.loginname.value.length < 6)
	{
		msg = msg + 'Username must be longer than 6 characters\n';
	}
	
	if(theform.pass.value == '')
	{
		msg = msg + 'Please enter a password\n';
	}
	

	if(theform.pass.value.length < 6)
	{
		msg = msg + 'Password must be longer than 6 characters\n';

	}

	if(theform.confpass.value == '')
	{
		msg = msg + 'Please confirm your password\n';
	}
	
	if(theform.confpass.value != theform.pass.value)
	{
		msg = msg + 'Passwords dont match\n';
	}
	
	dropdownIndex = document.getElementById('Salutation').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select your title\n';
	}
	
	if(theform.FirstName.value == '')
	{
		msg = msg + 'Please enter your first name\n';
	}
	
	if(theform.LastName.value == '')
	{
		msg = msg + 'Please enter your last name\n';
	}
	
	if(theform.Address.value == '')
	{
		msg = msg + 'Please enter your address\n';
	}
	
	if(theform.Address2.value == '')
	{
		msg = msg + 'Please enter the second line of your address\n';
	}
	
	if(theform.TownCity.value == '')
	{
		msg = msg + 'Please enter your town/city\n';
	}
	
	if(theform.Postcode.value == '')
	{
		msg = msg + 'Please enter your postcode\n';
	}
	
	dropdownIndex = document.getElementById('Country').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select your country\n';
	}
	else
	{
	
	if (dropdownIndex == 53)
		{
			statedropdownIndex = document.getElementById('state').selectedIndex;
			if (statedropdownIndex == 0) 
			{
				msg = msg + 'Please select your UK State\n';
			}
		}
		
		if (dropdownIndex == 55)
		{
			statedropdownIndex = document.getElementById('usstate').selectedIndex;
			if (statedropdownIndex == 0) 
			{
				msg = msg + 'Please select your US State\n';
				document.getElementById('state').disabled = true;
			}
		}
	}
	
	
	if(theform.TelWork.value == '')
	{
		msg = msg + 'Please enter your work telephone\n';
	}
	
	if(theform.TelHome.value == '')
	{
		msg = msg + 'Please enter your home telephone\n';
	}
	
	if(theform.TelMob.value == '')
	{
		msg = msg + 'Please enter your mobile number\n';
	}
	
	if(theform.Email.value == '')
	{
		msg = msg + 'Please enter your email address\n';
	}
	
		// test if valid email address, must have @ and .
	var checkEmail = "@.";
	var checkStr = theform.Email.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
	if (ch == checkEmail.charAt(j) && ch == "@")
	EmailAt = true;
	if (ch == checkEmail.charAt(j) && ch == ".")
	EmailPeriod = true;
		  if (EmailAt && EmailPeriod)
			break;
		  if (j == checkEmail.length)
			break;
		}
		// if both the @ and . were in the string
	if (EmailAt && EmailPeriod)
	{
			EmailValid = true
			break;
		}
	}
		if (!EmailValid)
		{
		msg = msg + 'Please enter a valid email address\n';
		}
	
	
	if(theform.BirthDay.value == '')
	{
		msg = msg + 'Please enter your birth day\n';
	}
	
	if(theform.BirthMonth.value == '')
	{
		msg = msg + 'Please enter your birth month\n';
	}
	
	if(theform.BirthYear.value == '')
	{
		msg = msg + 'Please enter your birth year\n';
	}

	
	dropdownIndex = document.getElementById('MaritalStatus').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select your marital status\n';
	}
	
//	dropdownIndex = document.getElementById('Ethnic').selectedIndex;
//	if (dropdownIndex == 0) 
//	{
//	msg = msg + 'Please select your ethnic group\n';
//	}
	
	dropdownIndex = document.getElementById('EMPLOYMENT').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select your occupation\n';
	}
	
	dropdownIndex = document.getElementById('ShowPro').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select your favourite type of program\n';
	}
	
	dropdownIndex = document.getElementById('HearFrom').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select how you heard about us\n';
	}
	
	dropdownIndex = document.getElementById('MainIntrest').selectedIndex;
	if (dropdownIndex == 0) 
	{
	msg = msg + 'Please select your interests/hobbies\n';
	}
	
	if(theform.Artist.value == '')
	{
		msg = msg + 'Please select your favourite band/artist\n';
	}

	
	if (msg != "")
	{
	alert(msg);
	return false;
	}


}


function validate()
{
	
	var dropdownIndex = document.getElementById('Country').selectedIndex;
	var dropdownValue = document.getElementById('Country')[dropdownIndex].value;	
//	alert(dropdownValue);
//	return false;
	if (dropdownValue == "USA")
	{
	document.getElementById("states").style.display = "none"; 
	document.getElementById("USSTATES").style.display = "block"; 
	}
	else
	{
	document.getElementById("USSTATES").style.display = "none"; 
	document.getElementById("states").style.display = "block"; 
	}

}

function disdrop()
{
	
	var dropdownIndex = document.getElementById('Country').selectedIndex;
	var dropdownValue = document.getElementById('Country')[dropdownIndex].value;	
//	alert(dropdownValue);
//	return false;
	if (dropdownValue == "USA")
	{
	document.getElementById('state').disabled = true;
	document.getElementById('usstate').disabled = false;
	}
	
	if (dropdownValue == "United Kingdom")
	{
	document.getElementById('state').disabled = false;
	document.getElementById('usstate').disabled = true;
	}

}


function loginverify(form)
{ 

	if(form.username2.value == '')
	{
		alert("Please enter your username");
		form.username2.focus();
		return false;	
	}
	
	if(form.password2.value == '')
	{
		alert("Please enter your password");
		form.password2.focus();
		return false;	
	}
	
	
//addLock('loginasp');
	
var url="loginverify.php?un=" + form.username2.value + "&pw=" + form.password2.value + "&sid=" + form.showid.value;
xmlHttp=GetXmlHttpObject(stateChanged);
xmlHttp.open("GET", url , true);
//window.open(url);
xmlHttp.send(null);
} 

function loginverifytop(form)
{ 

	if(form.username2.value == '')
	{
		alert("Please enter your username");
		form.username2.focus();
		return false;	
	}
	
	if(form.password2.value == '')
	{
		alert("Please enter your password");
		form.password2.focus();
		return false;	
	}
	
	
addLock('loginasp');
	
var url="loginverifytop.php?un=" + form.username2.value + "&pw=" + form.password2.value;
xmlHttp=GetXmlHttpObject(newstateChanged);
xmlHttp.open("GET", url , true);
//window.open(url);
xmlHttp.send(null);
} 

function loginverifyres(form)
{ 

	if(form.username2.value == '')
	{
		alert("Please enter your username");
		form.username2.focus();
		return false;	
	}
	
	if(form.password2.value == '')
	{
		alert("Please enter your password");
		form.password2.focus();
		return false;	
	}
	
	
addLock('loginasp');
	
var url="loginverifytop.php?un=" + form.username2.value + "&pw=" + form.password2.value;
xmlHttp=GetXmlHttpObject(newstateChangedbook);
xmlHttp.open("GET", url , true);
//window.open(url);
xmlHttp.send(null);
} 



function stateChanged() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	var resp = xmlHttp.responseXML; 
	var stat = resp.getElementsByTagName('stat')[0].firstChild.data;
	
		if (stat == "Welcome Back to Applause Store the No.1 Official Television and Radio Audience Ticket Destination")
		{
			alert(stat);
			var redi = resp.getElementsByTagName('redirecto')[0].firstChild.data;
			window.location.href = redi;
		}
		else
		{
		alert(stat);
		}
	
	} 
} 

function newstateChanged() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	
	removeLock('loginasp');

	var resp = xmlHttp.responseXML; 
	var stat = resp.getElementsByTagName('stat')[0].firstChild.data;
	var sURL = unescape(window.location.pathname);
	
	alert (stat);

		if (stat == "Welcome Back to Applause Store the No.1 Official Television and Radio Audience Ticket Destination")
		{
		window.location.reload( true );
		}
		
		if (stat == "Login Failed")
		{
			if(confirm("Do you want to be reminded of your username/password?"))
		    {	
			var redi = resp.getElementsByTagName('redirecto')[0].firstChild.data;
			window.location.href = redi;
			}
		}
		
		if (stat == "Account not activated")
		{
		var redi = resp.getElementsByTagName('redirecto')[0].firstChild.data;
		window.location.href = redi;
		}
	} 
} 

function newstateChangedbook() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	
	removeLock('loginasp');

	var resp = xmlHttp.responseXML; 
	var stat = resp.getElementsByTagName('stat')[0].firstChild.data;
	var sURL = unescape(window.location.pathname);
	
	alert (stat);

		if (stat == "Welcome Back to Applause Store the No.1 Official Television and Radio Audience Ticket Destination")
		{
		window.location.reload( true );
		}
		
		if (stat == "Login Failed")
		{
			if(confirm("Do you want to be reminded of your username/password?"))
		    {	
			var redi = resp.getElementsByTagName('redirecto')[0].firstChild.data;
			window.location.href = redi;
			}
		}
		
		if (stat == "Account not activated")
		{
		var redi = resp.getElementsByTagName('redirecto')[0].firstChild.data;
		window.location.href = redi;
		}
	} 
} 





function GetXmlHttpObject(handler)
{ 
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This site doesn't work with Opera") 
return 
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{ 
var strName="Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
} 
try
{ 
objXmlHttp=new ActiveXObject(strName)
objXmlHttp.onreadystatechange=handler 
return objXmlHttp
} 
catch(e)
{ 
alert("Error. Scripting for ActiveX/Javascript might be disabled") 
return 
} 
} 
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXmlHttp.onload=handler
objXmlHttp.onerror=handler 
return objXmlHttp
}
} 





function addLock(name)
{
	if(locks.amount == 0)
		lockScreen();
	
	if(!locks[name] || locks[name] == '')
	{
		locks.table[name] = locks.length;
		locks.amount++;
	}
};

function removeLock(name)
{
	if(locks.table[name] != '')
	{
		locks.table[name] = '';
		locks.amount--;
	}

	if(locks.amount == 0)
		unlockScreen();
};

function lockScreen()
{
	document.getElementById('page_cover').style.display = 'block';
	document.getElementById('loadTable').style.display = 'block';

};

function unlockScreen()
{
	document.getElementById('page_cover').style.backgroundImage = "url('semi-transparent.gif')";
	document.getElementById('loadTable').style.display = 'block';
	document.getElementById('page_cover').style.display = 'none';
};

function resizeTasks()
{
	try
	{
		document.getElementById('page_cover').style.height = document.documentElement.scrollHeight + 'px';
	}
	catch(e)
	{
		
	}
};

function conf()
{
	if(confirm("Are you sure you wish to book this date?"))
    {	
	

	}
	else
	{
	return false;
	}
}
window.onresize = resizeTasks;
