// STECH JSCRIPT ROUTINES



function createRequestObject() {
    var tmpXmlHttpObject;
    
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) { 
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

var http = createRequestObject();

var saveclicks = 0;

var pre1 = new Image();
pre1 = 'cart-saved.gif';

function stech_ajax_update_qty(product_id, quantity, add_btn, rem_btn) {

	if(quantity.value == -1)
		quantity.value = 0;

	add_btn.disabled = true;
	rem_btn.disabled = true;
	quantity.disabled = true;

	http.open('get', 'stech_ajax.php?function=set_quantity&product_id=' + product_id + '&set_quantity=' + quantity.value);
	
	var callback = function() {
			if(http.responseText == 'OK') {
				add_btn.disabled = false;
				rem_btn.disabled = false;
				quantity.disabled = false;
				
				saveclicks++;
				document.images.cartsave.src="cart-saved.gif";

				setTimeout("saveclicks--;if(saveclicks <= 0) document.images.cartsave.src='spacer.gif'", 3500);
			}
			else
				if(http.responseText == 'FAILED') {
					alert('Your shopping session has timed out due to inactivity.');

					window.reload();
				}
		};

    //assign a handler for the response
    http.onreadystatechange = callback;

    //actually send the request to the server
    http.send(null);
}

function blur_and_cancel(element) {
	element.blur();

	return false;
}

function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

function collection_getCount() {
	var inputs = document.getElementsByTagName('input');

	var ct = 0;

	for (var i = 0; i < inputs.length; i++) {

		if(inputs[i].type == "checkbox" && inputs[i].checked == true)
			ct++;
	}

	return ct;
}

function collection_disable_unchecked(disable) {

	var inputs = document.getElementsByTagName('input');

	for (var i = 0; i < inputs.length; i++) {

		if(inputs[i].type == "checkbox" && inputs[i].checked == false) {
			inputs[i].disabled = disable;

			document.getElementById('l-' + inputs[i].id).style.color = (disable ? '#D4D0C8' : '');
		}
	}
}

function collection_getMax() {

// this is a total hack

	if(document.cart_quantity) {
		if(document.getElementById('attrib-2-3') != null)
			return 4;
		else if(document.getElementById('attrib-3-17') != null)
			return 7;
		else
			return 99; // err on the side of not placing restrictions
	}

	return 99;
}

function collection_updateForm() {
	var count = collection_getCount();
	var max = collection_getMax();

	if(count < max) {
		collection_disable_unchecked(false);

		cdiv = document.getElementById("collectionCount");
		if(!cdiv)
			return;
		
		cdiv.style.display = "inline";
		cdiv.innerHTML = '<b><font color="red">Please select ' + (max - count) + ' more pen' + (max - count != 1 ? 's' : '') + '.</font></b>';
		document.getElementById("cartAdd").style.display = "none";
	}
	else {
		collection_disable_unchecked(true);

		document.getElementById("collectionCount").style.display = "none";
		document.getElementById("cartAdd").style.display = "inline";
	}
}

function collection_prepareInputs() {

	var inputs = document.getElementsByTagName('input');

	for (var i = 0; i < inputs.length; i++) {
		if(inputs[i].type == "checkbox") {
			inputs[i].onclick = collection_updateForm;
		}
	}

	collection_updateForm();
}

function stech_javascript_load() {

	collection_prepareInputs();
}
