function XMLHttpCreate() {
	try {
		var requester = new XMLHttpRequest();
	} catch (error) {
		try {
			var requester = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (error) {
			return false;
		}
	}
	return requester;
}

function stateHandler() {
	if (requester.readyState == 4) {
 		if (requester.status == 200) {
			createResponse();
		}
	}
	return true;
}

function createRequester(str) {
	requester = XMLHttpCreate();
	requester.onreadystatechange = stateHandler;
	requester.open("GET", str, true);
	requester.send(null);
}

function padZero(num) {
	if(num < 10) num = '0' + num;
	return num;
}

function convertToHumanTime(elapsedTime) {
	timeToDays = elapsedTime/86400;
	var days = parseInt(timeToDays);
	timeToHours = ((timeToDays-parseInt(timeToDays))*24).toPrecision(12);
	var hours = padZero(parseInt(timeToHours));
	timeToMinutes = (timeToHours-parseInt(timeToHours))*60;
	var minutes = padZero(parseInt(timeToMinutes));
	timeToSeconds = (timeToMinutes-parseInt(timeToMinutes))*60;
	var seconds = padZero(Math.ceil(timeToSeconds));
	return (days ? days + 'days ' : '') + hours + ':' + minutes + ':' + seconds;
}

var imageName = '';
var retrieveMode;
function createResponse() {
  switch(retrieveMode) {
    case 'getPrice':
    	var price = requester.responseXML.getElementsByTagName("option_value")[0].childNodes[0].nodeValue;

    	if (document.getElementById('totalPriceBottom')) {
        document.getElementById('totalPriceBottom').innerHTML = '£' + price;
    	}
    break;
    case 'getHelp':
    	var help = requester.responseXML.getElementsByTagName("help")[0].childNodes[0].nodeValue;
    	help = help.replace(/\n/g, '<br />');
      document.getElementById('helpContainer').style.visibility = 'visible'
    	document.getElementById('helpContainer').innerHTML = '<h4>BUILD INFORMATION</h4>' + '<p>' + help + '</p><div style="text-align:right"><a href="javascript:closeHelp()">Close X</div>';
    break;
    default:

    break;
  }
}

// Arreay of all the images associated with the supplied IDs
buildImageArray = new Array();
buildImageArray[6] = 'bp001.jpg';
buildImageArray[7] = 'bp001.jpg';
buildImageArray[8] = 'bp001.jpg';
buildImageArray[9] = 'bp001.jpg';
buildImageArray[10] = 'bp001.jpg';
buildImageArray[11] = 'bp001.jpg';
buildImageArray[12] = 'bp001.jpg';
buildImageArray[13] = 'bp001.jpg';
buildImageArray[14] = 'bp001.jpg';
buildImageArray[37] = 'bp002spcr.jpg';
buildImageArray[38] = 'bp002spbr.jpg';

function getPrice(attributeId, mode, checkboxChecked) {
  retrieveMode = 'getPrice';
  attributeId =  checkboxChecked == false ? 0 : attributeId;
  imageName = 'image_' + mode;
	createRequester('ajax.php?mode=' + mode + '&option_value=' + attributeId);
	// Loop through all the build pics and open in a new window if one existst for this particular ID
	for (var i in buildImageArray) {
	  if (i == attributeId) {
	    openBuildPic(buildImageArray[attributeId]);
	  }
	}
	//alert(attributeId)
  // Exceptions
  if (attributeId >= 45 && attributeId <= 47) { // Front castor - any option
    getCombinationPic('bp003', 'fc', attributeId);
  } else  if (attributeId == 53 || attributeId == 54 || attributeId == 55 || attributeId == 30001) { // Armrest Height >>> any option
    getCombinationPic('bp004', 'fcar', attributeId);
  } else  if (attributeId == 60 || attributeId == 61) { // Side panel colour - green / black
    getCombinationPic('bp005', 'fcar', attributeId);
  } else if (attributeId >= 62 && attributeId <= 65) { // Footrest - any option
    getCombinationPic('bp006', 'fcar', attributeId, 'fr');
  } else if (attributeId >= 70 && attributeId <= 73) { // Back Post Height - any option
    getCombinationPic('bp007', 'fcar', attributeId, 'frsb');
  }
}
// Combines the attendant or self propelled and cr or black rims
function getCombinationPic(designation, affix, attributeId, affixExtra) {
  if (attributeId >= 60) {
    if (document.getElementById("side_panel_colour_60"). checked == true) {
      affix += 'gp';
    } else {
      affix += 'bp';
    }
  }

  if (affixExtra) {
    affix += affixExtra;
  }

  if (document.getElementById("attSelect").checked == true) {
    openBuildPic(designation + 'at' + affix + '.jpg');
  } else if (document.getElementById("handrim_37").checked == true) {
    openBuildPic(designation + 'spcr' + affix + '.jpg');
  } else if (document.getElementById("handrim_38").checked == true) {
    openBuildPic(designation + 'spbr' + affix + '.jpg');
  }
}

function setAttendant(attendant) {
  retrieveMode = 'attendant';
	createRequester('ajax.php?attendant=' + attendant);
}

function getHelp(attributeId, optionId, e) {
  retrieveMode = 'getHelp';
  if (optionId) {
	  createRequester('help.php?option_id=' + optionId);
  } else {
	  createRequester('help.php?attribute_id=' + attributeId);
  }
  getMousePos(e);
}

function closeHelp() {
  document.getElementById('helpContainer').style.visibility = 'hidden'
}

function getMousePos(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	  document.getElementById('helpContainer').style.left = posx+250 + 'px';
	  document.getElementById('helpContainer').style.top = posy + 'px';
}

function saveTabId(tabId) {
  retrieveMode = 'tab';
	createRequester('ajax.php?tab_id=' + tabId);
}

function saveCustomSeatDepthVal(customSeatDepthVal) {
  retrieveMode = 'tab';
	createRequester('ajax.php?custom_seat_depth=' + customSeatDepthVal);
}

function saveCustomSeatHeightVal(customSeatHeightVal) {
  retrieveMode = 'tab';
	createRequester('ajax.php?custom_seat_height=' + customSeatHeightVal);
}

function saveCustomSeathDepthChecked(customSeatDepthChecked) {
  retrieveMode = 'tab';
	createRequester('ajax.php?custom_seat_depth_checked=' + customSeatDepthChecked);
}

function savedisplayArmrest(chckd) {
  retrieveMode = 'tab';
	createRequester('ajax.php?display_armrest_checked=' + chckd);
}

function saveCustomBackPostHeightVal(customBackPostHeightVal) {
  retrieveMode = 'tab';
	createRequester('ajax.php?custom_back_post_height=' + customBackPostHeightVal);
}