// remote scripting library
// (c) copyright 2005 modernmethod, inc
var sajax_debug_mode = false;
var sajax_request_type = "GET";

function replace(str, from, to) {
	var i = str.indexOf(from);
	if (!from || !str || i == -1) return str;
	var newstr = str.substring(0, i) + to;
	if (i+from.length < str.length)
	newstr += replace(str.substring(i+from.length,str.length),from,to);
	return newstr;
}

function sajax_debug(text) {
	if (sajax_debug_mode)
		alert("RSD: " + text);
}
function init_object() {
	sajax_debug("init_object() called..");
	
	var A;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			A=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (oc) {
			A=null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined")
		A = new XMLHttpRequest();
	if (!A)
		sajax_debug("Could not create connection object.");
	return A;
}
function do_call(func_name, args) {
	var i, x, n;
	var uri;
	var post_data;
	var len = args.length - 1;
	
	uri = "";
	if (sajax_request_type == "GET") {
		if (uri.indexOf("?") == -1) 
			uri = uri + "?rs=" + escape(func_name);
		else
			uri = uri + "&rs=" + escape(func_name);
		for (i = 0; i < len; i++) 
			uri = uri + "&rsargs[]=" + escape(args[i]);
		uri = uri + "&rsrnd=" + new Date().getTime();
		post_data = null;
	} else {
		post_data = "rs=" + escape(func_name);
		for (i = 0; i < len; i++) 
			post_data = post_data + "&rsargs[]=" + escape(args[i]);
	}
	
	x = init_object();
	x.open(sajax_request_type, uri, true);
	if (sajax_request_type == "POST") {
		x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
		x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	x.onreadystatechange = function() {
		if (x.readyState != 4) 
			return;
		sajax_debug("received " + x.responseText);
		
		var status;
		var data;
		var func = args[len];
		status = x.responseText.charAt(0);
		// data = x.responseText.substring(2);
		data = replace(x.responseText.substring(2),'\n',' ');
		if (status == "-") 
			alert("Error: " + data);
		else 
			// alert("data: " + data);
			// eval(func + "('" + data + "')");
			eval(func)(data);
	}
	x.send(post_data);
	sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
	sajax_debug(func_name + " waiting..");
	delete x;
}

		
// wrapper for getPrice		
function x_getPrice() {
	do_call("getPrice",
		x_getPrice.arguments);
}

		
// wrapper for setSession		
function x_setSession() {
	do_call("setSession",
		x_setSession.arguments);
}


// cross-platform compatible function to grab DOM elements by their ID
function getElement(id) {
	var obj = (document.getElementById) ? document.getElementById(id) : document.all[id];
	return obj;
}

// this function gets called by Sajax after the server
// function has been called with the result from the
// server function	
function getPrice_cb(price) {
	// castd formatted_price as a number
	
	var formatted_price = Number(price);

	// find the target text box
	target = getElement("priceTarget");

				if (price == '' || price == 0) {
			
			// not a possible combination				
			formatted_price = "N/A";
		} else {
			// formatted_price = Math.round(price * 100) * .01;
			formatted_price = Math.ceil(price);
		}			
			target.value = formatted_price;
	
	//define 'price' texbox size at runtime
	if(getElement("seeBelowPrice").style.display=="inline") {
		target.size = '9';
		
		getElement("currency").innerHTML = "See Below";
		getElement("currency").setAttribute("style","font-size:12px;font-weight:bold;");
		
	} else if(formatted_price == "N/A") {
		target.size = '3';
		getElement("currency").innerHTML = formatted_price;
	} else {
		target.size = '6';
		getElement("currency").innerHTML = "$" + formatted_price;
	}
	
		}

// grab the data to send to Sajax on the server	
function do_getPrice(productID)
{
	var widthIndex, heightIndex;
	
	selectWidth = getElement("select_width");
	extraWidth = getElement("width_extra");
	selectHeight = getElement("select_height");
	extraHeight = getElement("height_extra");

	widthIndex = selectWidth.selectedIndex;
	heightIndex = selectHeight.selectedIndex;

	// set the session to the current values
	x_setSession(selectWidth.options[widthIndex].value,selectHeight.options[heightIndex].value,extraWidth.options[extraWidth.selectedIndex].value,extraHeight.options[extraHeight.selectedIndex].value,"setSession_cb");

	if (extraWidth.selectedIndex > 0 && selectWidth.selectedIndex < (selectWidth.length - 1)) {
		// if the user selected a fraction, then
		// increment the inchage we are using
		widthIndex++;
	}	
	Width = selectWidth.options[widthIndex].value;
	
	if (extraHeight.selectedIndex > 0 && selectHeight.selectedIndex < (selectHeight.length - 1)) {
		heightIndex++;
	}
	Height = selectHeight.options[heightIndex].value;

	// make the Sajax call
	x_getPrice(productID,Width,Height,"getPrice_cb");
}

function setSession_cb(z) {
	// alert(z);
}

