// JavaScript Document
//======================================================================================//
// Help System																			//
//--------------------------------------------------------------------------------------//
function register(id,dest) {
	//request = getHTTPObject();
	var unique = id;
	var dest = dest;
	var JSONObject = new Object;
	JSONObject.unique = unique;
	JSONObject.dest = dest;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/register.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = dest;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"center\"><BR><img src=\"/img/ajax-loader.gif\"><BR>Processing<BR></div>";	
	}

function loadImages(id) {
	//request = getHTTPObject();
	var imageId = id;
	var JSONObject = new Object;
	JSONObject.imageId = imageId;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/loadImages.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	}

function selectImage(id,dest,rug) {
	//request = getHTTPObject();
	var imageId = id;
	var dest = dest;
	var rug = rug;
	var JSONObject = new Object;
	JSONObject.imageId = imageId;
	JSONObject.dest = dest;
	JSONObject.rug = rug;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/swapImage.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');
	}


function changeImage(im,dest) {
	//request = getHTTPObject();
	var image = im;
	var dest = dest;
	var JSONObject = new Object;
	JSONObject.image = image;
	JSONObject.dest = dest;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/changeImage.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	}
	

function dataReturn(data) {
	// if request object received response
	var dataObject = JSON.parse(data);
	var quantity = dataObject.qty;
	
	for (i=0; i<quantity; i++){ // Loops through all the data from the dataObject.
		// Using bracket notation to dynamically create the dataObject property names.	
		var dest =  dataObject['dest_' + i];
		var content = dataObject['content_' + i];
	
		var objID = dest;					// sets the id of the div opbject to get the return
		var obj = document.getElementById(objID);
		document.getElementById(objID).innerHTML = content;
	}	
}




function updateMaster(value,dest,tbl,col,id) {
	var updateValue = value;
	var updateDest = dest;
	var updateTbl = tbl;
	var updateCol = col;
	var updateId = id;
	
	var JSONObject = new Object;
	JSONObject.updateValue = updateValue;
	JSONObject.updateDest = updateDest;
	JSONObject.updateTbl = updateTbl;
	JSONObject.updateCol = updateCol;
	JSONObject.updateId = updateId;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/upd.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = dest;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"left\" class=\"loader\"><img src=\"/img/ajax-loader.gif\"></div>";	
	}

function updateFREE(value,tbl,col) {
	var updateValue = value;
	var updateTbl = tbl;
	var updateCol = col;
	
	var JSONObject = new Object;
	JSONObject.updateValue = updateValue;
	JSONObject.updateTbl = updateTbl;
	JSONObject.updateCol = updateCol;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/free.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	}

function updateAssociate(value,client,host,next) {
	var updateValue = value;
	var updateDest = client;
	var updateHost = host;
	var updateClient = client;
	var updateNext = next;
	var dest = "rug_"+client;
	
	var JSONObject = new Object;
	JSONObject.updateValue = updateValue;
	JSONObject.updateDest = updateDest;
	JSONObject.updateHost = updateHost;
	JSONObject.updateClient = updateClient;
	JSONObject.updateNext = updateNext;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/ass.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = dest;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"left\" class=\"loader\"><img src=\"/img/ajax-loader.gif\"></div>";	
	}







function deleteMaster(dest,tbl,id) {
	var updateDest = dest;
	var updateTbl = tbl;
	var updateId = id;
	
	var JSONObject = new Object;
	JSONObject.updateDest = updateDest;
	JSONObject.updateTbl = updateTbl;
	JSONObject.updateId = updateId;
	JSONstring = JSON.stringify(JSONObject);
	rstring = 'del.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = dest;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"left\" class=\"loader\"><img src=\"/img/ajax-loader.gif\"></div>";	
	}

function updateRadio(value,tbl,col,id) {
	var updateValue = value;
	var updateTbl = tbl;
	var updateCol = col;
	var updateId = id;
	
	var JSONObject = new Object;
		JSONObject.updateValue = updateValue;
		JSONObject.updateTbl = updateTbl;
		JSONObject.updateCol = updateCol;
		JSONObject.updateId = updateId;
		JSONstring = JSON.stringify(JSONObject);
		rstring = 'rad.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');
	}
	
function setDelivery(value) {
	var updateValue = value;
	var dest = "addressBook";
	
	var JSONObject = new Object;
		JSONObject.updateValue = updateValue;
		JSONstring = JSON.stringify(JSONObject);
		rstring = 'setDelivery.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');
		
		var objID = dest;					// sets the id of the div opbject to get the return
		var obj = document.getElementById(objID);
		obj.innerHTML = "<div align=\"center\" class=\"loader\"><img src=\"/img/ajax-loader-new.gif\"></div>";	
	}
