function MyHttpObject() {
    this.lookupAddress = lookupAddress;
    this.postLookupAddress = postLookupAddress;
    this.setUrl = setUrl;
    this.setQuery = setQuery;
    this.setCallback = setCallback;
    this.getResult = getResult;

    var url = "";
    var query = "";
    var callback = "";
    var result = "";
    var resultObj = "";
    var http = getHTTPObject();

function lookupAddress()
    {
        http.open("GET", url, true);
        http.onreadystatechange = handleHttpResponse;
        http.send(null);
    }
    
function postLookupAddress()
	{
		http.open("POST", url, true);
		http.onreadystatechange = handleHttpResponse;
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   		http.setRequestHeader("Content-length", query.length);
		http.setRequestHeader("Connection","close");
	    http.send(query);
	}

function handleHttpResponse() {
        if (http.readyState == 4 && http.status == 200)
        {
            result = http.responseText;
            resultObj = http.responseXML;
            eval(callback);
        }
    }

    function setUrl(newUrl)
    {
        url = newUrl;
    }
    function setQuery(newQuery)
    {
        query = newQuery;
    }
    function setCallback(newCallback)
    {
        callback = newCallback;
    }
    function getResult()
    {
        return result;
    }

    function getHTTPObject()
    {
        if (window.XMLHttpRequest) { 
            http_request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { 
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                http_request = false;
                }
            }
        }
        return http_request;
    }

}

