var quickCheckout = false;
function toggleDivBox(szDivID, iState) 
{
   if (document.getElementById(szDivID)!= null)
   {	obj = document.getElementById(szDivID);
		if (iState){
		   obj.style.display = 'block';
		   // set initial shipping val if displaying order div
			if (szDivID == "order") {
				setInitialShippingvalue();
			}		   
		}
		else{
			obj.style.display = 'none';
		}
   }
} 
function divDisplaying(szDivID) 
{
   if (document.getElementById(szDivID)!= null)
   {	obj = document.getElementById(szDivID);
		if (obj.style.display == 'block'){
			return true;
		}
		else{
			return false;
		}
   }
}

function showPaymentInformation(){
	objPayment = document.getElementById('payment');
	var w = 300;
	var h = 300;
	var winl = cursorPageX-w-w;//-w;
	var wint = cursorPageY-h-150;//-h;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	objPayment.style.left = winl+'px';
	objPayment.style.top  = wint+'px';
	addLayerDrag(objPayment);
	toggleDivBox('payment',1);
}
function showSecurityCodeBlock(status){
	//objPayment = document.getElementById('payment');
	objSecurityCode = document.getElementById('securityCode');
	//addLayer(objSecurityCode,objPayment)
	var w = 300;
	var h = 300;
	var winl = cursorPageX-w;//-w;
	var wint = cursorPageY-h;//-h;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	objSecurityCode.style.left = winl+'px';
	objSecurityCode.style.top  = wint+'px';
	toggleDivBox('securityCode',status);
	setLayerOnTop(objSecurityCode)
}


function getState(){
	DWREngine._execute(cfscriptLocation, null, 'getState', processState);
}
function getCountry(){
	DWREngine._execute(cfscriptLocation, null, 'getCountry', processCountry);
}

function processCountry(results){
	//alert(results.length);
	DWRUtil.removeAllOptions("billCountry");
	DWRUtil.removeAllOptions("shipCountry");
	DWRUtil.addOptions("billCountry", results, "KEY", "VALUE");
	//DWRUtil.addOptions("shipCountry", results, "KEY", "VALUE");
	DWRUtil.setValue("billCountry",217);

	eShipCountry = document.getElementById("shipCountry")
	newoption = new Option('United States of America', '217', false, false);
	eShipCountry.options[0] = newoption;

	DWRUtil.setValue("shipCountry",217);
}
function processState(results){
	DWRUtil.removeAllOptions("billState");
	DWRUtil.removeAllOptions("shipState");
	DWRUtil.addOptions("billState", results, "KEY", "VALUE");
	DWRUtil.addOptions("shipState", results, "KEY", "VALUE");
}
function checkBillCountry(selectedCountryID){
	if (selectedCountryID == 'undefined')
		countryID = selectedCountryID;
	else
		countryID = DWRUtil.getValue("billCountry");
	
	
	if (countryID != '217'){
		DWRUtil.setValue("billStateDispText",'State/Province');
		//document.getElementById("billStateDispText").innerHTML = 'State/Province';
		toggleDivBox('billStateSelectBox',0);
		toggleDivBox('billStateTextBox',1);
	}
	else{
		DWRUtil.setValue("billStateDispText",'State *');
		//document.getElementById("billStateDispText").innerHTML = 'State *';
		toggleDivBox('billStateSelectBox',1);
		toggleDivBox('billStateTextBox',0);
	}	
	updateShipAddress();
}
function checkShipCountry(selectedCountryID){
	if (selectedCountryID == 'undefined')
		countryID = selectedCountryID;
	else
		countryID = DWRUtil.getValue("shipCountry");
	if (countryID != '217'){
		DWRUtil.setValue("shipStateDispText",'State/Province');
		//document.getElementById("shipStateDispText").innerHTML = 'State/Province';
		toggleDivBox('shipStateSelectBox',0);
		toggleDivBox('shipStateTextBox',1);
	}
	else{
		DWRUtil.setValue("shipStateDispText",'State *');
		//document.getElementById("shipStateDispText").innerHTML = 'State *';
		toggleDivBox('shipStateSelectBox',1);
		toggleDivBox('shipStateTextBox',0);
	}
}

