////// Constants \\\\\\
//  - Pipes (|) used to seperate array elements
var Seperator = "|";
//  - Tilde (~) used to seperate arrays
var ArraySeperator = "~";

////////////////////////////////////
//  loadBody()
//  - Runs every time the page loads
function loadBody(SelectStyleIndex)
{
	setStyleList();
}


///*********** PROPERTY SETTING FUNCTIONS ***********\\\

function resetInfo() {
	setCushionArrList();
	document.getElementById("selectCushions").selectedIndex = 0;
	setCushions();
	
	setColourList();
	document.getElementById("selectColour").selectedIndex = 0;
	setColour();
	
	setLegList();
	document.getElementById("selectLegs").selectedIndex = 0;
	setLegs();
	
	setInnerHTML("textMSRP", "");
	setInnerHTML("textCushions", "");
	setInnerHTML("textColour", "");
	setInnerHTML("textLegs", "");
	setInnerHTML("textFabricYards", "");
}

function resetFrame() {
	
	setCushionArrList();
	document.getElementById("selectCushions").selectedIndex = 0;	
	setCushions();	
	
	setLegList();
	document.getElementById("selectLegs").selectedIndex = 0;
	setLegs();
			
	setInnerHTML("textMSRP", "");
	setInnerHTML("textCushions", "");
	setInnerHTML("textColour", "");
	setInnerHTML("textLegs", "");
	setInnerHTML("textFabricYards", "");
		
}

function setDefaultFrame() {
	setCushionArrList();
	document.getElementById("selectCushions").selectedIndex = 1;	
	setCushions();	
}

////////////////////////////////////
//  setStyle()
//  - Calls PHP create(styleCode)
function setStyle()
{
	var selectStyleObj = document.getElementById("selectStyle");
	x_create(selectStyleObj.options[selectStyleObj.selectedIndex].value, create_cb);
		
	setLineImage();
	resetInfo();
	setFrameList();
	setFrame();
	
}

////////////////////////////////////
//  setFrame()
//  - Calls PHP setFrame(frameCode)
function setFrame()
{
	var selectFrameObj = document.getElementById("selectFrame");
	
	x_setFrame(selectFrameObj.options[selectFrameObj.selectedIndex].value, setFrame_cb);
	setLineImage();
	resetFrame();
	//setDefaultFrame();
}

////////////////////////////////////
//  setCushions()
//  - Calls PHP setCushions(CushionsCode)
function setCushions()
{
	var selectCushionsObj = document.getElementById("selectCushions");
	x_setCushionArr(selectCushionsObj.options[selectCushionsObj.selectedIndex].value,setCushionArr_cb);
}

////////////////////////////////////
//  setColour()
//  - Calls PHP setColour(Colour)
function setColour()
{
	var selectColourObj = document.getElementById("selectColour");
	x_setColour(selectColourObj.options[selectColourObj.selectedIndex].value, setColour_cb);
}

////////////////////////////////////
//  setLegs()
//  - Calls PHP setLegs(Legs)
function setLegs()
{
	var selectLegsObj = document.getElementById("selectLegs");
	x_setLegs(selectLegsObj.options[selectLegsObj.selectedIndex].value, setLegs_cb);
}

////////////////////////////////////
//  clearSession()
//  - Calls PHP create("-")
function clearSession()
{
	x_create("-", create_cb);
}

///*********** PRESENTATION FUNCTIONS ***********\\\



////////////////////////////////////
//  setProductImage()
//  - Calls PHP SetProductImageURL
function setProductImage(){ x_getProductImageURL(getProductImageURL_cb); }

////////////////////////////////////
//  getProductImage_cb(url)
//  - Receives product image url
//	- Calls JS setProductImageURL
function getProductImageURL_cb(result) {
	setProductImageURL(result);
}

////////////////////////////////////
//  setProductImageURL(url)
//  - Sets the product image div appropriately
function setProductImageURL(url) {
	setInnerHTML("imgProduct", ((url == "" || url == false || url == "images/" || url == "lines/.jpg")?'':'<img src=\"' + url + '\">'));
}

////////////////////////////////////
//  setLineImage()
//  - Calls PHP SetLineImageURL
function setLineImage(){ x_getLineImageURL(getLineImageURL_cb); }