function setDelivery1(value) {
	var updateValue = value;
	var dest = "addressBook";
	
	var JSONObject = new Object;
		JSONObject.updateValue = updateValue;
		JSONstring = JSON.stringify(JSONObject);
		rstring = 'setDelivery1.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');
		
		var objID = dest;					// sets the id of the div opbject to get the return
		var obj = document.getElementById(objID);
		obj.innerHTML = "<div align=\"center\" class=\"loader\"><img src=\"/img/ajax-loader-new.gif\"></div>";	
	}


function updateCheckbox(value,tbl,col,id,newval,dest) {
	var updateValue = value;
	var updateTbl = tbl;
	var updateCol = col;
	var updateId = id;
	var newValue = newval;
	var dest = dest;
	
	var JSONObject = new Object;
	JSONObject.updateValue = updateValue;
	JSONObject.updateTbl = updateTbl;
	JSONObject.updateCol = updateCol;
	JSONObject.updateId = updateId;
	JSONObject.newValue = newValue;
	JSONObject.dest = dest;
	
	JSONstring = JSON.stringify(JSONObject);
	rstring = 'ckb.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	}

function updateCheckboxAddress(value,tbl,col,id,newval) {
	var updateValue = value;
	var updateTbl = tbl;
	var updateCol = col;
	var updateId = id;
	var newValue = newval;
	
	var JSONObject = new Object;
	JSONObject.updateValue = updateValue;
	JSONObject.updateTbl = updateTbl;
	JSONObject.updateCol = updateCol;
	JSONObject.updateId = updateId;
	JSONObject.newValue = newValue;
	
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/ckb_address.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	}
function updateRadioAddress(value,tbl,col,id,newval,uid) {
	var updateValue = value;
	var updateTbl = tbl;
	var updateCol = col;
	var updateId = id;
	var newValue = newval;
	var uid = uid;
	
	var JSONObject = new Object;
	JSONObject.updateValue = updateValue;
	JSONObject.updateTbl = updateTbl;
	JSONObject.updateCol = updateCol;
	JSONObject.updateId = updateId;
	JSONObject.newValue = newValue;
	JSONObject.uid = uid;
	
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/rad_address.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	}







function updateMasterCombo(id) {
	//request = getHTTPObject();
	var value = id;
	var JSONObject = new Object;
	JSONObject.value = value;
	JSONObject.dest = 'masterDiv';
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/master.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = 'masterDiv';					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader.gif\"></div>";	
	}

function updateMain(id,masterGroup) {
	//request = getHTTPObject();
	var value = id;
	var masterGroup = masterGroup;
	var JSONObject = new Object;
	JSONObject.value = value;
	JSONObject.masterGroup = masterGroup;
	JSONObject.dest = 'mainDiv';
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/main.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = 'mainDiv';					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader.gif\"></div>";	
	}

function optionChange(id,dest) {
	//request = getHTTPObject();
	var option = id;
	var dest = dest;
	var JSONObject = new Object;
	JSONObject.option = option;
	JSONObject.dest = dest;
	JSONstring = JSON.stringify(JSONObject);
	rstring = '/2010/admin/optionChange.php?json='+JSONstring;
	jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var objID = dest;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader.gif\"></div>";	
	}

function updateRow(qty,rid,o1,o2,o3,rug) {
	//request = getHTTPObject();
	var quantity = qty;
	var rowId = rid;
	var optionOne = o1;
	var optionTwo = o2;
	var optionThree = o3;
	var rugId = rug;
	
	var JSONObject = new Object;
		JSONObject.quantity = quantity;
		JSONObject.rowId = rowId;
		JSONObject.optionOne = optionOne;
		JSONObject.optionTwo = optionTwo;
		JSONObject.optionThree = optionThree;
		JSONObject.rugId = rugId;
		JSONstring = JSON.stringify(JSONObject);
		rstring = '/2010/productUpdate.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');

	var dest_1 = rowId+"_totalPrice";
	var dest_2 = rowId+"_delivery";


	var objID = dest_1;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader-new.gif\" width=\"20\" height=\"20\"></div>";	

	var objID = dest_2;					// sets the id of the div opbject to get the return
	var obj = document.getElementById(objID);
	obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader-new.gif\" width=\"20\" height=\"20\"></div>";	
	
	}

