<!--

// ***************** AJAX stuff *************************

// creates a request object with which the data can be
// "tunneled" through
function create_request(){
	var req;					// the request object that will be created
	// if using a regular complaince standard browser
	if (window.XMLHttpRequest){
   	    req = new XMLHttpRequest();
   	}// if using Internet Explorer
	else if (window.ActiveXObject) {
   	    req = new ActiveXObject("Microsoft.XMLHTTP");
   	}
	if (!req) {
    	alert('Cannot create an XMLHTTP instance');
        return false;
    }else{
		return req;
	}
}
 /* the base ajax calling function
  * usually you want to set up a function that calls this one
  * and supplies it with the parameters for a specific <div>tag you need to work with
  * curDiv = the id of the div tag to put HTML into
  * url = the dynamic page waiting for post data, that will return the HTML
  * callback = the function that will be called when data is received by the browser
  * sendStr = a string of name=value pairs seperated by &s, which get sent to the url page
  * show = whether you want to change a hidden div to a visible one
  */
function base_ajax(tagID, url, sendStr, callback){
	var req = create_request();
	if(req){
		//alert("thanks for using ajax");
		req.open("POST", url, true);
		/* when the data has been completely loaded back into the browser
		  * and the status is OK, it puts the text string that was returned
		  * into the tag specified in curDiv, and shows the div (if specified)
		  */
		req.onreadystatechange = function(){
			//alert(req.readyState + " - " + req.status);
			if (req.readyState == 4 && req.status == 200) {
				var elem = document.getElementById(tagID);
				//alert(req.responseText);
				//alert("elem looking for " + tagID);
				if(elem){
					elem.innerHTML = req.responseText;
				}
				if(callback){
					eval(callback);
				}
			}	
		}
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(sendStr);
	}
}

function add_ajax(tagID, url, sendStr){
	var req = create_request();
	if(req){
		//alert("thanks for using ajax");
		req.open("POST", url, true);
		/* when the data has been completely loaded back into the browser
		  * and the status is OK, it puts the text string that was returned
		  * into the tag specified in curDiv, and shows the div (if specified)
		  */
		req.onreadystatechange = function(){
			//alert(req.readyState + " - " + req.status);
			if (req.readyState == 4 && req.status == 200) {
				var elem = document.getElementById(tagID);
				//alert(req.responseText);
				//alert("elem looking for " + tagID);
				if(req.responseText == "#SAVED#"){
					refresh_window();
				}else{
					elem.innerHTML = req.responseText;
				}
			}	
		}
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(sendStr);
	}
}

function find_ajax(tagID, url, sendStr){
	var req = create_request();
	if(req){
		//alert("thanks for using ajax");
		req.open("POST", url, true);
		/* when the data has been completely loaded back into the browser
		  * and the status is OK, it puts the text string that was returned
		  * into the tag specified in curDiv, and shows the div (if specified)
		  */
		req.onreadystatechange = function(){
			//alert(req.readyState + " - " + req.status);
			if (req.readyState == 4 && req.status == 200) {
				var elem = document.getElementById(tagID);
				//alert(req.responseText);
				//alert("elem looking for " + tagID);
				rText = req.responseText;
				if(rText.substring(0,1) == "#"){					
					window.location = rText.substring(1);
				}
				else{
					elem.innerHTML = req.responseText;
				}
			}	
		}
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(sendStr);
	}
}

function login_ajax(tagID, url, sendStr){
	var req = create_request();
	if(req){
		//alert("thanks for using ajax");
		req.open("POST", url, true);
		/* when the data has been completely loaded back into the browser
		  * and the status is OK, it puts the text string that was returned
		  * into the tag specified in curDiv, and shows the div (if specified)
		  */
		req.onreadystatechange = function(){
			//alert(req.readyState + " - " + req.status);
			if (req.readyState == 4 && req.status == 200) {
				var elem = document.getElementById(tagID);
				//alert(req.responseText);
				//alert("elem looking for " + tagID);
				rText = req.responseText;
				if(rText.substring(0,1) == "#"){
					set_login(rText.substring(1));
					window.location = urlPath + "/admin/";
				}
				else{
					elem.innerHTML = req.responseText;
				}
			}	
		}
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(sendStr);
	}
}


var defaultAjaxPage = "http://www.kindelfurniture.com/admin/includes/admin_ajax.php";

function save_order_ajax(listItems, table, primaryKey, secondaryKey){
	base_ajax("message", defaultAjaxPage, "mode=orderList&table=" + table + "&secondaryKey=" + secondaryKey + "&primaryKey=" + primaryKey + "&listItems=" + listItems, "");
}

function admin_refresh_ajax(sendStr){	
	base_ajax("message", defaultAjaxPage, sendStr, "refresh_window();");
}

function admin_save_ajax(sendStr){
	base_ajax("message", defaultAjaxPage, sendStr, "document.forms[0].submit();");
}

function show_region(region){
	regionDiv = (region == "Global" ? "globalResults" : "regionResults");
	base_ajax(regionDiv, "/partners/Partner.aspx", "mode=regionlist&region=" + region, "");
}

function add_child(parentID){
	document.getElementById("childWarning").innerHTML = "Looking up article...";
	childInput = document.forms["addChild"].childNumber.value;
	add_ajax("childWarning", defaultAjaxPage, "mode=addchild&parentID=" + parentID + "&childNumber=" + childInput);
}

function admin_go_to(){
	article = document.forms["articleGoTo"].article.value;
	find_ajax("goToWarning", defaultAjaxPage, "mode=gotoarticle&articleNumber=" + article);
}

function admin_login(un, pw){
	login_ajax("message", defaultAjaxPage, "mode=lj8x4&ks834h=" + un + "&sk5sn1=" + hex_md5(pw));
}

function admin_login2(un, pw){
	alert(hex_md5(pw));
	login_ajax("message", defaultAjaxPage, "mode=test&un=" + un + "&pw=" + hex_md5(pw));
}

function add_article_to_collection_group(collectionGroupID, articleID){
	document.getElementById("collectionGroupWarning").innerHTML = "Trying to add to collection group...";
	childInput = document.forms["addChild"].childNumber.value;
	add_ajax("collectionGroupWarning", defaultAjaxPage, "mode=addarticletocollectiongroup&collectionGroupID=" + collectionGroupID + "&articleID=" + articleID);
}

function save_group_photo_order(collectionID){
	var photos = get_list_order("groupPhotoList", "pid");
	base_ajax("", defaultAjaxPage, "mode=&collectionID= " + collectionID + "&photoIDs=" + photos.join(","), "");
}

function save_group_photo_order(collectionID){
	document.getElementById("groupPhotoMessage").innerHTML = "";
	var photos = get_list_order("groupPhotoList", "pid");
	base_ajax("groupPhotoMessage", defaultAjaxPage, "mode=orderGroupPhotos&collectionID= " + collectionID + "&photoIDs=" + photos.join(","), "");
}

function save_detail_photo_order(articleID){
	document.getElementById("detailPhotoMessage").innerHTML = "";
	var photos = get_list_order("detailPhotoList", "pid")
	base_ajax("detailPhotoMessage", defaultAjaxPage, "mode=orderDetailPhotos&articleID= " + articleID + "&photoIDs=" + photos.join(","), "");
}

//-->