function checkoutLogin(){
	//alert("checkoutLogin");
	var username = DWRUtil.getValue('loginemail');
	var password = DWRUtil.getValue('loginpassword');
	document.getElementById("userErrorMsg").innerHTML = '';
	//var password = '32031354AD580F902F5CC2BEE42A1815';
	if (username.length > 0 && password.length > 0) {
		resetForm();			
		DWREngine._execute(cfscriptLocation, null, 'existsUserCheckout',sessID,username,password, processResult);
	} else {
		alert('Enter username and password');
	}	
}

function myAccountLogin(){
	var username = DWRUtil.getValue('loginemail');
	var password = DWRUtil.getValue('loginpassword');
	if (username.length > 0 && password.length > 0) {
		DWREngine._execute(cfscriptLocation, null, 'existsUserCheckout',sessID,username,password, processResult);
		//document.forms['LoginForm'].submit();
	} else {
		alert('Enter username and password');
	}	
}

function checkUserName(){
	var username = DWRUtil.getValue('username');
	
	var newUserErrorMsg = document.getElementById("newUserErrorMsg");

	if (newUserErrorMsg !=null)
		newUserErrorMsg.innerHTML = '';

	if (username.length > 0) {
		DWREngine._execute(cfscriptLocation, null, 'newUserCheckout',sessID,username, processUsernameCheckResult);
	} else {
		alert('Enter the username to be created');
	}
}

function processUsernameCheckResult(results){
	//alert('processUsernameCheckResult');
	if (DWRUtil._isArray(results)){
		//alert(results.length);
		if (results.length > 0){
			//location.href='index.cfm?pk=checkout&ac=ship';
			updateBillShipAddress();
		}
		else {
			document.getElementById("newUserErrorMsg").innerHTML = '<b>Username alreay exists</b>';
			alert('Username alreay exists');
		}
	}
	else {
		document.getElementById("userErrorMsg").innerHTML = '<b>Invalid username and password</b>';
		alert('Invalid username and password');
	}
}


function newUser(){
	//var username = DWRUtil.getValue('newemail');
	//alert("entered New User");
	resetForm();
	//alert("after ResetForm");	
	//getState();
	//alert("after Getstate");	
	//getCountry();
	//alert("after Get Country");	

	toggleDivBox('newUserSubmit',1);
	toggleDivBox('existingUserSubmit',0);
	toggleDivBox('login',0);
	toggleDivBox('address',1);
	//document.getElementById("newUserErrorMsg").innerHTML = '';
	//if (username.length > 0) {
		//DWREngine._execute(cfscriptLocation, null, 'newUserCheckout',sessID,username, processResult);
	//} else {
		//alert('Enter the username to be created');
	//}
}

function processResult(results){

	if (DWRUtil._isArray(results)){
		if (results.length > 0){
			action = DWRUtil.getValue("action");
			if (action =='login'){
				//alert('redirect');
				document.forms['LoginForm'].submit();
			}
			else {
				location.href='index.cfm?pk=checkout&ac=ship';
				//getCountry();
				//getState();
				//toggleDivBox('newUserSubmit',0);
				//toggleDivBox('existingUserSubmit',1);
				//getBillShipAddrDisplay();
				//populateBillAddr(results);
				//toggleDivBox('login',0);
				//toggleDivBox('address',1);
			}
		}
		//else
			//document.getElementById("newUserErrorMsg").innerHTML = '<b>Username alreay exists</b>';
			//alert('Username alreay exists');
	}
	else {
		document.getElementById("userErrorMsg").innerHTML = '<b>Invalid username and password</b>';
		alert('Invalid username and password');
	}
}