////////////////////////////////////
//  getLineImage_cb(url)
//  - Receives Line image url
//	- Calls JS setLineImageURL
function getLineImageURL_cb(result) {
	setLineImageURL(result);
}

////////////////////////////////////
//  setLineImageURL(url)
//  - Sets the Line image div appropriately
function setLineImageURL(url) {
	setInnerHTML("imgLines", ((url == "" || url == false || url == "../lines/images/" || url == "lines/.jpg" || url == "lines/.gif")?'':'<img src=\"' + url + '\">'));
}


////////////////////////////////////
//  setLegImage()
//  - Calls PHP SetLegImageURL
function setLegImage(){ x_getLegImageURL(getLegImageURL_cb); }

////////////////////////////////////
//  getLegImage_cb(url)
//  - Receives Leg image url
//	- Calls JS setLegImageURL
function getLegImageURL_cb(result) {
	setLegImageURL(result);
}

////////////////////////////////////
//  setLegImageURL(url)
//  - Sets the Leg image div appropriately
function setLegImageURL(url) {
	setInnerHTML("imgLegs", ((url == "" || url == false || url == "images/" || url == "lines/.jpg")?'':'<img src=\"' + url + '\">'));
}

////////////////////////////////////
//  setTitles()
//  - Sets the product/model titles appropriately
function setTitles()
{
	var selectStyleObj = document.getElementById("selectStyle");
	var selectFrameObj = document.getElementById("selectFrame");
	var productTitle = "";
	var modelTitle = "";

	//Set Titles if....
	//No selectStyleObj Selected
	if(selectStyleObj.selectedIndex == 0){	
		setInnerHTML("textProductTitle",productTitle);
		setInnerHTML("textModel",modelTitle);
	}
	//Style, No Frame
	if(selectStyleObj.selectedIndex > 0){
		productTitle = selectStyleObj.options[selectStyleObj.selectedIndex].text;
		modelTitle   = selectStyleObj.options[selectStyleObj.selectedIndex].value;
		setInnerHTML("textProductTitle",productTitle);
		setInnerHTML("textModel",modelTitle);
	}
	
	//Style, Frame
	if(selectStyleObj.selectedIndex > 0 && selectFrameObj.selectedIndex > 0){
		productTitle = selectStyleObj.options[selectStyleObj.selectedIndex].text + 
						" " + selectFrameObj.options[selectFrameObj.selectedIndex].text;
		modelTitle = selectStyleObj.options[selectStyleObj.selectedIndex].value + 
						selectFrameObj.options[selectFrameObj.selectedIndex].value;
		setInnerHTML("textProductTitle",productTitle);
		setInnerHTML("textModel",modelTitle);
	}
}

////////////////////////////////////
//  setDetails()
//  - Calls JS setTitles
//	- Calls PHP getMSRP
//	- Calls PHP getFabricYards
//	- Calls JS/PHP getLegImage
function setDetails()
{
	//Call one PHP
	// Since only one PHP request can go out at
	// a time, the getFabricYards call is in getMSRP_cb
	// and getLegImage is in the Fabric yards cb
	x_getMSRP(getMSRP_cb);
	
	//Set titles
	setTitles();
}

////////////////////////////////////
//  getMSRP_cb(msrp)
//  - Sets the MSRP text appropriately
function getMSRP_cb(result)
{
	if(result != "" && result != false){
		setInnerHTML("textMSRP","$" + result);
	}
	//get the fabric yards too.. bad coding i know but it wouldn't be called otherwise
	x_getFabricYards(getFabricYards_cb);
}

////////////////////////////////////
//  getFabricYards_cb(fabyards)
//  - Sets the fabric yard text appropriately
function getFabricYards_cb(result)
{
	if(result != "" && result != false){
		setInnerHTML("textFabricYards", result + " yards");
	}
	
	//get the new leg image here.. bad coding i know but it wouldn't be called otherwise
	setLegImage();
}

////////////////////////////////////
//  setCushionArr_cb(url)
//  - Sets the cushion arrangement text appropriately
//	- Resets the product image
//  - Sets the line image
function setCushionArr_cb(result) {
	if(result != "" && result != false){
		var selectCushionsObj = document.getElementById("selectCushions");
		var cushtext          = selectCushionsObj.options[selectCushionsObj.selectedIndex].text;
		
		setInnerHTML("textCushions", cushtext);
		setProductImageURL(result);
		setLineImage();	//populate the line image
	}
	
}

