/**
 * Ajax.js
 *
 * Collection of Scripts to allow in page communication from browser to (struts) server
 * ie can reload part instead of full page
 *
 * How to use
 * ==========
 * 1) Call retrieveURL from the relevant event on the HTML page (e.g. onclick)
 * 2) Pass the url to contact (e.g. Struts Action) and the name of the HTML form to post
 * 3) When the server responds ...
 *		 - the script loops through the response , looking for <span id="name">newContent</span>
 * 		 - each <span> tag in the *existing* document will be replaced with newContent
 *
 * NOTE: <span id="name"> is case sensitive. Name *must* follow the first quote mark and end in a quote
 *		 Everything after the first '>' mark until </span> is considered content.
 *		 Empty Sections should be in the format <span id="name"></span>
 */

//global variables
var req;
var which;

var Utf8 = {
    
    // public method for url encoding
    encode : function (string) {
        if (string == undefined || string == 'undefined') return 'undefined'; //or ''
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        
        for (var n = 0; n < string.length; n++) {
            
            var c = string.charCodeAt(n);
            
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            
        }
        
        return utftext;
    },
    
    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        
        while ( i < utftext.length ) {
            
            c = utftext.charCodeAt(i);
            
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
            
        }
        
        return string;
    }
    
}

/**
 * Get the contents of the URL via an Ajax call
 * url - to get content from (e.g. /struts-ajax/sampleajax.do?ask=COMMAND_NAME_1) 
 * nodeToOverWrite - when callback is made
 * nameOfFormToPost - which form values will be posted up to the server as part 
 *					of the request (can be null)
 */
function retrieveURL(url,nameOfFormToPost) {
    
    //get the (form based) params to push up as part of the get request
    url=url+getFormAsString(nameOfFormToPost);
    
    //Do the Ajax call
    if (window.XMLHttpRequest) { // Non-IE browsers
        req = new XMLHttpRequest();
        req.onreadystatechange = processStateChange;
        try {
            req.open("GET", url, true); //was get
        } catch (e) {
            alert("Problem Communicating with Server\n"+e);
        }
        req.send(null);
    } else if (window.ActiveXObject) { // IE

        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processStateChange;
            req.open("GET", url, true);
            req.send();        
        }
    }
}

/*
 * Set as the callback method for when XmlHttpRequest State Changes 
 * used by retrieveUrl
 */
function processStateChange() {
    
    if (req.readyState == 4) { // Complete
        if (req.status == 200) { // OK response
            
            ///alert("Ajax response:"+req.responseText);
            
            //Split the text response into Span elements
            spanElements = splitTextIntoSpan(req.responseText);
            
            //Use these span elements to update the page
            replaceExistingWithNewHtml(spanElements);
            
        } else {
            alert("Problem with server response:\n " + req.statusText);
        }
    }
}

/**
 * gets the contents of the form as a URL encoded String
 * suitable for appending to a url
 * @param formName to encode
 * @return string with encoded form values , beings with &
 */ 
/*function getFormAsString(formName){

//Setup the return String
returnString ="";

//Get the form values
formElements=document.forms[formName].elements;

//loop through the array , building up the url
//in the form /strutsaction.do&name=value

for ( var i=formElements.length-1; i>=0; --i ){
//we escape (encode) each value
returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
}

//return the values
return returnString; 
}*/
/*
function getFormAsString_(formName){
    
    var radioHash = new Hash();
    var checkboxHash = new Hash();
    
    //Setup the return String
    returnString ="";
    
    //Get the form values
    formElements=document.forms[formName].elements;
    
    //loop through the array , building up the url
    //in the form /strutsaction.do&name=value
    
    for ( var i=formElements.length-1; i>=0; --i ){
        //we escape (encode) each value
        if (formElements[i].type == "radio") {
            if (formElements[i].checked == true) {
                radioHash.set(formElements[i].name,formElements[i].value);
            }
        } 
        else {
            if (formElements[i].type == "checkbox") {
                if (formElements[i].checked == true) {
                    checkboxHash.set(formElements[i].name,formElements[i].value);
                }
            }
            else returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
        }
    }

    radioHash.each(function(pair) {
        returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
    });

    checkboxHash.each(function(pair) {
        returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
    });
    
    return returnString;
}

 */

