// Global Variables

var ajax_controller = '/apps/store/cart_ajax.asp';
var guiForm;
var orderItemsForm;
var dictionaryForm;
var guiFormName = '';
var chooserFilter = '';
var queryCount = 1;
var orderID = '0';
var itemID = '';

// ------------------------------------------------------------------------------

function showOrder(vOrderID,vRefresh) {
    orderID = vOrderID;
	v_callmethod = 'showorder';
	if (vRefresh == 'true') {
	    document.getElementById('CurrentOrder').innerHTML = '<div style="font-size:14pt;font-weight:bold;text-align:center;"><img src="/images_system/loading1.gif" border="0" align="absmiddle" /> Retrieving Your Order...</div>';
	}
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + vOrderID;
	strURL = strURL + '&itemid=' + itemID; 
	//alert(strURL); 
	PassAjaxResponseToFunction(strURL, 'showOrder_return', 'null');
	return true;
}

function showOrder_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		document.getElementById('CurrentOrder').innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function showBuyer(vOrderID,vRefresh,vMode) {
	v_callmethod = 'showbuyer';
	if (vRefresh == 'true') {
	    document.getElementById('CustomerInformation').innerHTML = '<div style="font-size:14pt;font-weight:bold;text-align:center;"><img src="/images_system/loading1.gif" border="0" align="absmiddle" /> Retrieving Your Information...</div>';
	}
	if (vMode == 'billing') { 
	    document.getElementById('ChangeBilling').innerHTML = '<img src="\/images_system\/spotloader.gif">';
	} else if (vMode == 'shipping') {
	    document.getElementById('ChangeShipping').innerHTML = '<img src="\/images_system\/spotloader.gif">';
	}
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + vOrderID;
	strURL = strURL + '&mode=' + vMode;
	//alert(strURL);  
	PassAjaxResponseToFunction(strURL, 'showBuyer_return', 'null');
	return true;
}

function showBuyer_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		document.getElementById('CustomerInformation').innerHTML = arr_response[1];
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function showLogin() {
	v_callmethod = 'showlogin';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	//alert(strURL); 
	PassAjaxResponseToFunction(strURL, 'showLogin_return', 'null');
	return true;
}

function showLogin_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		document.getElementById('CustomerInfoArea').innerHTML = arr_response[1];
		document.getElementById('StoreLoginUsername').focus();
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function processLogin() {
	v_callmethod = 'processlogin';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&username=' + escape(document.getElementById('StoreLoginUsername').value);
	strURL = strURL + '&password=' + escape(document.getElementById('StoreLoginPassword').value);
	//alert(strURL); 
	PassAjaxResponseToFunction(strURL, 'processLogin_return', 'null');
	return true;
}

function processLogin_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showBuyer(orderID,'true','');
		showOrder(orderID,'true');
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function showRegistration() {
	v_callmethod = 'showregistration';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	//alert(strURL); 
	PassAjaxResponseToFunction(strURL, 'showRegistration_return', 'null');
	return true;
}

function showRegistration_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		document.getElementById('CustomerInfoArea').innerHTML = arr_response[1];
		document.getElementById('StoreFname').focus();
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function processRegistration() {
    if (document.getElementById('StoreFname').value != '' && document.getElementById('StoreLname').value != '' && document.getElementById('StoreEmail').value != '' 
        && document.getElementById('StoreAddress').value != '' && document.getElementById('StoreCity').value != '' && document.getElementById('StoreState').value != '' 
        && document.getElementById('StoreZip').value != '' && document.getElementById('StoreCountry').value != '' && document.getElementById('StorePhone').value != '' 
        && document.getElementById('StoreUsername').value != '' && document.getElementById('StorePassword').value != '' && document.getElementById('StorePasswordConfirm').value != '') {
    	v_callmethod = 'processregistration';
    	strURL = 'callmethod=' + v_callmethod
    	strURL = strURL + '&fname=' + escape(document.getElementById('StoreFname').value);
    	strURL = strURL + '&lname=' + escape(document.getElementById('StoreLname').value);
    	strURL = strURL + '&email=' + escape(document.getElementById('StoreEmail').value);
    	strURL = strURL + '&company=' + escape(document.getElementById('StoreCompany').value);
    	strURL = strURL + '&title=' + escape(document.getElementById('StoreTitle').value);
    	strURL = strURL + '&address=' + escape(document.getElementById('StoreAddress').value);
    	strURL = strURL + '&city=' + escape(document.getElementById('StoreCity').value);
    	strURL = strURL + '&state=' + escape(document.getElementById('StoreState').value);
    	strURL = strURL + '&zip=' + escape(document.getElementById('StoreZip').value);
    	strURL = strURL + '&country=' + escape(document.getElementById('StoreCountry').value);
    	strURL = strURL + '&phone=' + escape(document.getElementById('StorePhone').value);
    	strURL = strURL + '&fax=' + escape(document.getElementById('StoreFax').value);
    	strURL = strURL + '&username=' + escape(document.getElementById('StoreUsername').value);
    	strURL = strURL + '&password=' + escape(document.getElementById('StorePassword').value);
    	strURL = strURL + '&password2=' + escape(document.getElementById('StorePasswordConfirm').value);
    	//alert(strURL); 
    	PassAjaxResponseToFunctionUsingPost(ajax_controller,strURL, 'processRegistration_return', 'null');
    } else {
        alert('Please fill out all of the required fields.');
    }
	return true;
}