////////////////////////////////////
//  setColour_cb(url)
//  - Sets the cushion arrangement text appropriately
//	- Resets the product image
function setColour_cb(result)
{
	if(result != "" && result != false){
		var selectColourObj = document.getElementById("selectColour");
		var colour = selectColourObj.options[selectColourObj.selectedIndex].text;
		setInnerHTML("textColour", colour);
		setProductImageURL(result);
	}
}

////////////////////////////////////
//  setLegs_cb(url)
//  - Sets the cushion arrangement text appropriately
//	- Resets the product image
function setLegs_cb(result)
{
	if(result != "" && result != false){
		var selectLegsObj = document.getElementById("selectLegs");
		var Legs = selectLegsObj.options[selectLegsObj.selectedIndex].value;
		if (Legs.indexOf("_") != -1){
			var temp = Legs.split("_");
			Legs = temp[0];
		}
		setInnerHTML("textLegs",Legs);
		setLegImageURL(result);
		setDetails();
	}
}



///*********** POPULATORS ***********\\\

////////////////////////////////////
//  create_cb(boolean)
//  - Receives Product class status
//  - Sets Initial Product/Model Title
//  - Calls JS setFrameList, JS setLegsList
function create_cb(result)
{
	if (result) {
		//Set Product/Model Titles
		setTitles();
		
		//Call update functions
		setFrameList(); 
		return;
	}
	
	// An error occured in the backend
	message = "An error occurred while trying to accomadate your selection\n" +
				"Please Try Again.";
	alert(message);
	return;
}

////////////////////////////////////
//  setFrame_cb(url)
//  - Receives Product Image url
//	- Calls JS setProductImageURL, setSeatList, setDetails
function setFrame_cb(result)
{
	//set the image
	if(result != "" && result != false){
		setProductImageURL(result)
		
		//clear out the frame, leg and cushion arr list
		selectRemoveAll(document.getElementById("selectCushions"));
		selectRemoveAll(document.getElementById("selectLegs"));
		setCushionArrList();
		setColourList();
		setLegList();
	
	}
	
	//call other js functions
	setDetails();
}

////////////////////////////////////
//  setCushionArrList()
//  - Calls PHP getCushionArrList
function setCushionArrList() {
	x_getCushionArrList(getCushionArrList_cb);
}

////////////////////////////////////
//  setStyleList()
//  - Calls PHP getStyleList
function setStyleList() {
	x_getStyleLists(getStyleLists_cb);
}

////////////////////////////////////
//  setFrameList()
//  - Calls PHP getFrameList
function setFrameList() {
	x_getFrameLists(getFrameLists_cb);
}
			
////////////////////////////////////
//  setLegList()
//  - Calls PHP getLegList
function setLegList() {
	x_getLegLists(getLegLists_cb);
}

////////////////////////////////////
//  setColourList()
//  - Calls PHP getColourList
function setColourList() {
	x_getColourList(getColourList_cb);
}

////////////////////////////////////
//  getCushionArrList_cb()
//  - Recieves one array in form of string
//	- Sets Cushions select
function getCushionArrList_cb(result)
{
	if(result != "" && result != false){
		var array = result.split(Seperator);
		
		if(array[0] == "Not Applicable" || array[1] == "Not Applicable")
			setInnerHTML("textCushions", "Not Applicable");
		
		//clear & repop the select 
		var selectCushionsObj = document.getElementById("selectCushions");
		selectRemoveAll(selectCushionsObj);
		selectAddFromArrays(selectCushionsObj,array,array);
		
		//refresh the image
		setProductImage();
	}
}

////////////////////////////////////
//  getStyleList_cb()
//  - Recieves 2 arrays in form of string
//	- Sets Styles select to values of arrays
function getStyleLists_cb(result)
{
	if(result != "" && result != false){
		var arrays = result.split(ArraySeperator);
		var selectStyleObj = document.getElementById("selectStyle");
		var StyleCodeListString;
		var StyleNameListString;
		var StyleCodeList;
		var StyleNameList;

		//split the arrays
		if(arrays.length > 0 && arrays.length <= 2){
			StyleCodeListString = arrays[0];
			StyleNameListString = arrays[1];
			StyleCodeList = StyleCodeListString.split(Seperator);
			StyleNameList = StyleNameListString.split(Seperator);
		} else { alert("An error occurred while populating the styles list\nPlease Try Again."); }
		
		//clear & repop the select
		selectRemoveAll(selectStyleObj);
		selectAddFromArrays(selectStyleObj,StyleNameList,StyleCodeList);
		
		//refresh the image
		setProductImage();
	}
}

