var character;
var cFound;
var cFoundText;
var itemSubmit;
var xmlhttp;
var paypalRecipient;
var giftCheck;
var giftTo;
var titleForm;
var paypalForm;
var titles;

function getSelectedTitleStats()
{
	for(var i = 0; i < titleForm.titleRadio.length; i++)
	{
		if(titleForm.titleRadio[i].checked)
			return [titleForm.titleRadio[i].value, titleForm.titlePrice[i].value, titleForm.titleID[i].value];
	}
	return null;
}

function characterChange()
{
	if(recipient.value == "")
	{
		cFoundText.innerHTML = "";
		return;
	}
	
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    else if (window.ActiveXObject) // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	
	characterSearching();
	
    xmlhttp.onreadystatechange=xmlLoaded;
    
    var validateURL = "store/findTitles.php?skyrateName=" + recipient.value;
    xmlhttp.open("GET", validateURL, true);
    xmlhttp.send(null);
}

function xmlLoaded()
{
	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.responseText == "")
			characterNotFound();
		else
			characterVerified();
	} 
}

function characterSearching()
{
	cFoundText.innerHTML = "searching...";
	cFoundText.style.color = "#000000";
}

function characterNotFound()
{
	cFoundText.innerHTML = "not found";
	cFoundText.style.color = "#ff0000";
}

function characterVerified()
{
	cFoundText.innerHTML = "verified";
	cFoundText.style.color = "#00ff00";
	
	var titleStr = xmlhttp.responseText.split(":")[1];
	titles = titleStr.split(",");
}

function giftChange()
{
	if(giftCheck.checked)
		giftTo.style.display = "";
	else
		giftTo.style.display = "none";
}

function confirmGiftTitle(titleID)
{
	for(var i=0; i<titles.length-1; i++)
	{
		if(titleID == titles[i])
		{
			if(confirm(paypalRecipient.value + " already has that title.  To buy it anyway, press OK."))
				return true;
			else
				return false;
		}
	}
	
	return true;
}

function validateForm()
{
	var titleTriplet = getSelectedTitleStats();
	
	if(titleTriplet == null)
	{
		alert("Please choose a title.");
		return false;
	}
	
	paypalTitle.value = titleTriplet[0];
	paypalPrice.value = titleTriplet[1];
	
	if(giftCheck.checked)
	{
		if(cFoundText.innerHTML == "verified")
		{
			paypalRecipient.value = recipient.value;
			if(!confirmGiftTitle(titleTriplet[2]))
				return false;
		}
		else if (recipient.value == "")
		{
			alert("Enter a character name or uncheck the gift box.");
			return false;			
		}
		else
		{
			alert("Invalid character name.");
			return false;
		}
	}
	else
		paypalRecipient.value = skyrateName;
	
	if(!tosCheck.checked)
	{
		alert("You must accept the Terms of Service.");
		return false;			
	}
	
	return true;
}

function submitForm()
{
	if(validateForm())
		paypalForm.submit();
}

function initVariables()
{
	recipient = document.getElementById("recipient");
	cFoundText = document.getElementById("cFoundText");
	itemSubmit = document.getElementById("itemSubmit");
	
	giftCheck = document.getElementById("giftCheck");
	giftTo = document.getElementById("giftTo");
	
	titleForm = document.getElementById("titleForm");
	
	paypalForm = document.getElementById("paypalForm");
	paypalRecipient = document.getElementById("paypalRecipient");
	paypalTitle = document.getElementById("paypalTitle");
	paypalPrice = document.getElementById("paypalPrice");
	
	tosCheck = document.getElementById("tosCheck");
}