/* ------------------------------------------------------------------------------------------------------------------------------- */
//var domain		= 'http://www.dragondoor.com/';
var domain		= document.domain;
var url			= 'http://' +domain + '/cgi-bin/cart/instant_ordering.pl';
var ioproducts	= new Array;
var customercookie;
var test;
/* ------------------------------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------------------------------- */
var productcode;
/* ------------------------------------------------------------------------------------------------------------------------------- */
function initializeInstantOrdering() {
	if (setCustomerCookie){ setCustomerCookie()  }
	//if (setCustomerCookie){ }
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function setCustomerCookie() {
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function devcheck() {
	admin	= getCookieValue('validadmin');

	validation	= false;
	if (admin) {
		validation	= admin.match(/admin/i) ? true : false;
	}
	return validation;
}

var customercookie = 0;
/* ------------------------------------------------------------------------------------------------------------------------------- */
function loadInstantOrdering(trackid,kbid) {
	// use some AJAX to load the instant order button on other domains
	var msg  = '<b>loadInstantOrdering()</b> <br>';
	msg		+= 'Cust Cookie: '+customercookie+'<br><br>';

	//alert('loadInstantOrdering()');
	//writeToElement('debugmsg',msg,'a');

	if (ioproducts && !domain.match(/dragondoor\.com/ig)) {

		//setCustomerCookie();
		productstr	= ioproducts.toString();

		cc	= getCookieValue('customer');
		writeToElement('debugmsg','Cust Cookie: '+cc+'<br>','a');

		//alert('cc '+cc);

		//alert(productstr);
		//testing();
		//alert(test);

		// set up an AJAX transaction to get the ordering detail
		//form		= document.instantorderform;
		//formparms	=	formData2QueryString(form);
		//url			= 'http://www.dragondoor.com/cgi-bin/cart/instant_ordering.pl';
		//url			= 'http://www.enterthekettlebell.com/cgi-bin/cart/instant_ordering.pl';
		parmstr		 = '?requestaction=showbutton';
		parmstr		+= '&products='	+ productstr;
		parmstr		+= '&customer='	+ customercookie;

		if (trackid){	parmstr		+= '&trackid='	+ trackid; }
		if (kbid){		parmstr		+= '&kbid='		+ kbid; }

		target		 = 'loadInstantOrderingResult';
		targettype	 = 'script';

		//alert(parmstr);
		submitAjaxRequest(url,parmstr,target,targettype);
	}
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function loadInstantOrderingResult(result) {
	parseAjaxResponse(result);

	msg	= "<b>loadInstantOrderingResult()</b> \n";
	msg	+= "parsed " + AjaxResponse.length + " fields\n";

	for (var idx in AjaxResponse) {
		//msg	+=	AjaxResponse[idx].field + ' ' + AjaxResponse[idx].value + '\n';
		writeToElement(AjaxResponse[idx].field,AjaxResponse[idx].value,'');
		msg	+=	AjaxResponse[idx].field + '\n';
		//msg	+= "<hr> \n";
	}

	canode	= document.getElementById('customeraccount');

	if (canode) {
		if (canode.innerHTML.length>10) {
			writeToElement('myaccountlink','','');
		}
	}

	//alert(result);

	//result	= result.replace(/</g,"&lt;");
	//msg	+= "result \n" + result + " \n";

	//msg	= msg.replace(/\n/g,"<br>");
	//writeToElement('debugmsg',msg,'a');
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function loadInstantOrderDetail(result) {
	//alert(result);

	re	= /<product>(.+)<\/product><price>(.+)<\/price><shipping>(.+)<\/shipping><tax>(.*)<\/tax><ordertotal>(.+)<\/ordertotal><discount>(.+)<\/discount>/ig;
	var detailarray	= re.exec(result);

	if (!detailarray) { detailarray	= re.exec(result); }	// hack around - makes repeated calls work

	//alert(result);

	showElement('instantorder');
	showElement('instantedit');

	hideElement('iotax');
	hideElement('ioship');
	hideElement('iototal');
	hideElement('iodiscount');

	writeToElement('instantorderproduct',		detailarray[1]);
	showElement('ioprice');

	if (detailarray[2]!= '0.00') {
		writeToElement('instantorderprice',		'$'+detailarray[2]);
	}

	if (productcode.match(/^eb/i)) {
		hideElement('shippinglistrow');
	}

	if (detailarray[3]!= '0.00' && !productcode.match(/^eb/i)) {
		writeToElement('instantordershipping',	'$'+detailarray[3]);
		showElement('ioship');
	}

	if (detailarray[4]!= '0.00') {
		writeToElement('instantordertax',		'$'+detailarray[4]);
		showElement('iotax');
	}

	if (detailarray[5]!= '0.00') {
		writeToElement('instantordertotal',		'$'+detailarray[5]);
		showElement('iototal');
	}

	if (detailarray[6]!= '0.00') {
		writeToElement('instantorderdiscount',	'$'+detailarray[6]);
		showElement('iodiscount');
	}

}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function myAccount() {
	window.location	= 'https://www.dragondoor.com/cgi-bin/myaccount.pl'
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function writeToElement(id,data,mode) {
	var element	= document.getElementById(id);
	if (element) {
		if (mode) {
			if (mode.match(/a/i)) {	element.innerHTML	+= data;
			} else {				element.innerHTML	= data;	}
		} else {
			element.innerHTML	= data;
		}
	}
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function submitOrder() {
	// set up an AJAX transaction to get the ordering detail
	form		= document.instantorderform;
	//url			= 'http://www.dragondoor.com/cgi-bin/cart/instant_ordering.pl';
	formparms	=	formData2QueryString(form);
	parmstr		= '?requestaction=processorder';
	parmstr		+= '&pcode='+productcode;
	parmstr		+= '&'+formparms;

	//target		= 'orderResults';
	//targettype	= 'script';

	target		= 'debugmsg';
	targettype	= 'html';

	submitAjaxRequest(url,parmstr,target,targettype);
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function instantorder(pcode) {
	// check for expired credit card notices
	var expirednote	= document.getElementById('expirednote');
	if (expirednote) {
		alert(expirednote.innerHTML);
	} else {
		duplicateCheck(pcode);
	}
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function showExpiredMsg(expnotice) {
	// display expired credit card notices
	expnotice	= expnotice.replace(/<br>/g,"\n");
	expnotice	= 'Customer Account Expired Credit Card Notice: \n\n' + expnotice;
	alert(expnotice);
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function runtest() {
	var response	= "{'duplicatenote' : 'You ordered B33 on Sep 29, 2006 3:24PM ATL. See Order ID 98547 in your order history. Do you want to order B33? ', 'pcode' : 'B33'}";
	duplicateCheckResult(response);
}


/* ------------------------------------------------------------------------------------------------------------------------------- */
function duplicateCheck(pcode) {
	// check for potential duplicate order processing prior to submitting order requests
	var dupnote	= document.getElementById('duplicatenotice');
	if (dupnote) {
		// dupnote includes a duplicate notice when detected during button generation
		duplicateCheckResult(dupnote.innerHTML);
	} else {
		// we will check with an ajax call when no dupnote is found
		parmstr		= '?requestaction=duplicatecheck';
		parmstr		+= '&pcode='	+ pcode;
		if (customercookie) {parmstr		+= '&customer='+customercookie; }
		target		= 'duplicateCheckResult';
		targettype	= 'script';

		submitAjaxRequest(url,parmstr,target,targettype);

		msg	= 'checking for duplicate via ajax<br>';
		msg	+= parmstr;
		//writeToElement('debugmsg',msg,'');
	}
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function duplicateCheckResult(responsejson) {
	//writeToElement('debugmsg','duplicateCheckResult<br>','a');

	eval("var response = ("+responsejson+")");

	var duplicatenotice	= response['duplicatenote'];
	var pcode			= response['pcode'];

	if (duplicatenotice){
		duplicatenotice	= duplicatenotice.replace(/<br>/ig, '\n');
		if (confirm(duplicatenotice)) {
			//writeToElement('debugmsg','processing'+pcode,'');
			processinstantorder(pcode);
		} else {
			//writeToElement('debugmsg','cancelled','');
		}
	} else {
		//writeToElement('debugmsg','processing'+pcode,'');
		processinstantorder(pcode);
	}
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function processinstantorder(pcode) {
	productcode	= pcode;
	// set up an AJAX transaction to get the ordering detail
	form		= document.instantorderform;
	//url			= 'http://www.dragondoor.com/cgi-bin/cart/instant_ordering.pl';
	formparms	=	formData2QueryString(form);

	var msg  = '<b>instantorder()</b> <br>';
	msg		+= 'pcode: '+pcode+'<br><br>';
	msg		+= 'customer: '+customercookie+'<br><br>';

	/*
	// get the order detail prior to submitting the order
	parmstr		= '?requestaction=orderdetail';
	parmstr		+= '&pcode='+pcode;
	parmstr		+= '&'+formparms;
	target		= 'loadInstantOrderDetail';
	targettype	= 'script';
	*/

	// submit an order for the selected product
	parmstr		= '?requestaction=processorder';
	parmstr		+= '&pcode='+productcode;
	if (customercookie) {parmstr		+= '&customer='+customercookie; }
	parmstr		+= '&'+formparms;
	target		= 'orderResults';
	targettype	= 'script';

	if (isDev()) {
		msg		+= '<br>'+url+parmstr;
		writeToElement('debugmsg',msg,'a');
	}


	AjaxResponse.length	= 0;
	submitAjaxRequest(url,parmstr,target,targettype);
	//writeToElement('debugmsg',msg,'');
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
function orderResults(result) {

	parseAjaxResponse(result);

	var msg  = '<b>orderResults()</b> <br>';
	msg	= "parsed " + AjaxResponse.length + " fields\n";
	//msg	+= "result \n" + result + " \n";

	for (var idx in AjaxResponse) {
		field	= AjaxResponse[idx].field;
		value	= AjaxResponse[idx].value;
		if (field=='orderid') { orderid	= value }

		// debugging
		//msg	+= field + '\n';

		if (field=='debugmsg') { writeToElement(field,value,'a') }
		//if (field=='debugmsg') { msg	+= value + '\n'; }
		//if (field=='orderid') { msg	+= '<br><br>orderid	= ' + value; writeToElement('debugmsg',msg,'a'); }

	}

	var error	= orderid ? false : true;

	msg	+= 'orderid	= ' + orderid + '<br>';
	//alert(msg);

	if (orderid) {
		if (orderid.match(/\d+/)) {
			getreceipturl	 = 'http://www.dragondoor.com/cgi-bin/cart/instant_ordering.pl';
			getreceipturl	+= "?requestaction=displayreceipt&orderid="+orderid;
			window.location	= getreceipturl;
			//alert(getreceipturl);


			msg	+= 'getreceipturl ' + getreceipturl + '<br>';
			//writeToElement('orderresults',getreceipturl,'a');
			//showElement('orderresults');
			error	= false;
		}
	}


	if (error) {
		errormsg	= "<b>We were unable to process this order submission</b>";
		writeToElement('orderresults',errormsg,'a');
		showElement('orderresults');
	}

	//writeToElement('debugmsg',msg,'a');
}


/* ------------------------------------------------------------------------------------------------------------------------------- */
function instantorderAddressChange(list) {
	var pcode		= productcode;
	var addresstype	= list.name;

	var msg = 'Address Change ' + addresstype;
	msg += '\nOrdering ' + pcode;
	//alert(msg);

	//orderingproductcell				= document.getElementById('instantorderproduct');
	//orderingproductcell.innerHTML	= pcode;
	//showElement('instantorder');
	//showElement('instantedit');

	// set up an AJAX transaction to get the ordering detail
	form		= document.instantorderform;
	//url			= 'http://www.dragondoor.com/cgi-bin/cart/instant_ordering.pl';
	formparms	=	formData2QueryString(form);
	parmstr		= '?requestaction=orderdetail';
	parmstr		+= '&pcode='+pcode;
	parmstr		+= '&'+formparms;
	//parmstr		+= '&addresschange='+addresstype;
	target		= 'loadInstantOrderDetail';
	targettype	= 'script';

	//alert(parmstr);
	submitAjaxRequest(url,parmstr,target,targettype);
}

/* ------------------------------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------------------------------- */
function orderEbook(pcode,upsell) {
	var width		= 750;
	var height		= 550;
	var myBars		= 'directories=no,location=no,menubar=no,status=yes,titlebar=yes,toolbar=no';
	var myOptions	='scrollbars=yes,width='+width+',height='+height+',resizable=yes';
	var myFeatures	= myBars + ',' + myOptions;
	
	var url			= 'http://www.dragondoor.com/digital_order_popup.html?pcode='+pcode
	
	if (upsell) {
		url			+= '&us=1';
	}
	
	var refWindow	= window.open(url, 'ref_Win', myFeatures);

	//acceptlink		= refWindow.document;


	//setOrderLink(refWindow,url);
}

var elapsed	= 0;
function setOrderLink(refWindow,url) {

	//acceptlink		= refWindow.document.getElementById('accept');
	acceptlink		= refWindow;

	maxsecs	= 5;

	if (acceptlink) {
		if (acceptlink.location.length>10) {
			elapsed	= maxsecs;
		}
	}

	//if (maxsecs>elapsed && !acceptlink.location.match(/blank/i)) {
	if (maxsecs>elapsed) {
		timerID = setTimeout('setOrderLink(\''+url+'\')',1000);
		elapsed++;
	} else {
		//acceptlink		= document.getElementById('accept');
		//acceptlink.href	= url;
		//alert(acceptlink);
		if (acceptlink) {
			alert(acceptlink.location+' '+elapsed);
		} else {
			alert('ok');
		}


	}

}

/* ------------------------------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------------------------------------------------------------- */
