var cObj=null;
var cObj2=null;
var itemSubtotal;
var itemName;
var itemNum;
var shippingTotal;
var studyGuideTotal;
var studyGuideOnlineSysTotal;
var duplicateRegDialog = null;
var genderfemale=false;
var receive="";
var notify="";
var licenseType="";
var licenseNum="";
var quantity;
var unitPrice;
var register = true;
var specialty;


var responseSuccess = function(o) {
	try
	{
	var program = document.getElementById('program');

	clearOptionList(program);
	
	//instantiate the W3C DOM Parser
	var parser = new DOMImplementation();

	//load the XML into the parser and get the DOMDocument
	var domDoc = parser.loadXML(o.responseText);

	//get the root node (in this case, it is ROOTNODE)
	var docRoot = domDoc.getDocumentElement();

	var domNodeList = docRoot.getChildNodes();
	var nodes = domNodeList.getLength();
	for(i=0;i<nodes;i++)
	{
		var programdata = docRoot.getElementsByTagName("program").item(i);
		var id = programdata.getElementsByTagName("id").item(0).getFirstChild().getNodeValue();
		var name = programdata.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
		
		addOption(program, id, name);
	}
	addOption(program, -1, "Other Program");

	// Reset the program other field
	programother = document.getElementById('programother');
	if (program.value == -1)
	{
		programother.disabled = false;
	}
	else
	{
		programother.value = "";
		programother.disabled = true;
	}
	}
	catch(e)
	{
		alert('b');
	}
}

var responseSuccess2 = function(o)
{
	//alert(o.responseText);
	programs = eval("("+o.responseText+")");
	
	var program = document.getElementById('program');

	clearOptionList(program);
	
	for(i=0;i<programs.length;i++)
	{
		addOption(program, programs[i].id, programs[i].name);
	}
	addOption(program, -1, "Other Program");

	// Reset the program other field
	programother = document.getElementById('programother');
	if (program.value == -1)
	{
		programother.disabled = false;
	}
	else
	{
		programother.value = "";
		programother.disabled = true;
	}
	
}

var successExistingEmail = function(o)
{
	var code = eval('(' + o.responseText + ')');
	
	if(code.emailCode=="one")//email already exists
		duplicateRegDialog.show(); //alert("Email address already exists. Please choose another email."+"\n\n"+"If you feel you have received this in error, please contact us at support@etasonline.com");
	else
		//register user and send of email/thank you page
		//processRegistration();
		//sendToPaypal();
		insertUser();
}

var successUserInfo = function(o)
{
	//document.registration.submit();
	window.location = "http://www.lulu.com/commerce/index.php?fBuyContent=5259953"
}

var failureUserInfo = function(o)
{
	alert("Failed to insert user into database");
}

var responseFailure = function(o) {
	alert(o.responseText);
	alert('Failed to retrieve list of programs for the selected state.');
}

var failureExistingEmail = function(o)
{
	alert("Failed to determine if email already exists");
}

var callback = {
	success: responseSuccess2,
	failure: responseFailure,
	scope: this
}

var callbackExistingEmail = {
	success: successExistingEmail,
	failure: failureExistingEmail,
	scope: this
}

var callbackUserInfo= {
	success: successUserInfo,
	failure: failureUserInfo,
	scope: this
}

function onLoad(spec)
{
	specialty=spec;
	// When the document loads populate the program list with the first state.
	var state = document.getElementById('state');

	var cObj = YAHOO.util.Connect.asyncRequest('GET', 'getprogrambystate.php?state='+state.value, callback, null);
	
	duplicateRegDialog = new YAHOO.widget.Dialog("dlg", { 
	//width:"33em",
	fixedcenter:true,
	modal:true,
	visible:false,
	draggable:false });
	duplicateRegDialog.setHeader("Registration Duplication");
	//dlg.setBody("<p style=\"text-align:center\">Unknown user name</p><p style=\"text-align:center\">If you experience problems logging in, please contact ETAS at <a href=\"mailto:support@pce-corp.com?subject=SDPA%20Login%20Difficulties\">support@pce-corp.com</a> or (212) 213-5436.</p>");

	duplicateRegDialog.setBody("<p style=\"text-align:center\">Email address already exists. Please choose another email.</p><p style=\"text-align:center\"> If this is an error, please contact us at <a href=\"mailto:support@etasonline.com?subject=Duplicate Registration\">support@etasonline.com</a>");	
	duplicateRegDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
	duplicateRegDialog.render(document.body);
}