function getFormAsString(formName){
    var radioHash = new Hash();
    //var checkboxHash = new Hash();
    
    //Setup the return String
    returnString ="";
    
    //Get the form values
    formElements=document.forms[formName].elements;
    
    //loop through the array , building up the url
    //in the form /strutsaction.do&name=value
    if(navigator.userAgent.indexOf("Safari")!=-1){
        for ( var i=formElements.length-1; i>=0; --i ){
            //we escape (encode) each value
            if (formElements[i].type == "radio") {
                if (formElements[i].checked == true) {
                    radioHash.set(formElements[i].name,formElements[i].value);
                }
            } 
            else {
                if (formElements[i].type == "checkbox") {
                    if (formElements[i].checked == true) {
                        //checkboxHash.set(formElements[i].name,formElements[i].value);
                        returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                    }
                }
                //else returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                else if(formElements[i] != undefined && formElements[i].type != "file" && formElements[i].value != null && formElements[i].name != null &&formElements[i].name != "" 
                    && formElements[i].value != "" && formElements[i].name != undefined && formElements[i].style.display != "none")
                    returnString=returnString+"&"+formElements[i].name+"=" + formElements[i].value;
            }
        }

        radioHash.each(function(pair) {
            returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
        });

    /*checkboxHash.each(function(pair) {
                returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
            });*/
    /*
        for ( var i=formElements.length-1; i>=0; --i ){
            returnString=returnString+"&"+formElements[i].name+"=" + formElements[i].value;
        }*/
    } else if(navigator.userAgent.indexOf("Gecko")!=-1) {        
        for ( var i= 0; i < formElements.length; i++) {            
            //we escape (encode) each value
            if (formElements[i].type == "radio") {
                if (formElements[i].checked == true) {
                    radioHash.set(formElements[i].name,formElements[i].value);
                }
            } 
            else {                
                if (formElements[i].type == "checkbox") {
                    
                    if (formElements[i].checked == true) {
                        //checkboxHash.set(formElements[i].name,formElements[i].value);
                        returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                    }
                }
                //else returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                else if(formElements[i] != undefined && formElements[i].type != "file" && formElements[i].value != null && formElements[i].name != null &&formElements[i].name != "" 
                    && formElements[i].value != "" && formElements[i].name != undefined && formElements[i].style.display != "none")
                    returnString=returnString+"&"+formElements[i].name+"=" + formElements[i].value;
            }
        }

        radioHash.each(function(pair) {
            returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
        });

    /*checkboxHash.each(function(pair) {
                returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
            });*/
        
    /*
        for ( var i=formElements.length-1; i>=0; --i ){
            returnString=returnString+"&"+formElements[i].name+"=" + formElements[i].value;
        }*/
    }  
    else 
    {
        if(navigator.userAgent.indexOf("MSIE 7.0")!=-1) {
            for ( var i=formElements.length-1; i>=0; --i ){
                //we escape (encode) each value
                if (formElements[i].type == "radio") {
                    if (formElements[i].checked == true) {
                        radioHash.set(formElements[i].name,formElements[i].value);
                    }
                } 
                else {
                    if (formElements[i].type == "checkbox") {
                        if (formElements[i].checked == true) {
                            //checkboxHash.set(formElements[i].name,formElements[i].value);
                            returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                        }
                    }
                    //else returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                    else if(formElements[i] != undefined && formElements[i].type != "file" && formElements[i].value != null && formElements[i].name != null &&formElements[i].name != "" 
                        && formElements[i].value != "" && formElements[i].name != undefined && formElements[i].style.display != "none")
                        returnString=returnString+"&"+formElements[i].name+"="+escape(Utf8.encode(formElements[i].value));
                }
            }

            radioHash.each(function(pair) {
                returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
            });

        /*checkboxHash.each(function(pair) {
                returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
            });*/

        /*
            for ( var i=formElements.length-1; i>=0; --i ){
                returnString=returnString+"&"+formElements[i].name+"="+escape(Utf8.encode(formElements[i].value));
            }
             */
        } 
        else 
        {
            for (var i=formElements.length-1; i>=0; --i ){
                //we escape (encode) each value
                if (formElements[i].type == "radio") {
                    if (formElements[i].checked == true) {
                        radioHash.set(formElements[i].name,formElements[i].value);
                    }
                }
                else {
                    if (formElements[i].type == "checkbox") {
                        if (formElements[i].checked == true) {
                            //checkboxHash.set(formElements[i].name,formElements[i].value);
                            returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(Utf8.encode(formElements[i].value));
                        }
                    }
                    //else returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                    else if(formElements[i] != undefined && formElements[i].type != "file" && formElements[i].value != null && formElements[i].name != null &&formElements[i].name != ""
                        && formElements[i].value != "" && formElements[i].name != undefined && formElements[i].style.display != "none")
                        returnString=returnString+"&"+formElements[i].name+"="+escape(Utf8.encode(formElements[i].value));
                }
                
            }

            radioHash.each(function(pair) {
                returnString=returnString+"&"+escape(pair.key)+"="+escape(Utf8.encode(pair.value));
            });
                
        /*checkboxHash.each(function(pair) {
                    returnString=returnString+"&"+escape(pair.key)+"="+escape(Utf8.encode(pair.value));
                });*/
                        
        /*
                for ( var i=formElements.length-1; i>=0; --i ){
                    returnString=returnString+"&"+formElements[i].name+"="+escape(Utf8.encode(formElements[i].value));
                }*/
        }
    }

    //alert(returnString.length);
    //alert(returnString);
    //return the values

    return returnString; 

}

/**
 *  Modified by CongLT - 20081117
 *  Only map elements that style class name contain "ajax"
 *
 */
