// 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 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;
}

// ####################################################################################################################
//	RUNS EVERYTIME A SELECT BOX HAS BEEN CHANGED - ALLOWS THE USER TO PROGRESS THROUGH THE FORM
// OR IF THEY HAVE CHOSEN A BLANK VALUE, THEY MUST GO BACK (AND IT CLEARS ALL SUBSEQUENT FORM VALUES)
// ####################################################################################################################
// takes:
//	current element id
// the current counter 
//	1 or 0 depending if the field is mandatory or not.
function proceed(element, counter, mandatory){
	
	alert(this);
	browser=navigator.appName; 
	// We're always dealing with the next ID, not the current one so we need to increment to start with
	counter++;
//	alert('mandatory = '+mandatory);
	if(mandatory == 0) {
		if(document.getElementById('field'+counter))	{
			
			if(browser == "Microsoft Internet Explorer")	document.getElementById('field'+counter).style.display = 'block';	
			else document.getElementById('field'+counter).style.display = 'table-row';
			
		} else {
			// There are no more ID's so we're at the end of the form - This will eventually submit.
			alert('end of form');
		}
		return;
	}
	
	// Checks to see if the current ID's value is blank
	if(element.value == '') {
		
		// If its blank, we need to hide every available ID beyond this so we're checking if it exists first so we don't
		// get hundreds of javascript errors!
		while(document.getElementById('field'+counter)){
			document.getElementById('field'+counter).style.display = 'none';
			counter++;
		}
		
	// If we're at this point, the user has chosen a value
	} else {
		
		// If the next select box is available (exists) we can display the row
		if(document.getElementById('field'+counter))	{
			
			if(browser == "Microsoft Internet Explorer")	document.getElementById('field'+counter).style.display = 'block';	
			else document.getElementById('field'+counter).style.display = 'table-row';	
			
			post_data('classes/dab/dab.ajax.php?wizard_id='+wizard_id,'wizard','product_wizard');	
		} else {
			// There are no more ID's so we're at the end of the form - This will eventually submit.
			alert('end of form');
		}
	}
}
	
function next(wizard_id, counter, session_count){

	counter++;
	
	if(counter < session_count) {
		check = confirm('This change will undo any choices you made after this point.\r\nAre you sure you wish to proceed?');	
		if(!check)return false;
	}	

	if(document.getElementById('field'+counter)){

		post_data('classes/dab/dab.ajax.php?wizard_id='+wizard_id+'&counter='+counter,'wizard','product_wizard');	
	
	} else {
		//alert('Wizard complete!\r\nPlease check your product on the right-hand side\r\nIf you are happy with your selections, please press "BUY"');	
	}
	
}

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}

function loader(return_id, colour) {
	document.getElementById(return_id).innerHTML = "<div style='text-align:left;'><img src='images/loader_"+colour+".gif' style='margin:5px;' /></div>";
}


document.onkeypress = stopRKey; 

var xmlHttp = false;

//-----------------------------------------------------------------------------------------------------------------------------

// 	get_data()

// 	page			= the script that you want to run in the background
//		element_id	= the id of the element that the process with return to

//-----------------------------------------------------------------------------------------------------------------------------

function get_data(page, element_id) {

	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Sorry, your browser does not support Ajax. Please upgrade!");
				return false;
			}
		}
	}	
	
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			document.getElementById(element_id).innerHTML = xmlHttp.responseText;
		}
	}
	
	xmlHttp.open("GET", page, true);
	xmlHttp.send(null);

}

//-----------------------------------------------------------------------------------------------------------------------------

// 	post_data()

// 	page			= the script that you want to run in the background
//		form_name	= the 'name' of the form that the data will retrieve
//		element_id	= the id of the element that the process with return to

//-----------------------------------------------------------------------------------------------------------------------------

function post_data(page, form_name, element_id) {

	function return_post_data() {

		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				result = xmlHttp.responseText;
				document.getElementById(element_id).innerHTML = result;            
			} else {
				alert("AJAX request failed. Please check your connection and try again.");
			}
		}
	}
	
	// Proper start of the function (builds up the POST string - looking at all the fields)
	var parameters = "";
	
	for (i = 0; i < document.forms[form_name].elements.length; i++) {
		key = document.forms[form_name].elements[i].name;
		value = document.forms[form_name].elements[i].value;
		value = encodeURI(value);
		value = encodeURIComponent(value);
		
		if(typeof key != 'undefined'){
			// If its an unchecked checkbox, get rid of it
			if(document.forms[form_name].elements[i].type == "checkbox") {
				if(document.forms[form_name].elements[i].checked) {
					parameters += "&" + key + "=" + value;
				}
			} 
			// If its a radio button
			else if (document.forms[form_name].elements[i].type == "radio") {
				if(document.forms[form_name].elements[i].checked) {
					parameters += "&" + key + "=" + value;
				}
			} else {
				parameters += "&" + key + "=" + value;
			}
		}			
	}
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Sorry, your browser does not support Ajax. Please upgrade!");
				return false;
			}
		}
	}	
	
	xmlHttp.onreadystatechange = return_post_data;
	xmlHttp.open("POST", page, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
			
}	