//calculates item subtotal
function calcSubtotal()
{
	/*if(document.getElementById("studyGuide").value =="")
		studyGuideTotal = 0;
	else
		studyGuideTotal = (document.getElementById("studyGuide").value) * (199);
	
	if(document.getElementById("studyGuideOnlineSys").value =="")
		studyGuideOnlineSysTotal = 0;
	else
		studyGuideOnlineSysTotal = (document.getElementById("studyGuideOnlineSys").value) * (239);*/
	
	if(document.getElementById("studyGuide").value =="")
		document.getElementById("studyGuideOnlineSys").disabled=false;

	else
		document.getElementById("studyGuideOnlineSys").disabled=true;
	
	if(document.getElementById("studyGuideOnlineSys").value =="")
		document.getElementById("studyGuide").disabled=false;
	else
		document.getElementById("studyGuide").disabled=true;
	
	
	if(document.getElementById("studyGuide").disabled)
	{
		quantity=document.getElementById("studyGuideOnlineSys").value;
		unitPrice = 349;
		itemSubtotal = quantity*unitPrice;
		itemName = "Study Guide Online System";
		itemNum = "podOnline";
		//itemSubtotal= (document.getElementById("studyGuideOnlineSys").value) * (239);
	}
	else
	{
		quantity=document.getElementById("studyGuide").value;
		unitPrice = 299;
		itemSubtotal = quantity*unitPrice;
		itemName = "Study Guide";
		itemNum = "podStudy";
		shippingTotal = 10*quantity;
		document.getElementById("shipping").value=shippingTotal;

		//itemSubtotal = (document.getElementById("studyGuide").value) * (199);
	}
	
	if (document.getElementById("subtotal").hasChildNodes())
	{
		tmpEl1 = document.getElementById("subtotal").firstChild;
		while(tmpEl1)
		{
			tmpEl2 = tmpEl1.nextSibling;
			tmpEl1.parentNode.removeChild(tmpEl1);
			tmpEl1 = tmpEl2;
		}
	}
	
		tmpCell = document.createElement("TD");
		tmpCell.style.width = "230";
		tmpCell.style.height = "45";
		tmpTxt = document.createTextNode('$'+itemSubtotal+'.00');
		tmpCell.appendChild(tmpTxt);
		document.getElementById("subtotal").appendChild(tmpCell);

	//document.getElementById("subtotal").innerHTML='<td width="230" height="45" alt="" >$'+itemSubtotal+'.00</td>'; DOESN'T WORK FOR INTERNET EXPLORER
	
	document.getElementById("amount").value=unitPrice;
	document.getElementById("quantity").value=quantity;	
	document.getElementById("item_name").value=itemName;
	document.getElementById("item_number").value=itemNum;
}

function checkKeyPress()
{
	if(document.getElementById("studyGuideOnlineSys").value!=1)
		document.getElementById("studyGuideOnlineSys").value="";
}

function onChangeCategory()
{
	categoryother = document.getElementById('categoryother');
	category = document.getElementById('category');

	if (category.value == -1)
	{
		categoryother.disabled = false;
	}
	else
	{
		categoryother.value = "";
		categoryother.disabled = true;
	}
}

function onChangeState()
{
	try
	{
	var state = document.getElementById('state');
	/*if(YAHOO.util.Connect.isCallInProgress(cObj))
		return false;*/
	//alert(state.value);
	cObj = YAHOO.util.Connect.asyncRequest('GET', 'getprogrambystate.php?state='+state.value, callback, null);
	
	}
	catch(e)
	{
		alert('a');
	}
}

function onChangeProgram()
{
	programother = document.getElementById('programother');
	program = document.getElementById('program');

	if (program.value == -1)
	{
		programother.disabled = false;
	}
	else
	{
		programother.value = "";
		programother.disabled = true;
	}
}

function onChangeEstablish()
{
	establishother = document.getElementById('establishother');
	establish = document.getElementById('establish');

	if (establish.value == -1)
	{
		establishother.disabled = false;
	}
	else
	{
		establishother.value = "";
		establishother.disabled = true;
	}
}

function validationError(msg)
{
	alert(msg);
	return false;
}

function validateRegistration()
{
	if (document.registration.firstname.value == "")
		return validationError("please specify first name");

	if (document.registration.lastname.value == "")
		return validationError("please specify last name");

	if(specialty!="podiatry")//possibly omit this check as order.js only belongs to podiatry. remove category check entirely
	{
		if (document.registration.category.value == -1)
			if (document.registration.categoryother.value == "")
				return validationError("please specify other category");
	}
			
	if (document.registration.state.value == "999")
		return validationError("please specify a state");

	if (document.registration.program.value == -1)
		if (document.registration.programother.value == "")
			return validationError("please specify other program");
			
	if (document.registration.email.value == "")
		return validationError("please specify email");

	if (document.registration.confirmemail.value == "")
		return validationError("please confirm email");
	
	if (document.registration.email.value != document.registration.confirmemail.value)
		return validationError("confirm email and email do not match");
	
	if (document.registration.password.value == "")
		return validationError("please specify password");

	if (document.registration.confirmpassword.value == "")
		return validationError("please confirm password");

	if (document.registration.password.value != document.registration.confirmpassword.value)
		return validationError("confirm password and password do not match");

	if (document.registration.establish.value == -1)
		if (document.registration.establishother.value == "")
			return validationError("please specify how you've heard about us");
			
	
	if (document.getElementById("agree").checked == false)
			return validationError("please read the terms of use & privacy policy and indicate your agreement");


	return true;
	//return false;
}

function checkExistingEmail()
{
	email = document.registration.email.value;
	cObj2 = YAHOO.util.Connect.asyncRequest("POST", "checkExistingEmail.php",callbackExistingEmail,"email="+email);
}

