// First, our functions for the GET process

var Access = new Array();
Access['A'] = 0;
Access['B'] = 0;

function showCommunity(community,e,myForm)
{
// force form submit if user clicks enter
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	//else return true;

	if (keycode == 13)
	{
	var formName = 'validateCommunity' + myForm;
	AjaxSend(document[formName], '/html/forms/validateCommunity.php', 'post', myForm); 
	return false;
	}
	
	if (community=="") 
	{ 
	var txtHint = 'txtHint' + myForm;
	document.getElementById(txtHint).innerHTML="";
	if (myForm == 'B') { document.getElementById(txtHint).style.display="none"; }
	return; 
	}

	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }

	xmlhttp.onreadystatechange=function() 
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{ 
		var txtHint = 'txtHint' + myForm;
		if (myForm == 'B' && community.match(/[A-Za-z]/) ) 
			{ document.getElementById(txtHint).style.display="block"; }
		document.getElementById(txtHint).innerHTML=xmlhttp.responseText; 
		} 
	}

	xmlhttp.open("GET","/html/forms/validateCommunityGet.php?q="+community+'&f='+myForm,true);
	xmlhttp.send('1');
}

function setMessage(myForm) 
{
	var formName = 'validateCommunity' + myForm;
	Access[myForm]++;
	if (Access[myForm] == 1) { document[formName].community.value = ''; }
}

function setCommunity(community,myForm) 
{
	var formName = 'validateCommunity' + myForm;
	document[formName].community.value = community; 
	AjaxSend(document[formName], '/html/forms/validateCommunity.php', 'post', myForm); 
	return false;
	//document.validateCommunity.submit(); 
}

// Now, our functions for the POST process

function CreateRequestObj () {
        // although IE supports the XMLHttpRequest object, but it does not work on local files.
    var forceActiveX = (window.ActiveXObject && location.protocol === "file:");
    if (window.XMLHttpRequest && !forceActiveX) {
        return new XMLHttpRequest();
    }
    else {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {}
    }
}

    // create HTTP request body form form data
function GetMessageBody (form) {
    var data = "";
    for (var i = 0; i < form.elements.length; i++) {
        var elem = form.elements[i];
        if (elem.name) {
            var nodeName = elem.nodeName.toLowerCase ();
            var type = elem.type ? elem.type.toLowerCase () : "";

                // if an input:checked or input:radio is not checked, skip it
            if (nodeName === "input" && (type === "checkbox" || type === "radio")) {
                if (!elem.checked) {
                    continue;
                }
            }

            var param = "";
                // select element is special, if no value is specified the text must be sent
            if (nodeName === "select") {
                for (var j = 0; j < elem.options.length; j++) {
                    var option = elem.options[j];
                    if (option.selected) {
                        var valueAttr = option.getAttributeNode ("value");
                        var value = (valueAttr && valueAttr.specified) ? option.value : option.text;
                        if (param != "") {
                            param += "&";
                        }
                        param += encodeURIComponent (elem.name) + "=" + encodeURIComponent (value);
                    }
                }
            }
            else {
                param = encodeURIComponent (elem.name) + "=" + encodeURIComponent (elem.value);
            }

            if (data != "") {
                data += "&";
            }
            data += param;                  
        }
    }
    return data;
}

    // returns whether the HTTP request was successful
function IsRequestSuccessful (httpRequest) {
        // IE: sometimes 1223 instead of 204
    var success = (httpRequest.status == 0 || 
        (httpRequest.status >= 200 && httpRequest.status < 300) || 
        httpRequest.status == 304 || httpRequest.status == 1223);
    
    return success;
}

// Now, the functions that control POST data coming in from the form

        function AjaxSend (form, url, method, myForm) {
                // get message data
            var data = GetMessageBody (form);

                // send the request
            var httpRequest = CreateRequestObj ();
                // try..catch is required if working offline
            try {
                httpRequest.open (method, url, false);  // synchron
                httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                httpRequest.send (data);
            }
            catch (e) {
                alert ("We encountered an error.");
                return;
            }

// Read the response returned from PHP
	if (IsRequestSuccessful (httpRequest)) 
	{
		    if (httpRequest.responseText.substr(0,2) == "1|") 
			{
			var Location = httpRequest.responseText.substr(2);
			location.href = Location;
			}
		else { 
		var txtHint = 'txtHint' + myForm;
		
		var formName = 'validateCommunity' + myForm;
	
		//if (myForm == 'B'  && document[formName].community.value.match(/[A-Za-z]/)) 
			//{ 
			document.getElementById(txtHint).style.display="block"; 
			//}
		document.getElementById(txtHint).innerHTML=httpRequest.responseText; }
	}
    else { alert ("An error occurred while processing. Please try again."); }
        }