function getFormAsStringAjax(formName){
    var radioHash = new Hash();
    //var checkboxHash = new Hash();
    
    //Setup the return String
    returnString ="";
    
    //Get the form values
    formElements=document.forms[formName].elements;
    
    for ( var i=formElements.length-1; i>=0; --i )
    {
        //we escape (encode) each value
        if (formElements[i].type == "radio" && formElements[i].className.indexOf("ajax") != -1) 
        {
            if (formElements[i].checked == true) 
            {
                radioHash.set(formElements[i].name,formElements[i].value);
            }
        } 
        else 
        {
            if (formElements[i].type == "checkbox" && formElements[i].className.indexOf("ajax") != -1) 
            {
                if (formElements[i].checked == true) 
                {
                    if(navigator.userAgent.indexOf("Safari")!=-1 || navigator.userAgent.indexOf("Gecko")!=-1)
                    {
                        //checkboxHash.set(formElements[i].name,formElements[i].value);
                        returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                    }
                    else if(navigator.userAgent.indexOf("MSIE 7.0")!=-1)
                    {
                        returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
                    }
                    else
                    {
                        returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(Utf8.encode(formElements[i].value));
                    }
                }
            }
            //else returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
            else if(formElements[i] != undefined && formElements[i].type != "file" && formElements[i].value != null && formElements[i].name != null &&formElements[i].name != "" 
                && trimAll(formElements[i].value) != '' && formElements[i].name != undefined && formElements[i].style.display != "none"
                && formElements[i].className.indexOf("ajax") != -1)
                {
                if (formElements[i].className.indexOf("ajaxNoValue") != -1)
                {
                    returnString=returnString+"&"+formElements[i].name+"=";
                }
                else if (formElements[i].className.indexOf("ajaxNotEmptyValue") != -1)
                {
                    returnString=returnString+"&"+formElements[i].name+"=notEmpty";
                }
                else
                {
                    if(navigator.userAgent.indexOf("Safari")!=-1 || navigator.userAgent.indexOf("Gecko")!=-1)
                    {
                        returnString=returnString+"&"+formElements[i].name+"=" + formElements[i].value;
                    }
                    else if(navigator.userAgent.indexOf("MSIE 7.0")!=-1)
                    {
                        returnString=returnString+"&"+formElements[i].name+"="+escape(Utf8.encode(formElements[i].value));
                    }
                    else
                    {
                        returnString=returnString+"&"+formElements[i].name+"="+escape(Utf8.encode(formElements[i].value));                                                         
                    }                                
                }
            }
        }
    }

    radioHash.each(function(pair) 
    {
        if(navigator.userAgent.indexOf("Safari")!=-1 || navigator.userAgent.indexOf("Gecko")!=-1)
        {
            returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
        }
        else if(navigator.userAgent.indexOf("MSIE 7.0")!=-1)
        {
            returnString=returnString+"&"+escape(pair.key)+"="+escape(pair.value);
        }
        else
        {
            returnString=returnString+"&"+escape(pair.key)+"="+escape(Utf8.encode(pair.value));
        }
    });
            

    //alert(returnString.length);
    //alert(returnString);
    //return the values

    return returnString; 
}



/**
 * Splits the text into <span> elements
 * @param the text to be parsed
 * @return array of <span> elements - this array can contain nulls
 */
function splitTextIntoSpan(textToSplit){
    
    //Split the document
    returnElements=textToSplit.split("</span>")
    
    //Process each of the elements 	
    for ( var i=returnElements.length-1; i>=0; --i ){
        
        //Remove everything before the 1st span
        spanPos = returnElements[i].indexOf("<span");		
        
        //if we find a match , take out everything before the span
        if(spanPos>0){
            subString=returnElements[i].substring(spanPos);
            returnElements[i]=subString;
            
        } 
    }
    
    return returnElements;
}

/*
 * Replace html elements in the existing (ie viewable document)
 * with new elements (from the ajax requested document)
 * WHERE they have the same name AND are <span> elements
 * @param newTextElements (output of splitTextIntoSpan)
 *					in the format <span id=name>texttoupdate
 */
function replaceExistingWithNewHtml(newTextElements){
    
    //loop through newTextElements
    for ( var i=newTextElements.length-1; i>=0; --i ){
        
        //check that this begins with <span
        if(newTextElements[i].indexOf("<span")>-1){
            
            //get the name - between the 1st and 2nd quote mark
            startNamePos=newTextElements[i].indexOf('"')+1;
            endNamePos=newTextElements[i].indexOf('"',startNamePos);
            name=newTextElements[i].substring(startNamePos,endNamePos);
            
            //get the content - everything after the first > mark
            startContentPos=newTextElements[i].indexOf('>')+1;
            content=newTextElements[i].substring(startContentPos);
            
            //Now update the existing Document with this element
            
            //check that this element exists in the document
            if(document.getElementById(name)){
                
                //alert("Replacing Element:"+name);
                document.getElementById(name).innerHTML = content;
            } else {
        //alert("Element:"+name+"not found in existing document");
        }
        }
    }
}