function sendToPaypal()
{		
		var title = document.registration.title.value+"|";
		var firstname = document.registration.firstname.value+"|";
		var middleinitial = document.registration.middleinitial.value+"|";
		var lastname = document.registration.lastname.value+"|";
		var category = document.registration.category.value+"|";
		var categoryother = document.registration.categoryother.value+"|";
		var state = document.registration.state.value+"|";
		var program = document.registration.program.value+"|";
		var programother=document.registration.programother.value+"|";
		var password = document.registration.password.value+"|";
		var establish = document.registration.establish.value+"|";
		var establishother = document.registration.establishother.value+"|";
		var confirmation_key=""+"|";
		var specialty_id=3+"|";
		var receive = ""+"|";
		var notify = ""+"|";
		var licenseType = ""+"|";
		var licenseNum = ""+"|";
		var email = document.registration.email.value+"|";
		
		
		//if the study guide online system input box is disabled, then the study guide input box is enabled. Therefore set register to false and registrant info should not be entered into the database
		if(document.getElementById("studyGuideOnlineSys").disabled)
			register = false;
		
		if(document.getElementById("studyGuide").disabled)
			document.getElementById("return").value = "http://pod.kenya.testhost.com/thankyou.php";

		
		if(document.registration.Gender.value==2)
			genderfemale=true+"|";

		var registrantInfo = title+firstname+middleinitial+lastname+category+categoryother+state+program+programother+password+establish+establishother+confirmation_key+specialty_id+receive+notify+licenseType+licenseNum+email+genderfemale+register;
		document.getElementById("custom").value= registrantInfo;
		document.registration.submit();
		
		//window.location = "https://www.sandbox.paypal.com/cgi-bin/webscr";	
}


function insertUser()
{
		var title = document.registration.title.value;
		var firstname = document.registration.firstname.value;
		var middleinitial = document.registration.middleinitial.value;
		var lastname = document.registration.lastname.value;
		//var category = document.registration.category.value;
		//var categoryother = document.registration.categoryother.value;
		var state = document.registration.state.value;
		var program = document.registration.program.value;
		var programother=document.registration.programother.value;
		var password = document.registration.password.value;
		var establish = document.registration.establish.value;
		var establishother = document.registration.establishother.value;
		
		//var confirmation_key="";
		var specialty_id=3;
		//var receive = "";
		//var notify = "";
		//var licenseType = "";
		//var licenseNum = "";
		var email = document.registration.email.value;
		var graduate_yr = document.registration.graduate_yr.value;
		
		if(document.registration.Gender.value==2)
			genderfemale=true;
	
		cObj2 = YAHOO.util.Connect.asyncRequest("POST", "insert_user.php",callbackUserInfo,"title="+title+"&firstname="+firstname+"&middleintial="+middleinitial+"&lastname="+lastname+"&state="+state+"&program="+program+"&programother="+programother+"&password="+password+"&establish="+establish+"&establishother="+establishother+"&specialty_id="+specialty_id+"&confirmed="+"&email="+email+"&graduate_yr="+graduate_yr+"&genderfemale="+genderfemale);
}


function processRegistration()
{	
		title = document.registration.title.value+"|";
		firstname = document.registration.firstname.value+"|";
		middleinitial = document.registration.middleinitial.value+"|";
		lastname = document.registration.lastname.value+"|";
		category = document.registration.category.value+"|";
		categoryother = document.registration.categoryother.value+"|";
		state = document.registration.state.value+"|";
		program = document.registration.program.value+"|";
		programother=document.registration.programother.value+"|";
		password = document.registration.password.value+"|";
		establish = document.registration.establish.value+"|";
		establishother = document.registration.establishother.value+"|";
		
		if(document.registration.Gender.value==2)
			genderfemale=true;
		
		document.getElementById("custom").value= title+firstname+middleinitial+lastname+category+categoryother+state+program+programother+password+establish+establishother+genderfemale;
	
		/*if(specialty=="pediatrics")
		{
			if(document.registration.receive.checked)
				receive="yes";
			else
				receive="no";
		
			if(document.registration.notify.checked)
				notify="yes";
			else
				notify="no";
		
			if(document.registration.licenseNum.value!="")
			{
				licenseNum=document.registration.licenseNum.value;
				licenseType=document.registration.licenseType.value;
			}
			else
			{
				licenseNum=0;
				licenseType="State License";
			}
		}
		
		
		if(specialty=="psychiatry")
			accesscode=document.registration.accesscode.value;
		else
			accesscode = 0;*/
		
		//document.registration.gender3.checked;
		
		//window.location = "processregistration.php?email="+email+"&title="+title+"&firstname="+firstname+"&middleinitial="+middleinitial+"&lastname="+lastname+"&genderfemale="+genderfemale+"&category="+category+"&categoryother="+categoryother+"&state="+state+"&program="+program+"&programother="+programother+"&password="+password+"&establish="+establish+"&establishother="+establishother+"&receive="+receive+"&notify="+notify+"&licenseNum="+licenseNum+"&licenseType="+licenseType+"&accesscode="+accesscode;	
}