// JavaScript Document
function delete_item(item_id){
	document.location.href='index.php?option=basket&action=remove&basket_item_id='+item_id;		
}

function delete_checkout_item(item_id){
	if(confirm("Do you really want to delete this item from shopping basket?")){
		document.location.href='index.php?option=checkout1&action=remove&basket_item_id='+item_id;		
	}
}

function delete_quickcheckout_item(item_id){
	if(confirm("Do you really want to delete this item from shopping basket?")){
		document.location.href='index.php?option=quick_checkout2&action=remove&basket_item_id='+item_id;		
	}
}

function empty_basket(){
	if(confirm("Do you really want to delete this shopping basket?")){
		document.location.href='index.php?option=basket&action=clear';		
	} 
}

function warranty_more_info(){
	if(document.getElementById('warranty_more_info').style.display == "block"){
		document.getElementById('warranty_more_info').style.display = "none";
	} else {
		document.getElementById('warranty_more_info').style.display = "block";
	}
	
	return;
}

function makeRequest(url, parameters, obj, functionCallBack) {

	var httpRequest;

	if(typeof functionCallBack == 'undefined'){

		functionCallBack = '';	//call back function to be launch after the request is finished

	}

	

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...

		httpRequest = new XMLHttpRequest();

		if (httpRequest.overrideMimeType) {

			httpRequest.overrideMimeType('text/xml');

			// See note below about this line

		}

	} 

	else if (window.ActiveXObject) { // IE

		try {

			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");

			} 

			catch (e) {

					   try {

							httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

						   } 

						 catch (e) {}

					  }

								   }



	if (!httpRequest) {

		alert('Sorry we can\'t provide this service now.');

		return false;

	}

	

	//sends only POST request

	httpRequest.onreadystatechange = function() { ajaxContents(httpRequest, obj, functionCallBack); };

	httpRequest.open('POST', url, true);

	httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	httpRequest.setRequestHeader("Content-length", parameters.length);

	httpRequest.setRequestHeader("Connection", "close");

	httpRequest.send(parameters);

	

	return true;

}



function ajaxContents(httpRequest, obj, functionCallBack) {

	if (httpRequest.readyState == 4) {

		if (httpRequest.status == 200) {

			var result = httpRequest.responseText;

			if(result == "false") {

				alert('The request was not recognized. Try log in.');

			} else{

				//alert(result);
				//alert('done');

				if ( obj != null ) document.getElementById(obj).innerHTML = result;

				if(functionCallBack != "") eval(functionCallBack);

			}

		} else {

			//alert('Sorry the request was not successfull. Try again.');

		}

	}

}

function get_form_vars(obj_id){
	
	var theForm = document.getElementById('form_'+obj_id);
	
	var feature_name = '';
	
	for(i=0; i<theForm.elements.length; i++){
		var alertText = ""
		
		//alertText += "Element Type: " + theForm.elements[i].name + "\n";
		//alertText += "Element Type: " + theForm.elements[i].type + "\n";
	
		if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
		  	//alertText += "Element Value: " + theForm.elements[i].value + "\n";
		}else if(theForm.elements[i].type == "checkbox"){
		  	//alertText += "Element Checked? " + theForm.elements[i].checked + "\n";
		}else if(theForm.elements[i].type == "select-one"){
		  	//alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n";
		  	var tmp_name = theForm.elements[i].name;
		  	if(tmp_name.indexOf('feature_') == 0){
		  		feature_name = tmp_name;
		  		break;
		  	}
		}
		  
		//alert(alertText)
	}
	
	return feature_name;
	
}

function add_to_basket(obj_id, product_id){
	
	var feature_name = get_form_vars(obj_id);
	var url_string = "";
	
	if(feature_name != ""){
		var url_string = "&"+feature_name+"="+document.getElementById(feature_name).value;
	}
	
	var qty = document.getElementById('product_qty_' + obj_id).value;
	
	if(qty >= 0 && qty <= 1000000){
	
		var url = "/ajax/add_to_basket.php?product_qty="+qty+"&product_id="+product_id+"&product_size_id="+obj_id+url_string;
	
		makeRequest(url, '', 'info_'+obj_id, 'clear_info("'+obj_id+'");');
		
		url = "/ajax/express_checkout_basket.php";
		makeRequest(url, '', 'cards', '');
	
	} else {
		alert("Enter valid qty.");
	}
	
	document.getElementById('product_qty_' + obj_id).value = 1;
	
	return;
	
}

function clear_info(obj_id){
	document.getElementById('info_'+obj_id).style.display = 'block';
	setTimeout ("document.getElementById('info_"+obj_id+"').innerHTML = \"\";", 4000); 
	setTimeout ("document.getElementById('info_"+obj_id+"').style.display = \"none\";", 4000); 
}