function estimate_shipping(ShippingForm, type)
{
	method = ShippingForm.options[ShippingForm.selectedIndex].value;
	
    
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=estimate_Shipping&ShippingType=' + type + '&ShippingMethod=' + method );
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function reset_shipping(type)
{
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=reset_Shipping&Type=' + type);
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function updateQuantity(itemNumber){
	http = new MyHttpObject();
	item_position = 'item_' + itemNumber;
	quantity = document.getElementById(item_position).quantity.value;
	http.setUrl('/functions/cart_functions.php?action=update_Quantity&ItemNumber=' + itemNumber + '&Quantity=' + quantity );
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function removeItem(itemNumber){
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=remove_Item&ItemNumber=' + itemNumber );
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function clearCart(){
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=clear_Cart' );
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function submitDiscount(){
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=submit_Discount&DiscountCode=' + document.getElementById('discount_code').value );
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function removeDiscount(){
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=remove_Discount' );
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function removeCouponCode(code){
	http = new MyHttpObject();
	http.setUrl('/functions/cart_functions.php?action=remove_CouponCode&code=' + code);
	http.setCallback('document.getElementById(\'cart_content\').innerHTML =  result; fade_message();');
	http.lookupAddress();
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

var alertTimerId = 0;

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity < 1) {
		object.display = "none";
	}
	else {
		object.display = "block";
	}
} 

function show_message()
{
	if(document.getElementById('message_block').style.display == "none")
		document.getElementById('message_block').style.display = "block";
}

function fade_message()
{
	clearTimeout(alertTimerId);
	document.getElementById('message_block').style.display = "block";
	alertTimerId = setTimeout("fade_block('message_block')",3000);
	
}

function fade_block(block)
{
	opacity(block,100,0,300);
}

function updatePaymentChargeType(type)
{
	http = new MyHttpObject();
	http.setUrl('/functions/checkout_functions.php?action=updatePaymentChargeType&charge_type=' + type );
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.lookupAddress();
}

function checkoutFocus(stepNumber)
{
	http = new MyHttpObject();
	http.setUrl('/functions/checkout_functions.php?action=set_Focus&step_Number=' + stepNumber );
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.lookupAddress();
}

function continueCheckout()
{
	http = new MyHttpObject();
	http.setUrl('/functions/checkout_functions.php?action=continue_Checkout');
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.lookupAddress();
}

function login()
{
	http = new MyHttpObject();
	username = encodeURIComponent(document.getElementById('Email').value);
	password = encodeURIComponent(document.getElementById('Password').value);
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery('action=Login&Username=' + username + '&Password=' + password);
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.postLookupAddress();
}

function check_country()
{
	countrySelect = document.getElementById('Country');
	theIndex = countrySelect.selectedIndex;
	country = countrySelect.options[theIndex].value;
	if(country != "United States")
	{
		document.getElementById('State_Block').style.display="none";
		document.getElementById('Region_Block').style.display="block";
	}
	else
	{
		document.getElementById('State_Block').style.display="block";
		document.getElementById('Region_Block').style.display="none";
	}
}

function submitBilling()
{
	http = new MyHttpObject();
	
	var password;
	var confirm_password;
	
	customer_account = encodeURIComponent(document.getElementById('customer_account').value);
	first_name = encodeURIComponent(document.getElementById('First_Name').value);
	last_name = encodeURIComponent(document.getElementById('Last_Name').value);
	street_address = encodeURIComponent(document.getElementById('Street_Address').value);
	street_address_2 = encodeURIComponent(document.getElementById('Street_Address_2').value);
	city = encodeURIComponent(document.getElementById('City').value);
	countrySelect = document.getElementById('Country');
	theIndex = countrySelect.selectedIndex;
	country = countrySelect.options[theIndex].value;
	if(country == "United States")
	{
		stateSelect = document.getElementById('State');
		stateIndex = stateSelect.selectedIndex;
		state = stateSelect.options[stateIndex].value;
	}
	else
	{
		state = encodeURIComponent(document.getElementById('Region').value);
	}
	zip = encodeURIComponent(document.getElementById('Zip').value);
	phone = encodeURIComponent(document.getElementById('Phone').value);
	email_address = encodeURIComponent(document.getElementById('Email_Address').value);
	emailSelect = document.getElementById('Email_Type');
	emailIndex = emailSelect.selectedIndex;
	email_type = encodeURIComponent(emailSelect.options[emailIndex].value);
	if(customer_account == "")
	{
		if(document.getElementById('Password'))
			password = encodeURIComponent(document.getElementById('Password').value);
		else
			password = '';
		
		if(document.getElementById('ConfirmPassword'))
			confirm_password = encodeURIComponent(document.getElementById('ConfirmPassword').value);
		else
			confirm_password = '';
	}
	
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery('action=continue_Checkout&First_Name=' + first_name + '&Last_Name=' + last_name + '&Street_Address=' + street_address + '&Street_Address_2=' + street_address_2 + '&City=' + city + '&State=' + state + '&Zip=' + zip + '&Country=' + country + '&Phone=' + phone + '&Email_Address=' + email_address + '&Email_Type=' + email_type + '&Password=' + password + '&ConfirmPassword=' + confirm_password);
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.postLookupAddress();
}

function submitShipping()
{
	http = new MyHttpObject();
	
	first_name = encodeURIComponent(document.getElementById('First_Name').value);
	last_name = encodeURIComponent(document.getElementById('Last_Name').value);
	street_address = encodeURIComponent(document.getElementById('Street_Address').value);
	street_address_2 = encodeURIComponent(document.getElementById('Street_Address_2').value);
	city = encodeURIComponent(document.getElementById('City').value);
	countrySelect = document.getElementById('Country');
	theIndex = countrySelect.selectedIndex;
	country = countrySelect.options[theIndex].value;
	if(country == "United States")
	{
		stateSelect = document.getElementById('State');
		stateIndex = stateSelect.selectedIndex;
		state = stateSelect.options[stateIndex].value;
	}
	else
	{
		state = encodeURIComponent(document.getElementById('Region').value);
	}
	zip = encodeURIComponent(document.getElementById('Zip').value);
	phone = encodeURIComponent(document.getElementById('Phone').value);
	
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery('action=continue_Checkout&First_Name=' + first_name + '&Last_Name=' + last_name + '&Street_Address=' + street_address + '&Street_Address_2=' + street_address_2 + '&City=' + city + '&State=' + state + '&Zip=' + zip + '&Country=' + country + '&Phone=' + phone);
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.postLookupAddress();
}

function copyBillingInfo()
{
	http = new MyHttpObject();
	if(document.getElementById('shipping_same').checked)
	{
		value = "copy";
		
		http.setUrl('/functions/checkout_functions.php?action=copy_Billing_Info&Value=' + value);
		http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
		http.lookupAddress();
	}
	else
	{
		value = "reset";
		
		http.setUrl('/functions/checkout_functions.php?action=copy_Billing_Info&Value=' + value);
		http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
		http.lookupAddress();
	}
	
}

function submitShippingMethod()
{
	form = document.frmShippingMethod;
	shippingMethod = "none";
	
	if(form.shipping_method[0])
	{
		for(var i=0; i < form.shipping_method.length; i++) 
		{
			if(form.shipping_method[i].checked)
				shippingMethod = form.shipping_method[i].value;
		}
	}
	else
	{
		if(form.shipping_method.checked)
			shippingMethod = form.shipping_method.value;
	}
	
	http = new MyHttpObject();
	http.setUrl('/functions/checkout_functions.php?action=continue_Checkout&Shipping_Method=' + shippingMethod );
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.lookupAddress();
}

function submitPayment()
{
	var query = '';
	
	if(document.getElementById('Paypal_Charge_Radio') && document.getElementById('Paypal_Charge_Radio').checked)
	{
		query = 'action=continue_Checkout';
	}
	else
	{
		cc_name = encodeURIComponent(document.getElementById('CC_Name').value);
		
		typeSelect = document.getElementById('CC_Type');
		typeIndex = typeSelect.selectedIndex;
		cc_type = typeSelect.options[typeIndex].value;
			
		cc_number = encodeURIComponent(document.getElementById('CC_Number').value);
		cc_expiration_month = encodeURIComponent(document.getElementById('CC_Expiration_Month').value);
		cc_expiration_year = encodeURIComponent(document.getElementById('CC_Expiration_Year').value);
		cc_ccv = encodeURIComponent(document.getElementById('CC_CCV').value);
		
		query = 'action=continue_Checkout&CC_Name=' + cc_name + '&CC_Type=' + encodeURIComponent(cc_type) + '&CC_Number=' + cc_number + '&CC_Expiration_Month=' + cc_expiration_month + '&CC_Expiration_Year=' + cc_expiration_year+ '&CC_CCV=' + cc_ccv;
	}
	
	http = new MyHttpObject();
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery(query);
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.postLookupAddress();
}

function updateBackorder()
{
	http = new MyHttpObject();
	form = document.frmCheckout;
	for(var i=0; i < form.Backorder.length; i++) 
	{
		if(form.Backorder[i].checked)
			backorder = form.Backorder[i].value;
	}
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery('action=UpdateBackorder&Backorder=' + backorder);
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.postLookupAddress();
}

function updateCustomerType()
{
	http = new MyHttpObject();
	form = document.frmCheckout;
	for(var i=0; i < form.Customer_Type.length; i++) 
	{
		if(form.Customer_Type[i].checked)
			customerType = form.Customer_Type[i].value;
	}
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery('action=UpdateCustomerType&CustomerType=' + customerType);
	http.setCallback('document.getElementById(\'checkout_content\').innerHTML =  result; show_message();');
	http.postLookupAddress();
}

function updateOtherCustomerType() 
{
	http = new MyHttpObject();
	
	var otherType = encodeURIComponent(document.getElementById('Other_Type').value);
	
	http.setUrl('/functions/checkout_functions.php');
	http.setQuery('action=UpdateOtherCustomerType&OtherType=' + otherType);
	http.postLookupAddress();
}

function getCapQueryString()
{
	var pd_id = encodeURIComponent(document.getElementById('pd_id').value);
	
	var styleSelect = document.getElementById('Style');
	var styleIndex = styleSelect.selectedIndex;
	var style = styleSelect.options[styleIndex].value;
	
	var numColorsSelect = document.getElementById('Num_Colors');
	var numColorsIndex = numColorsSelect.selectedIndex;
	var numColors = numColorsSelect.options[numColorsIndex].value;
	
	var mainPrintSelect = document.getElementById('Main_Print');
	var mainPrintIndex = mainPrintSelect.selectedIndex;
	var mainPrint = mainPrintSelect.options[mainPrintIndex].value;
	
	var addSwimmerNamesSelect = document.getElementById('Add_Swimmer_Names');
	var addSwimmerNamesIndex = addSwimmerNamesSelect.selectedIndex;
	var addSwimmerNames = addSwimmerNamesSelect.options[addSwimmerNamesIndex].value;
	
	var quantity = document.getElementById('Quantity').value;
	
	var query_string = '&PD_ID=' + pd_id + '&Style=' + style + '&NumColors=' + numColors + '&MainPrint=' + mainPrint + '&AddSwimmerNames=' + addSwimmerNames + '&Quantity=' + quantity;
	
	if(document.getElementById('Cap_Text'))
	{
		var capText = encodeURIComponent(document.getElementById('Cap_Text').value);
		
		query_string += '&CapText=' + capText;
	}
	
	if(document.getElementById('Cap_Text_Color'))
	{
		var capTextColor = encodeURIComponent(document.getElementById('Cap_Text_Color').value);
		
		query_string += '&CapTextColor=' + capTextColor;
	}
	
	if(document.getElementById('Swimmers_Names'))
	{
		var swimmersNames = encodeURIComponent(document.getElementById('Swimmers_Names').value);
		
		query_string += '&SwimmersNames=' + swimmersNames;
	}
	
	return query_string;
}

function updateCapSelection()
{
	var query_string = 'action=updateCapSelection';
	query_string += getCapQueryString();
	
	http = new MyHttpObject();
	http.setUrl('/functions/product_functions.php');
	http.setQuery(query_string);
	http.setCallback('document.getElementById(\'cap_selection_info\').innerHTML =  result;');
	http.postLookupAddress();
}

function removeCapLogo()
{
	var query_string = 'action=removeCapLogo';
	query_string += getCapQueryString();
	
	http = new MyHttpObject();
	http.setUrl('/functions/product_functions.php');
	http.setQuery(query_string);
	http.setCallback('document.getElementById(\'cap_selection_info\').innerHTML =  result;');
	http.postLookupAddress();
}

function uploadCapLogo()
{
	var capFileName = encodeURIComponent(document.getElementById('capLogo').value);
	
	var query_string = 'action=uploadCapLogo&CapFileName=' + capFileName
	
	
	http = new MyHttpObject();
	http.setUrl('/functions/product_functions.php');
	http.setQuery(query_string);
	http.setCallback('document.getElementById(\'logoResults\').innerHTML =  result;');
	http.postLookupAddress();
}

function loadSizes()
{
	var pd_id = encodeURIComponent(document.getElementById('pd_id').value);
	
	var styleSelect = document.getElementById('Style');
	var styleIndex = styleSelect.selectedIndex;
	var style = styleSelect.options[styleIndex].value;
	
	http = new MyHttpObject();
	http.setUrl('/functions/product_functions.php');
	http.setQuery('action=loadSizes&PD_ID=' + pd_id + '&Style=' + style);
	http.setCallback('document.getElementById(\'size_box\').innerHTML =  result;');
	http.postLookupAddress();
}

function loadSizesForReturn(row, pd_id)
{
	var styleSelect = document.getElementById('Style_' + row);
	var styleIndex = styleSelect.selectedIndex;
	var style = styleSelect.options[styleIndex].value;
	
	http = new MyHttpObject();
	http.setUrl('/functions/product_functions.php');
	http.setQuery('action=loadSizesForReturn&PD_ID=' + pd_id + '&Style=' + style + '&Row=' + row);
	http.setCallback('document.getElementById(\'size_box_' + row + '\').innerHTML =  result;');
	http.postLookupAddress();
}

function checkSelectedColorSize()
{
	var styleSelect = document.getElementById('Style');
	var styleIndex = styleSelect.selectedIndex;
	var style = styleSelect.options[styleIndex].value;
	
	var sizeSelect = document.getElementById('Size');
	var sizeIndex = sizeSelect.selectedIndex;
	var size = sizeSelect.options[sizeIndex].value;
	
	if(style == "none" || size == "none")
	{
		alert("You must select a style and size in order to add a product to the cart!");
		return false;
	}
	else
		return true;
}

function checkSelectedColorSizeForReturn(max_row)
{

	var good = true;	
	
	for(var i=1; i <= max_row; i++) 
	{
		if(document.getElementById('Style_' + i))
		{
			var styleSelect = document.getElementById('Style_' + i);
			var styleIndex = styleSelect.selectedIndex;
			var style = styleSelect.options[styleIndex].value;
			
			if(style == "none")
				good = false;
		}
		
		if(document.getElementById('Size_' + i))
		{
			var sizeSelect = document.getElementById('Size_' + i);
			var sizeIndex = sizeSelect.selectedIndex;
			var size = sizeSelect.options[sizeIndex].value;
			
			if(size == "none")
				good = false;
		}
	}
	
	if(good == false)
	{
		alert("You must select a style and size in order to add a product to the cart!");
		return false;
	}
	else
		return true;
}
