function _makeXMLRequest(url, parameters, routine) {
	var in_xml = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		in_xml = new XMLHttpRequest();
		if (in_xml.overrideMimeType) {
			in_xml.overrideMimeType('text/plain');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			in_xml = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				in_xml = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!in_xml) {
		alert('Cannot make connection to server. Please upgrade or use a different browser.');
		return false;
	}
	
	eval ("in_xml.onreadystatechange = "+routine);
	in_xml.open("POST", url, true);
	in_xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	in_xml.setRequestHeader("Content-length", parameters.length);
	in_xml.setRequestHeader("Connection", "close");
	in_xml.send(parameters);
	return in_xml;
}

function _getParams(params, fn_name) {
    var list = '';
    if (params.length % 2) {
        alert(fn_name+' requires an even number of items in the parameter list');
        return null;
    }
    for (i=0; i < params.length; i = i + 2) {
        list = list + '&' + escape(params[i]) + '=' + escape(params[i+1]); 
    }
    return list;
}

function _showWait() {
    var wait = document.getElementById('please_wait');
    if (wait) { wait.style.visibility = 'visible'; } 
}

function  _hideWait() {
    var wait = document.getElementById('please_wait');
    if (wait) { wait.style.visibility = 'hidden'; } 
}

/* Useful Functions */

function getRadioValue(n) {
	var radios = document.getElementsByName(n);
	for (i = 0; i < radios.length; i++) {
		if (radios[i].checked) return radios[i].value;
	}
}

/* ********************************************* */
/* Generated Function */
/* ********************************************* */

var get_cart_quantity_div = '';
var get_cart_quantity_action = '';
var get_cart_quantity_xml = '';

function get_cart_quantity(params, action, div_id) {
    get_cart_quantity_div = div_id;
    get_cart_quantity_action = action;
    var params_list = _getParams(params, 'get_cart_quantity');
    if (params_list == null) { 
        return false; 
    } else {
        _showWait();
 	    get_cart_quantity_xml = _makeXMLRequest('/', 'action=custom&akr=ajax_get_cart_quantity'+params_list, 'get_cart_quantity_handler');
    }
}

function get_cart_quantity_handler() {
    if (get_cart_quantity_xml.readyState != 4) return false;
    if (get_cart_quantity_xml.status != 200) return false;

    _hideWait();
    if (get_cart_quantity_action == 'alert') {
        alert( get_cart_quantity_xml.responseText );
    } else if (get_cart_quantity_action == 'set_html') {
        document.getElementById(get_cart_quantity_div).innerHTML = get_cart_quantity_xml.responseText;;
    }

}

/* ********************************************* */
/* Generated Function */
/* ********************************************* */

var get_logged_in_div = '';
var get_logged_in_action = '';
var get_logged_in_xml = '';

function get_logged_in(params, action, div_id) {
    get_logged_in_div = div_id;
    get_logged_in_action = action;
    var params_list = _getParams(params, 'get_logged_in');
    if (params_list == null) { 
        return false; 
    } else {
        _showWait();
        get_logged_in_xml = _makeXMLRequest('/', 'action=custom&akr=ajax_get_logged_in'+params_list, 'get_logged_in_handler');
    }
}

function get_logged_in_handler() {
    if (get_logged_in_xml.readyState != 4) return false;
    if (get_logged_in_xml.status != 200) return false;

    _hideWait();
    if (get_logged_in_action == 'alert') {
        alert( get_logged_in_xml.responseText );
    } else if (get_logged_in_action == 'set_html') {
        document.getElementById(get_logged_in_div).innerHTML = get_logged_in_xml.responseText;;
    }

}


/* ********************************************* */
/* Generated Function */
/* ********************************************* */

var get_options_div = '';
var get_options_action = '';
var get_options_xml = '';

function get_options(params, action, div_id) {
    get_options_div = div_id;
    get_options_action = action;
    var params_list = _getParams(params, 'get_options');
    if (params_list == null) { 
        return false; 
    } else {
        _showWait();
        get_options_xml = _makeXMLRequest('/', 'action=custom&akr=ajax_get_options'+params_list, 'get_options_handler');
    }
}

function get_options_handler() {
    if (get_options_xml.readyState != 4) return false;
    if (get_options_xml.status != 200) return false;

    _hideWait();
    if (get_options_action == 'alert') {
        alert( get_options_xml.responseText );
    } else if (get_options_action == 'set_html') {

    	var Result = get_options_xml.responseText.split('|~|');
    	for (i=0; i < Result.length; i=i+2) {
	    	if (Result[i] == '1') { // Style
		        document.getElementById('attribute1').innerHTML = Result[i+1];
    		} else if (Result[i] == '2') { // Size
				document.getElementById('attribute2').innerHTML = Result[i+1];
			} else if (Result[i] == '3') { // Matte
				document.getElementById('attribute3').innerHTML = Result[i+1];
			}
		}
		do_get_price();
    }
}


/* ********************************************* */
/* Generated Function */
/* ********************************************* */

var get_price_div = '';
var get_price_action = '';
var get_price_xml = '';

function get_price(params, action, div_id) {
    get_price_div = div_id;
    get_price_action = action;
    var params_list = _getParams(params, 'get_price');
    if (params_list == null) { 
        return false; 
    } else {
        _showWait();
        get_price_xml = _makeXMLRequest('/', 'action=custom&akr=ajax_get_price'+params_list, 'get_price_handler');
    }
}

function get_price_handler() {
    if (get_price_xml.readyState != 4) return false;
    if (get_price_xml.status != 200) return false;

    _hideWait();
    if (get_price_action == 'alert') {
        alert( get_price_xml.responseText );
    } else if (get_price_action == 'set_html') {
        document.getElementById(get_price_div).innerHTML = get_price_xml.responseText;;
    }

}