////////////////////////////////////
//  getColourList_cb()
//  - Recieves 1 array in form of a string
//	- Sets Styles select to values of array
function getColourList_cb(result)
{
	if(result != "" && result != false){
		var array = result.split(Seperator);
		var selectColourObj = document.getElementById("selectColour");
		
		//clear & repop the select
		selectRemoveAll(selectColourObj);
		selectAddFromArrays(selectColourObj,array,array);
		
		//refresh the image
		setProductImage();
	}
}

////////////////////////////////////
//  getFrameList_cb()
//  - Recieves 2 arrays in form of string
//	- Sets Frames select to values of arrays
function getFrameLists_cb(result)
{
	if(result != "" && result != false){
		var arrays = result.split(ArraySeperator);
		var selectFrameObj = document.getElementById("selectFrame");
		var FrameCodeListString;
		var FrameNameListString;
		var FrameCodeList;
		var FrameNameList;
		
		//split the arrays
		if(arrays.length > 0 && arrays.length <= 2){
			FrameCodeListString = arrays[0];
			FrameNameListString = arrays[1];
			FrameCodeList = FrameCodeListString.split(Seperator);
			FrameNameList = FrameNameListString.split(Seperator);
		} else { alert("An error occurred while populating the frames box\nPlease Try Again."); }
		
		//clear & repop the select
		selectRemoveAll(selectFrameObj);
		selectAddFromArrays(selectFrameObj,FrameNameList,FrameCodeList);
		
		//refresh the image
		setProductImage();
	}
}

////////////////////////////////////
//  getLegList_cb()
//  - Recieves 2 arrays in form of string
//	- Sets Legs select to values of arrays
function getLegLists_cb(result)
{
	
	var selectLegsObj = document.getElementById("selectLegs");
	if(result != "" && result != false && result != ArraySeperator){
		var arrays = result.split(ArraySeperator);
		var LegCodeListString;
		var LegNameListString;
		var LegCodeList;
		var LegNameList;

		//split the arrays
		if(arrays.length > 0 && arrays.length <= 2){
			LegCodeListString = arrays[0];
			LegNameListString = arrays[1];
			LegCodeList = LegCodeListString.split(Seperator);
			LegNameList = LegNameListString.split(Seperator);
		} else { alert("An error occurred while populating the legs list\nPlease Try Again."); }
		
		//clear & repop the select
		selectRemoveAll(selectLegsObj);
		selectAddFromArrays(selectLegsObj,LegNameList,LegCodeList);
		
		//refresh the image
		setLegImage();
	}
	else {
		selectRemoveAll(selectLegsObj);
		LegNameList = new Array('Select Legs');
		LegCodeList = new Array('-');
		selectAddFromArrays(selectLegsObj, LegNameList, LegCodeList);
	}
}



///*********** GLOBAL FUNCTIONS ***********\\\

////////////////////////////////////
//  setInnerHTML(elementid,content)
//  - Sets the innerHtml property for desired element
//  - Cross-browser safe back to NS4
function setInnerHTML(elementid,content)
{
	document.getElementById(elementid).innerHTML = content;
}

////////////////////////////////////
//  selectRemoveAll(select)
//  - Removes all options from a select box
function selectRemoveAll(SelectObj)
{
	if (typeof SelectObj != "undefined") {
	
		if(SelectObj.length > 0) {
			for(i=SelectObj.length-1;i>=0;i--){
				SelectObj.options[i] = null;
			}
		}
	}
}

////////////////////////////////////
//  selectAddFromArrays(SelectObj,arrayNames,arrayValues)
//  - Adds options to the end of the SelectObj object using two arrays
function selectAddFromArrays(SelectObj,arrayNames,arrayValues)
{
		
	if (typeof SelectObj != "undefined" && arrayNames.length == arrayValues.length) {
		for(var i = 0; i < arrayNames.length; i++){
			var selectLength = SelectObj.options.length;
			var newOption = new Option(arrayNames[i],arrayValues[i]);
			SelectObj.options[selectLength] = newOption;
		}
	}
}