function addCart(row,itemID,status) {
	//request = getHTTPObject();
	var row = row;
	var itemID = itemID;
	var status = status;
	if (row > -1) {
		var optionsQuantityBuild = row+'_quantity_select';
		var optionsQuantity = document.getElementById(optionsQuantityBuild).value;
		if (optionsQuantity == 0) {
			alert("You must select a quantity to add to your shopping Cart");
			return;
			}
		} else {
		var optionsQuantity	= 1;
		}

	var JSONObject = new Object;
		JSONObject.row = row;
		JSONObject.itemID = itemID;
		JSONObject.status = status;
		JSONObject.optionsQuantity = optionsQuantity;
		JSONstring = JSON.stringify(JSONObject);
		rstring = '/2010/addCart.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');

	return;
	}
	
function alternate(id) {
	//request = getHTTPObject();
	var id = id;
	var dest="addressSelected";
	var JSONObject = new Object;
		JSONObject.id = id;
		JSONstring = JSON.stringify(JSONObject);
		rstring = '/2010/alternate.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');

		var objID = dest;					// sets the id of the div opbject to get the return
		var obj = document.getElementById(objID);
		obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader-new.gif\" width=\"20\" height=\"20\"></div>";	
	}
function alternateTest(id) {
	//request = getHTTPObject();
	var id = id;
	var dest="addressSelected";
	var JSONObject = new Object;
		JSONObject.id = id;
		JSONstring = JSON.stringify(JSONObject);
		rstring = '/2010/alternateTest.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');

		var objID = dest;					// sets the id of the div opbject to get the return
		var obj = document.getElementById(objID);
		obj.innerHTML = "<div align=\"center\"><img src=\"/img/ajax-loader-new.gif\" width=\"20\" height=\"20\"></div>";	
	}
	
function showCart(id,dq,row,q) {
	var id = id;
	var dq = dq;
	var row = row;
	var qty = q;
		
		
	var JSONObject = new Object;
		JSONObject.id = id;
		JSONObject.dq = dq;
		if (dq == 2) {
			JSONObject.qty = qty;
			}
		JSONstring = JSON.stringify(JSONObject);
		rstring = '/2010/showCart.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');
	return;
	}


function updateOption(val,pc,row,status) {
	var val = val;
	var pc = pc;
	var status = status;
	var pid = document.getElementById('pid').value;
	
	var JSONObject = new Object;
		JSONObject.val = val;
		JSONObject.pc = pc;
		JSONObject.row = row;
		JSONObject.pid = pid;
		JSONObject.status = status;
		
	var optionOneBuild = row+'_option_1_option';
	var optionOne = document.getElementById(optionOneBuild).value;
		JSONObject.optionOne = optionOne;
		
	var optionOneBuildMaster = row+'_master_1';
	var optionOneMaster = document.getElementById(optionOneBuildMaster).value;
		JSONObject.optionOneMaster = optionOneMaster;


	var optionsAvailableBuild = row+'_options';
	var optionsAvailable = document.getElementById(optionsAvailableBuild).value;
		JSONObject.optionsAvailable = optionsAvailable;
		
	if (pc != 4) {
		var optionsQuantityBuild = row+'_quantity_select';
		var optionsQuantity = document.getElementById(optionsQuantityBuild).value;
		} else {
		var optionsQuantity = val;
		}
		JSONObject.optionsQuantity = optionsQuantity;
	
	if (optionsAvailable > 1) {
		if (pc != 2) {
			var optionTwoBuild = row+'_option_2_select';
			var optionTwo = document.getElementById(optionTwoBuild).value;
			} else {
			var optionTwo = val;
			}
		if ((pc == 4) && (optionTwo == 0) && (optionsAvailable == 2)) {
			alert("You must select a product option before a quantity");
			return;
			}
		
		
		var optionTwoBuildMaster = row+'_master_2';
		var optionTwoMaster = document.getElementById(optionTwoBuildMaster).value;
			JSONObject.optionTwoMaster = optionTwoMaster;
			
			
		} else {
		var optionTwo = 0;	
		}
		JSONObject.optionTwo = optionTwo;
		
		
		
	if (optionsAvailable > 2) {
		if (pc != 3) {
			var optionThreeBuild = row+'_option_3_select';
			var optionThree = document.getElementById(optionThreeBuild).value;
			} else {
			var optionThree = val;
			}
		if ((pc == 4) && ((optionTwo == 0) || (optionThree == 0))) {
			alert("You must select both product options before a quantity");
			return;
			}
		var optionThreeBuildMaster = row+'_master_3';
		var optionThreeMaster = document.getElementById(optionThreeBuildMaster).value;
			JSONObject.optionThreeMaster = optionThreeMaster;



		} else {
		var optionThree = 0;	
		}
		JSONObject.optionThree = optionThree;

	
		JSONstring = JSON.stringify(JSONObject);
		rstring = '/2010/changeOption.php?json='+JSONstring;
		jx.load(rstring,function(data){dataReturn(data);},'text','post');

	return;
	}

