// ---------------------
// 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
			if(results=="success"){
           	 xml_response_do()
			}
        
		 } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.responseText);
         }
    }
}


function xml_response_do(){
	// Hide form, show thank you!
	document.getElementById("form1").style.display = "none";
	document.getElementById("form2").style.display = "none";
	document.getElementById("form3").style.display = "block";	
}




// ----------------
// 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 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;
};