function processRegistration_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showBuyer(orderID,'true','');
		showOrder(orderID,'true');
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function processReminder() {
	v_callmethod = 'processreminder';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&remindertext=' + escape(document.getElementById('StoreLoginReminderText').value);
	//alert(strURL); 
	PassAjaxResponseToFunction(strURL, 'processReminder_return', 'null');
	return true;
}

function processReminder_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		alert('Your password has been sent! Please check your e-mail in a few minutes.');
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updateOrderQuantity(vEntryID,vQuantity) {
    document.getElementById('UpdateCart').innerHTML = '<img src="\/images_system\/spotloader.gif">';
    if (document.getElementById('OrderComplete')) { document.getElementById('OrderComplete').innerHTML = '<img src="\/images_system\/spotloader.gif">'; }
	v_callmethod = 'updateorderquantity';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + orderID;
	strURL = strURL + '&entryid=' + vEntryID;
	strURL = strURL + '&quantity=' + vQuantity;
	//alert(strURL);  
	PassAjaxResponseToFunction(strURL, 'updateOrderQuantity_return', 'null');
	return true;
}

function updateOrderQuantity_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showOrder(orderID,'false')
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updateOrderOption(vEntryID,vOptionField,vOptionID) {
    document.getElementById('UpdateCart').innerHTML = '<img src="\/images_system\/spotloader.gif">';
    document.getElementById('OrderComplete').innerHTML = '<img src="\/images_system\/spotloader.gif">';
	v_callmethod = 'updateorderoption';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + orderID;
	strURL = strURL + '&entryid=' + vEntryID;
	strURL = strURL + '&optionfield=' + vOptionField;
	strURL = strURL + '&optionid=' + vOptionID;
	//alert(strURL);   
	PassAjaxResponseToFunction(strURL, 'updateOrderOption_return', 'null');
	return true;
}

function updateOrderOption_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showOrder(orderID,'false') 
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updateCustomerInfo(vType) {
    vError = '';
    if (document.getElementById(vType + 'Name').value == '') { vError = vError + '- Missing Name\n'; }
    if (document.getElementById(vType + 'Email').value == '') { vError = vError + '- Missing Email\n'; }
    if (document.getElementById(vType + 'Address').value == '') { vError = vError + '- Missing Address\n'; }
    if (document.getElementById(vType + 'City').value == '') { vError = vError + '- Missing City\n'; }
    if (document.getElementById(vType + 'State').value == '') { vError = vError + '- Missing State\n'; }
    if (document.getElementById(vType + 'Zip').value == '') { vError = vError + '- Missing Zip\n'; }
    if (document.getElementById(vType + 'Country').selectedIndex == 0) { vError = vError + '- Missing Country\n'; }
    if (document.getElementById(vType + 'Phone').value == '') { vError = vError + '- Missing Phone\n'; }
    if (vError == '') {
        v_callmethod = 'updatecustomerinfo';
        document.getElementById('Change'+vType).innerHTML = '<img src="\/images_system\/spotloader.gif">';
        strURL = 'callmethod=' + v_callmethod
        strURL = strURL + '&orderid=' + orderID;
        strURL = strURL + '&type=' + vType;
        strURL = strURL + '&' + vType + 'Name=' + escape(document.getElementById(vType + 'Name').value); 
        if (document.getElementById(vType + 'Org')) { strURL = strURL + '&' + vType + 'Org=' + escape(document.getElementById(vType + 'Org').value); }
        strURL = strURL + '&' + vType + 'Email=' + escape(document.getElementById(vType + 'Email').value);
        strURL = strURL + '&' + vType + 'Address=' + escape(document.getElementById(vType + 'Address').value);
        strURL = strURL + '&' + vType + 'City=' + escape(document.getElementById(vType + 'City').value);
        strURL = strURL + '&' + vType + 'State=' + escape(document.getElementById(vType + 'State').value);
        strURL = strURL + '&' + vType + 'Zip=' + escape(document.getElementById(vType + 'Zip').value);
        strURL = strURL + '&' + vType + 'Country=' + escape(document.getElementById(vType + 'Country').value);
        strURL = strURL + '&' + vType + 'Phone=' + escape(document.getElementById(vType + 'Phone').value);
        //alert(strURL);   
        PassAjaxResponseToFunctionUsingPost(ajax_controller, strURL, 'updateCustomerInfo_return', 'null');
    } else {
        alert('Please correct the following omissions:\n\n' + vError);
    }
    return true;
}

