// ---------------------
// AJAX HTTP REQUEST
// ---------------------
// global flags
var isIE = false;
var cont_div_is = "";	//Global Content Div to Fill..
var editor_save = false;
var editor_loc = "";
var xmlaction = "";

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}


function loadXMLDocPost(url,content) {
    // branch for native XMLHttpRequest object
	//alert(url+"\n"+content);
	
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(content);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
			req.open("POST", url, true);
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.send(content);
        }
    }
}

// handle onreadystatechange event of req object
//var numb = 0;
function processReqChange() {

	//document.getElementById(cont_div_is).innerHTML = "Loading...";	// Loading graphic..
	//numb = 0;
	//loading_action();
	
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			// What to do on successful return of data
			results = req.responseText
            xml_response_do(results)
        
		 } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.responseText);
         }
    }
}


function xml_response_do(results){
	
	// Set page divs
	if(xmlaction == "set_page_div" || xmlaction == "update_map_data"){
		alert(results);
		//if(results=="Page Successfuly Moved."){
			//window.location.href = "index.asp?p="+editor_loc;
		//}
	}
	
	// Response on successful page update
	if(xmlaction == "page_save_update"){
		//alert(results);
		editor_save = false;
		window.location.href = "index.asp?p="+editor_loc+"&aid="+editor_loc;
	} 
	
	// New Page Creation
	if(xmlaction == "new_page_create"){
		//alert(results);
		//if(results=="Page Successfuly Created."){
		if(results){
			alert("Page Successfuly Created.");
			window.location.href = "index.asp?r="+results+"&p="+editor_loc;
		}
	}
	
	// Set Page Status
	if(xmlaction == "set_page_status"){
		//alert(results);
		if(results){
			alert("Page Status Successfuly Saved.");
			window.location.href = "index.asp?r="+results+"&p="+editor_loc;
		}
	}
	
	// Set Page Visible
	if(xmlaction == "set_page_visible"){
		//alert(results);
		if(results){
			alert("Page Visibility Successfuly Saved.");
			window.location.href = "index.asp?r="+results+"&p="+editor_loc;
		}
	}
	
	// Delete Page
	if(xmlaction == "delete_page"){
		alert(results);
		if(results=="Page Successfuly Deleted."){
			window.location.href = "index.asp";
		}
	}
	
	// Page Renamed
	if(xmlaction == "rename_page"){
		//alert(results);
		if(results){
			alert("Page Successfuly Renamed.");
			window.location.href = "index.asp?r="+results+"&p="+editor_loc;
		}
	}
	
	// Page Moved
	if(xmlaction == "move_page"){
		alert(results);
		if(results=="Page Successfuly Moved."){
			window.location.href = "index.asp?p="+editor_loc;
		}
	}
	
	// Cats Updated/etc...
	if(xmlaction == "manage_cat"){
		alert(results);
		//if(results=="Page Successfuly Moved."){
			window.location.href = "index.asp?p="+editor_loc;
		//}
	}
	
	// Successful add page note
	
	if(xmlaction == "add_page_note"){
		load_wcp('t3','wcp_page_notes.asp');
	}
	
	
	// Successful Video Add
	if(xmlaction == "add_video" || xmlaction == "del_video" || xmlaction == "move_video"){
		load_wcp('t2','wcp_media.asp');
	}
	
	// General div content replacement
	if (xmlaction == "load_div"){
		document.getElementById(cont_div_is).innerHTML = results;
	}
	
	// Public Website Actions
	
	// Membership Log from homepage
	if(xmlaction == "publicmemberlog"){
		if(results == "Thank you for logging in!"){
			alert(results);
		}else{
			alert(results);
			window.location.href = "index.asp?p=Members";
		}
	}
	
}




// ----------------
// Specific Content Functions
// ----------------



// Post and SAVE Web Page Changes
/*function save_page(pageid,pagecont){
	
	xmlaction = "page_save_update";
	editor_save = true;
	editor_loc = pageid;
	
	// Clean up problem characters in pagecont
	var amp = /&/g;
	var singQuot = /\'/g;
	var dubQuot = /\"/g;
	pagecont = pagecont.replace(amp,"{AMP}");
	pagecont = pagecont.replace(singQuot,"{SQ}");
	pagecont = pagecont.replace(dubQuot,"{DQ}");
	
	// Global load div is...
	cont_div_is = "mid_col_cont";
	
	if(pageid){
		// Load the Content
		var urlis = "http://test.jpking.com/ajax/save_page_edit.asp?pid="+pageid+"&pcont="+pagecont;
		loadXMLDoc(''+urlis+'');
	}
}*/

function save_page(pageid,aid,pagecont){
	
	xmlaction = "page_save_update";
	editor_save = true;
	editor_loc = pageid;
	
	// Clean up problem characters in pagecont
	var amp = /&/g;
	var singQuot = /\'/g;
	var dubQuot = /\"/g;
	pagecont = pagecont.replace(amp,"{AMP}");
	pagecont = pagecont.replace(singQuot,"{SQ}");
	pagecont = pagecont.replace(dubQuot,"{DQ}");
	
	// Global load div is...
	cont_div_is = "mid_col_cont";
	
	if(pageid){
		// Load the Content
		var urlis = "http://www.jpking.com/jpk30/ajax/save_page_edit.asp";
		var contentis = "pid="+URLEncode(pageid)+"&aid="+URLEncode(aid)+"&pcont="+URLEncode(pagecont);
		loadXMLDocPost(urlis,contentis);
	}
}


function URLEncode(valueis)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = valueis;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};