function resetForm(){
	DWRUtil.setValue("homePhone",'');
	DWRUtil.setValue("workPhone",'');
	DWRUtil.setValue("email",'');
	DWRUtil.setValue("username",'');
	DWRUtil.setValue("password1",'');
	DWRUtil.setValue("password2",'');
	DWRUtil.setValue("billCompanyName",'');
	DWRUtil.setValue("billFirstName",'');
	DWRUtil.setValue("billInit",'');
	DWRUtil.setValue("billlastName",'');
	DWRUtil.setValue("billAddress1",'');
	DWRUtil.setValue("billAddress2",'');
	DWRUtil.setValue("billCity",'');
	if (!DWRUtil.setValue("billState",''))
		DWRUtil.setValue("billState",1);
		
	DWRUtil.setValue("billStateText",'');
	DWRUtil.setValue("billCountry",217);
	DWRUtil.setValue("billZip",'');	
	DWRUtil.setValue("shipPhone",'');
	DWRUtil.setValue("shipCompanyName",'');
	DWRUtil.setValue("shipFirstName",'');
	DWRUtil.setValue("shipInit",'');
	DWRUtil.setValue("shipLastName",'');
	DWRUtil.setValue("shipAddress1",'');
	DWRUtil.setValue("shipAddress2",'');
	DWRUtil.setValue("shipCity",'');
	if (!DWRUtil.setValue("shipState",''))
		DWRUtil.setValue("shipState",1);

	DWRUtil.setValue("shipStateText",'');
	DWRUtil.setValue("shipCountry",217);
	DWRUtil.setValue("shipZip",'');

}
function populateBillAddr(results){
	//var returnValue = DWRUtil.toDescriptiveString(results[0],1)
	var _billStateID = 0;
	var _billCountryID = 0;
	for (i = 0; i < results.length; i++){
		if (typeof results[i] == "object"){
			var userID = 0;
			var companyname;
			var firstname;
			var lastname;
			var initial;
			var email;
			var homephone;
			var workphone;
			var fax;
			var address1;
			var address2;
			var city;
			var state_id;
			var province_name;
			var country_id;
			var zipcode;
			var addressType;
			var stateName;
			var countryName;
			var useBill;
			var shipTotal;
			var taxTotal;
			var grandTotal;
			var PreferredGuestNumber = '';
			var giftMessage = '';
			
			var addressType = results[i]['ADDRESSTYPE'];			
			var useBill = results[i]['USE_BILL'];
			
			for (var prop in results[i]){
				switch (prop){
				case 'USERID': 
					userID = results[i][prop];
					break;
				case 'COMPANYNAME': 
					companyname = results[i][prop];
					break;
				case 'FIRSTNAME': 
					firstname = results[i][prop];
					break;
				case 'LASTNAME':
					lastname = results[i][prop];
					break;
				case 'INITIAL':
					initial = results[i][prop];
					break;
				case 'EMAIL':
					email = results[i][prop];
					break;			
				case 'HOMEPHONE':
					homephone = results[i][prop];
					break
				case 'WORKPHONE':
					workphone = results[i][prop];
					break			
				case 'ADDRESS1':
					address1 = results[i][prop];
					break
				case 'ADDRESS2':
					address2 = results[i][prop];
					break
				case 'CITY':
					city = results[i][prop]; 
					break
				case 'STATE_ID':
					state_id = results[i][prop];
					if (addressType == 1) {
						//alert(state_id);
						_billStateID = state_id;
						DWRUtil.setValue("billState",state_id);
					}
					else{
						DWRUtil.setValue("billState",state_id);
					}
					break
				case 'STATENAME':
					stateName = results[i][prop]; 
					break
				case 'COUNTRY_ID':
					country_id = results[i][prop];
					if (addressType == 1) {
						_billCountryID = country_id;
						if (country_id != 217)
							DWRUtil.setValue("dispBillState",province_name);
						else
							DWRUtil.setValue("dispBillState",stateName);
					}
					break
				case 'COUNTRYNAME':
					countryName = results[i][prop]; 
					break
				case 'PROVINCE_NAME':
					province_name = results[i][prop];
					break
				case 'ZIPCODE':
					zipcode = results[i][prop]
					break
				case 'ADDRESSTYPE':
					addressType = results[i][prop];
					break
				case 'USE_BILL':
					useBill = results[i][prop];
				case 'SHIPTOTAL':
					shipTotal = results[i][prop];				
				case 'TAXTOTAL':
					taxTotal = results[i][prop];	
				case 'GRANDTOTAL':
					grandTotal = results[i][prop];	
				case 'PREFERREDGUESTNUMBER':
					PreferredGuestNumber = results[i][prop];
				case 'SC_GIFT_MESSAGE':
					giftMessage = results[i][prop];
			}

			if (addressType == 1){
				//alert(shipTotal);
				//DWRUtil.setValue("shipTotalDiv",shipTotal);
				//DWRUtil.setValue("taxTotalDiv",taxTotal);
				//DWRUtil.setValue("grandTotalDiv",grandTotal);
				
				DWRUtil.setValue("billCompanyName",companyname);
				DWRUtil.setValue("dispBillCompanyName",companyname);
				DWRUtil.setValue("billFirstName",firstname);
				DWRUtil.setValue("billlastName",lastname); 
				DWRUtil.setValue("billInit",initial); 
				DWRUtil.setValue("dispBillName",firstname+' '+initial+' '+lastname);
								
				DWRUtil.setValue("email",email); 
				DWRUtil.setValue("dispEmail",email);

				DWRUtil.setValue("homePhone",homephone);
				DWRUtil.setValue("dispHomePhone",homephone);
				 
				DWRUtil.setValue("workPhone",workphone); 
				DWRUtil.setValue("dispWorkPhone",homephone);

				DWRUtil.setValue("billAddress1",address1); 
				DWRUtil.setValue("dispBillAddress1",address1); 
				DWRUtil.setValue("billAddress2",address2); 
				DWRUtil.setValue("dispBillAddress2",address2); 				
				DWRUtil.setValue("billCity",city); 
				DWRUtil.setValue("dispBillCity",city);
				DWRUtil.setValue("dispBillCity",city);
				DWRUtil.setValue("billState",state_id);

				//_billStateID = 0;
				//_billCountryID = 0; 
				if (country_id != 217)
					DWRUtil.setValue("dispBillState",province_name);
				else
					DWRUtil.setValue("dispBillState",stateName);
				
				DWRUtil.setValue("billCountry",country_id); 
				DWRUtil.setValue("dispBillCountry",countryName); 

				DWRUtil.setValue("billStateText",province_name);
				DWRUtil.setValue("billZip",zipcode);
				DWRUtil.setValue("dispBillZip",zipcode); 

				
				if (prop == 'PREFERREDGUESTNUMBER'){
					//alert(PreferredGuestNumber);
					DWRUtil.setValue("dispPreferredGuestNumber",PreferredGuestNumber);
					DWRUtil.setValue("PreferredGuestNumber",PreferredGuestNumber);
				}
				
				if (prop == 'SC_GIFT_MESSAGE') {
					//alert(giftMessage)
					DWRUtil.setValue("dspGiftMessage",giftMessage);				
				}
				
				if (useBill == 1){
					//DWRUtil.setValue("usebill",true);
					DWRUtil.setValue("dispShipCompanyName",companyname);				
					DWRUtil.setValue("dispShipName",firstname+' '+initial+' '+lastname);
					DWRUtil.setValue("dispShipPhone",homephone);
					DWRUtil.setValue("dispShipAddress1",address1); 
					DWRUtil.setValue("dispShipAddress2",address2); 
					DWRUtil.setValue("dispShipCity",city); 
					DWRUtil.setValue("dispShipState",DWRUtil.getValue("dispBillState"));
					DWRUtil.setValue("dispShipCountry",countryName); 
					DWRUtil.setValue("dispShipZip",zipcode); 
				}
				else
					DWRUtil.setValue("usebill",false);
				//alert(IsDisplaying('login'));
				//toggleDivBox('login',0);
				//toggleDivBox('address',1);
			}
			else if (addressType==0){
				DWRUtil.setValue("shipCompanyName",companyname);
				DWRUtil.setValue("dispShipCompanyName",companyname);
								
				DWRUtil.setValue("shipFirstName",firstname);
				DWRUtil.setValue("shipLastName",lastname); 
				DWRUtil.setValue("shipInit",initial); 
				DWRUtil.setValue("dispShipName",firstname+' '+initial+' '+lastname);
								
				DWRUtil.setValue("shipPhone",homephone); 
				DWRUtil.setValue("dispShipPhone",homephone);
								
				DWRUtil.setValue("shipAddress1",address1); 
				DWRUtil.setValue("dispShipAddress1",address1); 
								
				DWRUtil.setValue("shipAddress2",address2); 
				DWRUtil.setValue("dispShipAddress2",address2); 
				
				DWRUtil.setValue("shipCity",city); 
				DWRUtil.setValue("dispShipCity",city); 
				
				DWRUtil.setValue("shipState",state_id); 
				if (country_id != 217)
					DWRUtil.setValue("dispShipState",province_name);
				else
					DWRUtil.setValue("dispShipState",stateName);
				
				DWRUtil.setValue("shipCountry",country_id); 
				DWRUtil.setValue("dispShipCountry",countryName); 
				
				DWRUtil.setValue("shipStateText",province_name);
				DWRUtil.setValue("shipZip",zipcode);
				DWRUtil.setValue("dispShipZip",zipcode); 				
			}
			}
		}
	}
	if (results.length ==0)
		toggleDivBox('login',1);
	else if (!divDisplaying('order')){
		if (userID > 0){
			deleteRow('usernameRow');
			deleteRow('username1Row');
			deleteRow('password1Row');
			deleteRow('password2Row');
			DWRUtil.setValue('requiredFields2','')
			DWRUtil.setValue('requiredFieldMsgs2','')
		}
		toggleDivBox('login',0);toggleDivBox('address',1);
	}
	
	/*var date = new Date();
	var curDate = null;
	var millis = 2000;
	do { curDate = new Date(); }
	while(curDate-date < millis);*/
	
	//DWRUtil.setValue("billState",_billStateID);
	//DWRUtil.setValue("billCountry",_billCountryID); 
	
	//alert(_billCountryID);
	updateShipAddress();
	//checkShipCountry();
	//checkBillCountry(_billCountryID);
	setTimeout('checkBillCountry()',500);
	setTimeout('checkShipCountry()',500);
	
	DWRUtil.toggleDivBox('ajaxStatusBar',0);
}