function updateCustomerInfo_return(str_response) {
	//alert('Response: ' + str_response);  
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showBuyer(orderID,'false','');
		//if (arr_response[2] == 'Shipping') {
			showOrder(orderID,'true');
        //}
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updatePromoCode() {
    document.getElementById('UpdateCart').innerHTML = '<img src="\/images_system\/spotloader.gif">';
    if (document.getElementById('OrderComplete')) { document.getElementById('OrderComplete').innerHTML = '<img src="\/images_system\/spotloader.gif">'; }
	v_callmethod = 'updatepromocode';
	strURL = ajax_controller + '?' + 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + orderID;
	strURL = strURL + '&promocode=' + escape(document.getElementById('OrderPromoCode').value);
	//alert(strURL);  
	PassAjaxResponseToFunction(strURL, 'updatePromoCode_return', 'null'); 
	return true;
}

function updatePromoCode_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showOrder(orderID,'false')
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updateNotes() {
    document.getElementById('UpdateCart').innerHTML = '<img src="\/images_system\/spotloader.gif">';
    document.getElementById('OrderComplete').innerHTML = '<img src="\/images_system\/spotloader.gif">';
	v_callmethod = 'updatenotes';
	strURL = 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + orderID;
	strURL = strURL + '&ordernotes=' + escape(document.getElementById('OrderNotes').value);
	//alert(strURL);  
	//PassAjaxResponseToFunctionUsingPost(ajax_controller, strURL, 'updateNotes_return', 'null'); 
	PassAjaxResponseToFunctionUsingPost(ajax_controller, strURL, 'checkoutOrder', 'null'); 
	return true;
}

function updateNotes_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		//showOrder(orderID,'false')
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updateAdditionalDonation() {
    document.getElementById('UpdateCart').innerHTML = '<img src="\/images_system\/spotloader.gif">';
    document.getElementById('OrderComplete').innerHTML = '<img src="\/images_system\/spotloader.gif">';
	v_callmethod = 'updateadditionaldonation';
	strURL = 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + orderID;
	strURL = strURL + '&orderadditionaldonation=' + escape(document.getElementById('OrderAdditionalDonation').value);
	strURL = strURL + '&orderdonationdeductable=' + escape(document.getElementById('OrderDonationDeductable').checked);
	//alert(strURL);  
	PassAjaxResponseToFunctionUsingPost(ajax_controller, strURL, 'updateAdditionalDonation_return', 'null'); 
	return true;
}

function updateAdditionalDonation_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showOrder(orderID,'false')
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function updateShipping() {
    document.getElementById('UpdateCart').innerHTML = '<img src="\/images_system\/spotloader.gif">';
    document.getElementById('OrderComplete').innerHTML = '<img src="\/images_system\/spotloader.gif">';
	v_callmethod = 'updateshipping'; 
	strURL = 'callmethod=' + v_callmethod
	strURL = strURL + '&orderid=' + orderID;
	strURL = strURL + '&ordershipping=' + escape(document.getElementById('OrderShippingMethod').value);
	//alert(strURL);  
	PassAjaxResponseToFunctionUsingPost(ajax_controller, strURL, 'updateShipping_return', 'null'); 
	return true;
}

function updateShipping_return(str_response) {
	//alert('Response: ' + str_response); 
	arr_response = str_response.split('||');
	if ( arr_response[0] == '1') {
		showOrder(orderID,'false')
	} else {
		alert(arr_response[1]);
	}
	return true;
}

// ------------------------------------------------------------------------------

function checkoutOrder(str_response) {
	arr_response = str_response.split('||');
	//alert(arr_response[1]);
    if (document.getElementById('OrderShippingMethod')) {
        if (document.getElementById('OrderShippingMethod').selectedIndex == 0) {
            alert('Please choose a shipping method before completing your order.');
        } else if (document.getElementById('OrderShippingMethod')[document.getElementById('OrderShippingMethod').selectedIndex].text.indexOf('Error') > 0) {
            alert('Your shipping information is not allowing us to find a valid shipping method for you. Please verify your zip code and country information.');
        } else {
            location.href = '/apps/store/checkout.asp';
        }
    } else {
        location.href = '/apps/store/checkout.asp';
    }
}
