/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	AJAX ADD TO CART for X-CART
	by www.cart-lab.com 2/2/2006 12:36PM
	last update: 6/12/2006 5:35PM
	www.cart-lab.com/license.php
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

// simple trim to help prevent whitespace
function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// Create the Divtag Handler -- Mainly an IE 6 Fix
function handleDivTag(divtag) {
	var divtag;
	return divtag;
}
var divhandler = new handleDivTag(null);

// Main Request
function makeHttpRequest(url, callback_function, divtag, post_get, return_xml, parameters) {

	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
		   http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
	    try {
	    	http_request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {}
	  }
	} else {
		alert('Unfortunatelly you browser doesn\'t support this feature.');
		return false;
	}

	// added to handle dynamic div tags instead of passing exact id
	divhandler.divtag = divtag;

  // display loading in top right corner
 	//default_loading(true,divtag);
 	
	// Create handler, display/hide loading screen
	if (!document.URL.match(/^https/)) {
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				// load completed
				//default_loading(false);
				if (return_xml) {
					eval(callback_function + '(http_request.responseXML)');
				} else {
					eval(callback_function + '(http_request.responseText)');
				}
			} else {
				//default_loading(false);
				alert('There was a problem with the request.(Code: ' + http_request.status + ')');
			}
		}
	}
	}

	if(post_get=="GET") {
		http_request.open('GET', url, true);
		http_request.send(null);
	} else {
		http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//_id = document.orderform.children.id.value;
		//_debug(document.orderform.all);
		for (_form in document.orderform.all) { 
			//alert(_form+":"+document.orderform.all[_form].value);
			parameters += "&"+_form+"="+document.orderform.all[_form].value;
		}
		//alert(parameters);
    //http_request.setRequestHeader("Content-length", parameters.length);
    //http_request.setRequestHeader("Connection", "close");
    //http_request.send(parameters);		
	}
}

// Default loading screen to help Usuability
function default_loading(loaded,divtag) {
	l = document.getElementById('loading');
	o = document.getElementById('overlay');
	if(loaded)  {
		hideObjects();
		l.style.display = "block";
		o.style.display = "block";
		l.innerHTML = "Loading... "+divtag;
	} else {
		showObjects();
		l.style.display = "none";
		o.style.display = "none";
		l.innerHTML = ""
	}
}

// User has pressed "Buy Now" event handler
function mycallback(response) {
	if(response) {
		document.getElementById(divhandler.divtag).innerHTML = trimString(response);
		makeHttpRequest('/store/customer/update_minicart.php', 'miniupdate', 'minicart', 'GET');
	}
}

//function init_login() {
	//if (!document.URL.match(/^https/)) {
		//makeHttpRequest('/store/customer/update_login.php', 'update_login', 'login_logout', 'GET');
	//}
//}

function update_login(response) {
	if(response) {	
		document.getElementById(divhandler.divtag).innerHTML = trimString(response);
	}
}


// Update minicart information and toggle it if its not open
function init_minicart() {
	if (!document.URL.match(/^https/)) {
		makeHttpRequest('/store/customer/update_minicart.php', 'miniupdate', 'minicart', 'GET');
	}
}

function save_recent(productid) {
	if (!document.URL.match(/^https/)) {
		makeHttpRequest('/store/customer/mm_recent.php?'+productid, 'recentupdate', 'recent', 'GET');
	}
}

// Update minicart information and toggle it if its not open
function miniupdate(response) {
	if(response) {	
		document.getElementById(divhandler.divtag).innerHTML = trimString(response);
		makeHttpRequest('/store/customer/update_recent.php', 'recentupdate', 'recent', 'GET');
	}
}
function recentupdate(response) {
	if(response) {	
		//alert(response);
		document.getElementById(divhandler.divtag).innerHTML = trimString(response);
		makeHttpRequest('/store/customer/update_login.php', 'update_login', 'login_logout', 'GET');
	}
}

function _debug(inobj) {
	op = window.open();
	op.document.open('text/plain');
	for (objprop in inobj) {
	op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
	}
	op.document.close();
}