function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}


function deleteRow(rowID){
	var tableRow = document.getElementById(rowID); 
	if (tableRow!=null)
		tableRow.parentNode.deleteRow(tableRow.sectionRowIndex);
}

function updateShipAddress(){
	var usebill = DWRUtil.getValue("usebill");
	if (usebill == 1){
		DWRUtil.setValue("shipCompanyName", DWRUtil.getValue("billCompanyName")); 
		DWRUtil.setValue("shipFirstName", DWRUtil.getValue("billFirstName")); 
		DWRUtil.setValue("shipInit", DWRUtil.getValue("billInit")); 
		DWRUtil.setValue("shipLastName", DWRUtil.getValue("billlastName")); 
		DWRUtil.setValue("shipPhone", DWRUtil.getValue("homePhone")); 
		DWRUtil.setValue("shipAddress1", DWRUtil.getValue("billAddress1")); 
		DWRUtil.setValue("shipAddress2", DWRUtil.getValue("billAddress2")); 
		DWRUtil.setValue("shipCity", DWRUtil.getValue("billCity")); 
		DWRUtil.setValue("shipState", DWRUtil.getValue("billState")); 
		DWRUtil.setValue("shipStateText", DWRUtil.getValue("billStateText")); 
		DWRUtil.setValue("shipZip", DWRUtil.getValue("billZip")); 
		DWRUtil.setValue("shipCountry", DWRUtil.getValue("billCountry"));
		document.getElementById('shipCountry').onchange();
	}

}
var displayOrderDetail = false;
var getBillShipAddrDisplayRequestCount = 0;
function updateBillShipAddress(){
	var email = DWRUtil.getValue("email");
	var companyName = DWRUtil.getValue("billCompanyName"); 
	var firstname = DWRUtil.getValue("billFirstName");
	var initial	=DWRUtil.getValue("billInit");
	var lastname = DWRUtil.getValue("billlastName"); 
	var username = DWRUtil.getValue("username"); 
	var password = DWRUtil.getValue("password1"); 
	var homephone = DWRUtil.getValue("homePhone"); 
	var workphone = DWRUtil.getValue("workPhone"); 					 
	var address1 = DWRUtil.getValue("billAddress1"); 
	var address2 = DWRUtil.getValue("billAddress2"); 
	var city = DWRUtil.getValue("billCity"); 
	var state_id = DWRUtil.getValue("billState"); 
	var country_id = DWRUtil.getValue("billCountry"); 
	var province_name =	DWRUtil.getValue("billStateText");
	var zipcode = DWRUtil.getValue("billZip");
	var PreferredGuestNumber = DWRUtil.getValue("PreferredGuestNumber");
	var promoCode = DWRUtil.getValue("promoCode");	
	var giftMessage1 = DWRUtil.getValue("giftMessage1");
	var giftMessage2 = DWRUtil.getValue("giftMessage2");
	var usebill = DWRUtil.getValue("usebill");
	var usebillAsShip = 0;
	var giftMessage = '';
	
	//alert(promoCode);
	
	if (usebill)
		usebillAsShip = 1;
	//alert(PreferredGuestNumber);
	
	if (giftMessage1.length > 0) 
		giftMessage = giftMessage1;
	else if (giftMessage2.length > 0) 
		giftMessage = giftMessage2;
		
	//create regex pattern		
	var myRegExp = new RegExp("[^a-zA-Z0-9\s.,]","g");
	//replacing all characters in gift message that match Regex
	giftMessage = giftMessage.replace(myRegExp,' ');		

	
	DWREngine._execute(cfscriptLocation, null, 'updateShopCartAddress',sessID,1, email, companyName, firstname, initial, lastname, username, password, homephone, workphone, address1, address2, city, state_id, usebillAsShip, province_name, country_id, zipcode, PreferredGuestNumber, promoCode, giftMessage, processAddressResult);
	getBillShipAddrDisplayRequestCount = 1;	
	if (usebillAsShip==0){
		var email = '';
		var companyName = DWRUtil.getValue("shipCompanyName");
		var firstname = DWRUtil.getValue("shipFirstName");
		var initial	=DWRUtil.getValue("shipInit");
		var lastname = DWRUtil.getValue("shipLastName");
		var homephone = DWRUtil.getValue("shipPhone");
		var workphone = '';
		var address1 = DWRUtil.getValue("shipAddress1"); 
		var address2 = DWRUtil.getValue("shipAddress2");
		var city = DWRUtil.getValue("shipCity");
		var state_id = DWRUtil.getValue("shipState");
		var country_id = DWRUtil.getValue("shipCountry");
		var province_name =	DWRUtil.getValue("shipStateText");
		var zipcode = DWRUtil.getValue("shipZip");
		var giftMessage1 = DWRUtil.getValue("giftMessage1");
		var giftMessage2 = DWRUtil.getValue("giftMessage2");		
		var usebill = 0;
		var giftMessage = '';
		
		if (giftMessage1.length > 0) 
			giftMessage = giftMessage1;
		else if (giftMessage2.length > 0) 
			giftMessage = giftMessage2;

		//create regex pattern		
		var myRegExp = new RegExp("[^a-zA-Z0-9\s.,]","g");
		//replacing all characters in gift message that match Regex
		giftMessage = giftMessage.replace(myRegExp,' ');			

		DWREngine._execute(cfscriptLocation, null, 'updateShopCartAddress',sessID,0, email, companyName, firstname, initial, lastname, '', password, homephone, workphone, address1, address2, city, state_id, 0, province_name, country_id, zipcode, '', '', giftMessage, processAddressResult2);
		getBillShipAddrDisplayRequestCount = 2;	
	}
	displayOrderDetail = true;
	//setTimeout("processCheckoutStep2()", 1000); 
}
function getBillShipAddrDisplay(){
	//DWRUtil.toggleDivBox('ajaxStatusBar',1);
	DWREngine._execute(cfscriptLocation, null, 'getBillShipAddress',sessID, processAddressResults);
	//alert("CALLING getOrderTotal");
	getOrderTotal();
}
function processAddressResult2(results){
	getBillShipAddrDisplay();
}

