//fAJAXRequest
//
// client function for use in scripts
//
// parameters:
//
// sTargetID		= HTML ID of target DIV in which to dump returned data
// sURL				= URL to send request to
// sPost			= POST data to send, if "" then GET request is made (NOTE: GET may cache, this is a feature, not a bug!)
// sWait			= Text to display in target DIV whilst request is being made, "" = dont change current content
// sError			= Text to display in target DIV if error occurs, "" = dont change current content
//
function fAJAXRequest( sTargetID , sURL , sPost , sWait , sError )
{
	var e = document.getElementById( sTargetID );

	if( e )
	{
		if( sWait != "" )
			e.innerHTML = sWait;

		new oAJAXRequest( e , sURL , sPost , sError );
	}
}


// THE FOLLOWING ARE THE BACK END FUNCTIONS


// AJAX object state and response tracker function
//
// my server-scripts will always send back the first line as "200\n" if succesfull
//
// parameters:
//
// hAJAXRequest		= handle to request object
//
function fAJAXStateChange( hAJAXRequest )
{
	if( hAJAXRequest && hAJAXRequest.mRequest && hAJAXRequest.mRequest.readyState == 4 )
	{
		var s = hAJAXRequest.mRequest.responseText;
		//DEBUG: alert( s );

		if( hAJAXRequest.mRequest.status == "200" && s.substr(0,3) == "200" )
		{
			hAJAXRequest.mhTarget.innerHTML = s.substring(4);
		}
		else
		if( hAJAXRequest.msError != "" )
		{
			hAJAXRequest.mhTarget.innerHTML = hAJAXRequest.msError;
		}
	}
}

// AJAX object
//
// parameters:
//
// hTarget			= handle to target div (or whatever) we want to dump the returned HTML in
// sURL				= url to make request to
// sPost			= POST form data to send (leave as "" to make GET request)
// sError			= HTML to put in target div if request failed
//
// members:
//
// mRequest			= browser's own request object
// mhTarget			= hTarget
// msError			= sError
//
function oAJAXRequest( hTarget , sURL , sPost , sError )
{
	var me = this;
	this.mRequest	= null;
	this.mhTarget	= hTarget;
	this.msError	= sError;


	var txtRatingTitle = document.getElementById("txtRatingTitle");
	var txtRatingNick = document.getElementById("txtRatingNick");
	var txtRatingLocation = document.getElementById("txtRatingLocation");
	var txtRatingComments = document.getElementById("txtRatingComments");
	var txtStarRating = document.getElementById("hdnStarRating");
	var hdnRatingURL = document.getElementById("hdnRatingURL");
    var ErrorMsg = "";

    if (txtRatingNick.value == "" || txtRatingNick.value == " ") {
        ErrorMsg = ErrorMsg + "\n Nickname is required";
     }

    if (txtRatingLocation.value == "" || txtRatingLocation.value == " ") {
        ErrorMsg = ErrorMsg + "\n Location is required";
    }

    if (txtRatingTitle.value == "" || txtRatingTitle.value == " ") {
        ErrorMsg = ErrorMsg + "\n Subject is required";
    }

    if (txtRatingComments.value == "" || txtRatingComments.value == " ") {
        ErrorMsg = ErrorMsg + "\n Comments are required";
    }

    if (txtStarRating.value == "" || txtStarRating.value == " ") {
        ErrorMsg = ErrorMsg + "\n Rating is required";
    }

    // If all the required fields are filled
    if (ErrorMsg == "") {

        var btnSubmit = document.getElementById("btnSubmit");
        btnSubmit.value = "Wait...";
        btnSubmit.disabled = true;

        //do NOT use try{}catch as it is not supported in very old browsers and the script will not compile
        if (window.XMLHttpRequest)	//FF,NS,OP,IE7
        {
            this.mRequest = new XMLHttpRequest();
        }
        else
            if (window.ActiveXObject)	//IE5 & 6
            {
                this.mRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }

            if (this.mRequest) 
            {

                var strReviewInformation = "";
                strReviewInformation = "&rating=" + CheckStringForQ(txtStarRating.value) + "&ttl=" + CheckStringForQ(txtRatingTitle.value) + "&nk=" + CheckStringForQ(txtRatingNick.value) + "&loc=" + CheckStringForQ(txtRatingLocation.value) + "&comm=" + CheckStringForQ(txtRatingComments.value);
                sURL = sURL + strReviewInformation;

                if (sPost != "") {
                    this.mRequest.open('POST', sURL, true);
                    this.mRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                    this.mRequest.onreadystatechange = function () { fAJAXStateChange(me); };
                    this.mRequest.send(sPost);
                }
                else {
                    this.mRequest.open('GET', sURL, true);
                    this.mRequest.onreadystatechange = function () { fAJAXStateChange(me); };
                    this.mRequest.send(null);
                }
            }
    }
    else {
        // Show error message to the user
        alert(ErrorMsg);
    }
}

function querySt(sURL, sQueryString) {
hu = sURL;
gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == sQueryString) {
        return ft[1];
        }
    }
}

function StoreRating(rating)
{
    var hdnRating = document.getElementById("hdnStarRating");
    var liStarRating = document.getElementById("liStarRating");

    hdnRating.value = rating;
    liStarRating.style.width = (rating * 20) + 'px';
}

function GetSelectedRating() {
    var hdnRating = document.getElementById("hdnStarRating");
    return hdnRating.value;
}

function DisplayReviewBlock() {
    var ReviewBlock = document.getElementById("ReviewBlock");
    var stars = document.getElementById("stars");
    ReviewBlock.style.display = "block";
    stars.style.display = "none";
}

function CheckStringForQ(theString) {
    var strString;
    strString = theString;
	if (strString != "")
    {
        strString = ReplaceAll(theString, "&#39;", "'");
        strString = ReplaceAll(strString, "&#34;", "\"");        
    }    
	return strString;
}

function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;

}

function trimWhiteSpace(field) {
    field.value = trim(field.value);
}

function trim(s) {
    var l = 0; var r = s.length - 1;
    while (l < s.length && s[l] == ' ')
    { l++; }
    while (r > l && s[r] == ' ')
    { r -= 1; }
    return s.substring(l, r + 1);
}
