/*
- required by giftcertificates.html
*/
/* -- Physical GCs --------------------------------------------------------------------------------------------------------------- */
function orderPGC() {
	
	// order a printed GC
	// called by the add to cart button
	// prevalidates the GC forum via ajax
	// see processAddPGC_result() for post validation processing

	var pgcform = document.pgcform;

	if (validateSubmission('p')) {
		
		var loadnotice	= "submitting, please wait...";
		AjaxRequest.submit(
			pgcform,
			{	'url'		: 'http://www.dragondoor.com/cgi-bin/gc.pl',
				'parameters':{ 'validatePGC' : '1'},
				'timeout'	: 30*1000,
				'onTimeout' : function(req) { alert('this request timed out'); },
				'onSuccess'	: function(req) { processAddPGC_result(req.responseText); },
				'onLoading'	: function(req) { writeToElement('PGCmsgdiv',loadnotice); }
			}
		);
		
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------- */
function processAddPGC_result(respobj) {
	
	//writeToElement('PGCmsgdiv', respobj);
	
	var pgcform = document.pgcform;
	
	eval("var addGCresult = ("+respobj+")");

	if (addGCresult) {
		if (addGCresult.error) 		{	alert(addGCresult.error) };
		if (addGCresult.success)	{	pgcform.submit();	}
		//if (addGCresult.error) 		{	writeToElement('PGCmsgdiv', addGCresult.error) };
		//if (addGCresult.success)	{	writeToElement('PGCmsgdiv', addGCresult.success)	}
	} else {
		alert('Unexpected error - unable to submit gift certificate');
	}
	
}
/* ------------------------------------------------------------------------------------------------------------------------------- */
function loadPGCrec(gcobj) {
	
	// process AJAX response to request sent by loadGC()
	// loads the Physical/Printed GC UI form with a specified GC
	// passed a JS object containing the GC data

	if (gcobj) {	

		var form = document.getElementById("pgcform").elements;
		
		//gcrec	= false;
		//alert(gcobj);

		eval("var gcrec = ("+gcobj+")");
		if (gcrec) {

			form['gcid'].value				= gcrec.gcid;
			form['recipientemail'].value	= gcrec.recipientemail;

			form['sendername'].value		= gcrec.sendername;
			
			form['FirstName'].value		= gcrec.firstname;
			form['LastName'].value		= gcrec.lastname;
			form['Address'].value		= gcrec.address;
			form['Address2'].value		= gcrec.address2;
			form['City'].value			= gcrec.city;
			form['Zip'].value			= gcrec.zip;
			
			//form['msg1'].value			= gcrec.msg1;
			//form['msg2'].value			= gcrec.msg2;

			// set the state
			var stateindex;
			statelist					= form['State'].options;
			for (var i = statelist.length - 1; i>=0;  i--) {
				stateindex	= (statelist[i].value == gcrec.state) ? i : stateindex;
			}
			form['State'].selectedIndex	= stateindex;

			// set the country
			var countryindex;
			countrylist							= form['Country'].options;
			for (var i = countrylist.length - 1; i>=0;  i--) {
				countryindex	= (countrylist[i].value == gcrec.country) ? i : countryindex;
			}
			form['Country'].selectedIndex	= countryindex;

			// set the product code
			var pcodeindex;
			gclist							= form['add_PGC'].options;
			for (var i = gclist.length - 1; i>=0;  i--) {
				pcodeindex	= (gclist[i].value == gcrec.pcode) ? i : pcodeindex;
			}
			form['add_PGC'].selectedIndex	= pcodeindex;
			
			showElement('pgcui');
			scrollToElement('pgcui')
			
		} else {
			alert('unexpected error - unable to load the requested gift certificate');
		}
	} else {
		alert('unexpected error - unable to load the requested gift certificate');

	}
	
}
/* ------------------------------------------------------------------------------------------------------------------------------- */



/* -- Digital GCs ---------------------------------------------------------------------------------------------------------------- */
function orderGC() {
	// called by the add to cart button

	var form = document.gcform;

	if (validateSubmission('e')) {
		form.submit();
		//alert('gift certificate ordering is still under development');
		//return;
		
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------- */
function loadGCrec(gcobj) {
	
	// process AJAX response to request sent by loadGC()
	// loads the GC UI form with a specified GC
	// passed a JS object containing the GC data
	if (gcobj) {	

		var form = document.getElementById("gcform").elements;
		gcobj	= gcobj.replace(/\r/g,'\\r');
		//alert(gcobj);

		eval("var gcrec = ("+gcobj+")");
		if (gcrec) {
			//alert(gcrec.pcode);
			form['gcid'].value				= gcrec.gcid;
			form['recipientemail'].value	= gcrec.recipientemail;
			form['recipientname'].value		= gcrec.recipientname;
			form['sendername'].value		= gcrec.sendername;
			form['msg'].value				= gcrec.message;
			
			var pcodeindex;
			gclist							= form['add_EGC'].options;
			for (var i = gclist.length - 1; i>=0;  i--) {
				pcodeindex	= (gclist[i].value == gcrec.pcode) ? i : pcodeindex;
			}
			form['add_EGC'].selectedIndex	= pcodeindex;
			
			showElement('egcui');
			scrollToElement('egcui')
			
		} else {
			alert('unexpected error - unable to load the requested gift certificate');
		}
	} else {
		alert('unexpected error - unable to load the requested gift certificate');

	}
	
}
/* ------------------------------------------------------------------------------------------------------------------------------- */


function loadGC() {
	
	// called via body onload
	// load a GC if one is specified on the URL	
	// parse the gcid from the url if exists
	var egcid	= document.URL.match(/egc=(\d+)/i);		// digital
	var pgcid	= document.URL.match(/pgc=(\d+)/i);		// physical

	var loadnotice	= "fetching Gift Certificate, please wait...";

	if (egcid) {	//alert(egcid[1]);
		
		AjaxRequest.get(
			{	'url'		: 'http://www.dragondoor.com/cgi-bin/gc.pl',
				'parameters':{ 'egc' : egcid[1]},
				'onSuccess'	:function(req){ hideElement('debugout'); loadGCrec(req.responseText); },
				'onLoading'	:function(req){ writeToElement('debugout',loadnotice); }
			}
		);
	}

	if (pgcid) {
		
//			{	'url'		: 'http://www.dragondoor.com/cgi-bin/gc.pl',
//				'onSuccess'	:function(req){ hideElement('debugout'); loadGCrec(req.responseText); },

		AjaxRequest.get(
			{	'url'		: 'http://www.dragondoor.com/cgi-bin/gc.pl',
				'parameters':{ 'pgc' : pgcid[1]},
				'onSuccess'	:function(req){ loadPGCrec(req.responseText); },
				'onLoading'	:function(req){ writeToElement('debugout',loadnotice); }
			}
		);
	}

}
/* ------------------------------------------------------------------------------------------------------------------------------- */
function previewGC() {
	// used during dev - not included (yet?)
	if (validateSubmission()) {
		var loadnotice	= "generating preview, please wait...";
		AjaxRequest.submit(
			document.gcform,
			{	'url'		: 'http://www.dragondoor.com/cgi-bin/gc.pl',
				'parameters':{ 'previewgc' : '1'},
				'onSuccess'	:function(req){ hideElement('debugout'); writeToElement('GCpreview',req.responseText); },
				'onLoading'	:function(req){ writeToElement('debugout',loadnotice); }
			}
		);
	}
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function validateSubmission(targetform) {
	
	// called by orderGC() and orderPGC()
	
	if (targetform=='e') {
		// email GCs
		var egcform = document.gcform;
		if (!validEmail(egcform.recipientemail.value)) {
			alert('A valid recipient email address is required');
			return false;
		}
	
		if (egcform.msg.value.length>255) {
			alert('The size of your message exceeds the maximum number of permitted characters');
			return false;
		}
	}
	
	// printed GCs
	
	if (targetform=='p') {
		//alert('validating pgc');
		var pgcform = document.pgcform;
		var st		= pgcform.State.selectedIndex;
		var ct		= pgcform.Country.selectedIndex;
		var errormsg	= '';
		
		if (pgcform.FirstName.value.length<1) {		errormsg	+= 'Invalid First Name\n' }
		if (pgcform.LastName.value.length<1) {		errormsg	+= 'Invalid Last Name\n' }
		if (pgcform.City.value.length<2) {			errormsg	+= 'Invalid City\n' }
		if (pgcform.Address.value.length<2) {		errormsg	+= 'Invalid Street Address\n' }
		if (pgcform.State.selectedIndex == 0) {		errormsg	+= 'No State selected\n' }
		if (pgcform.Country.selectedIndex == 0) {	errormsg	+= 'No Country selected\n' }
		
		if ((st == 43 || st == 0) && (ct == 1 || ct == 2)) {	errormsg	+= 'Invalid State selected\n' }
		
		if (pgcform.recipientemail.value.length>1) {
			if (!validEmail(pgcform.recipientemail.value)) {
				errormsg	+= 'Invalid Email Address entered\n'
			}
		}
		
		if (errormsg) {
			alert(errormsg);
			return false;
		}
	}
	
	return true;
}
/* ------------------------------------------------------------------------------------------------------------------------------- */


/* ------------------------------------------------------------------------------------------------------------------------------- */
function setState() {
	// set the state fields to Other when a non North American country is selected
	var sc	= document.pgcform.Country.selectedIndex;
	var ss	= document.pgcform.State.selectedIndex;
	document.pgcform.State.selectedIndex	= (sc==1 || sc==2) ? ss : 43;
	//alert(sc);
	
/*	
	if ((form.Country.selectedIndex != 1) && (form.Country.selectedIndex != 2)) {
		form.State.selectedIndex = 0;
		form.State.selectedIndex = 43;
	}
*/
}
/* ------------------------------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------------------------------- */