function processAddressResult(results){
	var x = 1;
	if (getBillShipAddrDisplayRequestCount = 1)
		getBillShipAddrDisplay();
}

function processAddressResults(results){
	populateBillAddr(results);
	//var homephoneValue = DWRUtil.getValue("homePhone");
	//alert(homephoneValue);
	if (displayOrderDetail){
		toggleDivBox('order',1);
		processCheckoutStep2();
		//setTimeout("processCheckoutStep2()", 1000); 
	}
}
function processCheckoutStep2(){
	//toggleDivBox('address',0);
	//toggleDivBox('order',1);
	//displayOrderDetail = false;
	//alert('Update Address');
	location.href='index.cfm?pk=checkout&ac=ship';
}

function getOrderTotal(){
	DWREngine._execute(cfscriptLocation, null, 'getCartTotal',sessID, processOrderTotalResults);
}

function processOrderTotalResults(results){
	//results.SHIPTOTAL;
	//results.TAXTOTAL;
	//alert(results.GRANDTOTAL);
	DWRUtil.setValue("shipTotalDiv",results.SHIPTOTAL);
	DWRUtil.setValue("taxTotalDiv",results.TAXTOTAL);
	DWRUtil.setValue("grandTotalDiv",results.GRANDTOTAL);
}


function completeOrder(){
	//alert("About to Complete Order");
	var email = DWRUtil.getValue("email");
	var ccNumber = DWRUtil.getValue("ccNum"); 
	var ccType = DWRUtil.getValue("ccType");
	var ccExp	=DWRUtil.getValue("ccExp");
	var csvCode	=DWRUtil.getValue("csvCode");
	var ccFirstName	=DWRUtil.getValue("ccFirstName");
	var ccMI = DWRUtil.getValue("ccMI");
	var ccLastName	=DWRUtil.getValue("ccLastName");
	var discountAmt	=DWRUtil.getValue("discountAmt");
	//alert(discountAmt);
	document.getElementById("QStatus").innerHTML = '<table class="listing" cellpadding="0" cellspacing="0"><tr><td><img src="/images/loading.gif" /></td><td valign="middle" class="td1">Processing  your request</td></tr></table>';
		btnCompleteOrder.disabled = true;
	DWREngine._execute(cfscriptLocation, null, 'completeOrder',sessID,ccNumber, ccType, ccExp, csvCode, ccFirstName, ccMI, ccLastName, discountAmt, processOrderResult);

}


function adminCompleteOrder(){
	var email = DWRUtil.getValue("email");
	var ccNumber = DWRUtil.getValue("ccNum"); 
	var ccType = DWRUtil.getValue("ccType");
	var ccExp	=DWRUtil.getValue("ccExp");
	var csvCode	=DWRUtil.getValue("csvCode");
	var ccFirstName	=DWRUtil.getValue("ccFirstName");
	var ccMI =DWRUtil.getValue("ccMI");
	var ccLastName	=DWRUtil.getValue("ccLastName");
	document.getElementById("QStatus").innerHTML = '<table class="listing" cellpadding="0" cellspacing="0"><tr><td><img src="/images/loading.gif" /></td><td valign="middle" class="td1">Processing  your request</td></tr></table>';
	btnCompleteOrder.disabled = true;
	btnCompleteOrder.style.display='none';
	DWREngine._execute(cfscriptLocation, null, 'adminCompleteOrder',sessID,ccNumber, ccType, ccExp, csvCode, ccFirstName, ccMI, ccLastName, processOrderResult);

}




function checkOrderStatus(queID){
	//alert(queID);
	DWREngine._execute(cfscriptLocation, null, 'checkOrderQueStatus',queID,sessID, processOrderResult);
}
function removeFromOrderQue(queID){
	DWREngine._execute(cfscriptLocation, null, 'removeFromOrderQue',queID,sessID, processOrderResult);
}
function processOrderResult(results){
	//alert(results.STATUS)
	if (results.STATUS > 0 && results.STATUS ==4){
		document.getElementById("QStatus").innerHTML = 'Unable to process your Order';
		btnCompleteOrder.disabled = false;
		btnCompleteOrder.style.display='block';
	}
	else if (results.STATUS > 0 && results.STATUS ==5){
		document.getElementById("QStatus").innerHTML = '<img src="/images/check.gif" />';
		btnCompleteOrder.disabled = false;
		btnCompleteOrder.style.display='block';
		//alert(results.MESSAGE);
		alert('Unable to process your card. \nPlease check your card number, expiration date and security code');
	}
	else if (results.STATUS > 0 && results.STATUS !=3) {
		document.getElementById("QStatus").innerHTML = '<table class="listing" cellpadding="0" cellspacing="0"><tr><td><img src="/images/loading.gif" /></td><td valign="middle" class="td1">Processing  your request</td></tr></table>';;
		checkOrderStatus(results.QUEID);
	}
	else if (results.STATUS > 0 && results.STATUS == 3){
		document.getElementById("QStatus").innerHTML = 'Sucessfully Completed';
		 top.location.href='/index.cfm?pk=checkout&ac=thanks&oid='+results.ORDERID;
	}
	else {
		document.getElementById("QStatus").innerHTML = 'Unable to process your Order';
		btnCompleteOrder.disabled = false;
		btnCompleteOrder.style.display='block';
	}
}

function updateShippingType(shipTypeID){
	//alert(shipTypeID);
	DWREngine._execute(cfscriptLocation, null, 'updateShippingType',sessID,shipTypeID,processShipTypeResult);
}
function processShipTypeResult(results){
	//alert(results);
	getBillShipAddrDisplay();
}


/*
function adminCheckUserName(){
	alert("adminCheckuserName");
	var existingUserFlag = DWRUtil.getValue("usingExistingCustomerFlag");
	alert("Existing User = " + existingUserFlag);
	var username = DWRUtil.getValue('username');

	
	if (existingUserFlag == "Yes") {
		alert("processing existing user");
		var username = DWRUtil.getValue('username');
		var password = DWRUtil.getValue('password1');
		
		DWREngine._execute(cfscriptLocation, null, 'existsUserCheckout',sessID,username,password, processResult);
	} else {	
		if (username.length > 0) {
			DWREngine._execute(cfscriptLocation, null, 'newUserCheckout',sessID,username, processUsernameCheckResult);
		} else {
			alert('Enter the username to be created');
		}
	}	
}

function adminProcessUsernameCheckResult(results){
	if (DWRUtil._isArray(results)){
		//alert(results.length);
		if (results.length > 0){
			updateBillShipAddress();
		}
		else {
			document.getElementById("newUserErrorMsg").innerHTML = '<b>Username alreay exists</b>';
			alert('Username alreay exists');
		}
	}
	else {
		document.getElementById("userErrorMsg").innerHTML = '<b>Invalid username and password</b>';
	}
}
*/


function setInitialShippingvalue() {
	if (document.getElementById("SelectedShippingType")) {
		shipVal = document.getElementById("SelectedShippingType").value;
		//alert(shipVal);
		updateShippingType(shipVal);
	}	
}


