﻿// Global Variables
//-----------------------------------------------------------------------
var returntext; // used to retreive the text from the Ajax process
var dataentry; // used to store the text entered in the suggestion box
var usertype; // used to store the user type selection from combobox

// Display / Hide a Layer
//------------------------------------------------------------------------
function OpenBox(layer)
{
var style1 = document.getElementById(layer).style;
style1.display = (style1.display=="block")? "none":"block";
}

// Create XMLHttpRequest object
//------------------------------------------------------------------------
window.onload = function() {
    if (!window.XMLHttpRequest){
        window.XMLHttpRequest = function(){
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }    
}


// AJAX Save Function
//------------------------------------------------------------------------
function SaveSuggestion(){
    if(ValidateSuggestionInput()){
        if (!dataentry) {
            alert("No 'Suggestions' to save, Please enter a suggestion / comment");
            return;
        }
        var outdatatemp = "Suggestion=" + dataentry + "&UserType=" + usertype;
        
        CommenceAjax(outdatatemp) 
	} // End of ValidateSuggestionInput
}

	
function CommenceAjax(outdata){	
   var xr = new XMLHttpRequest();
   //Set Saving Div to open
   OpenBox("Saving");
    xr.open("GET", "WebService/WSAjaxWork.asmx/SaveSuggestion?" + outdata , true);
    xr.onreadystatechange = function() {
        if (xr.readyState == 4) {
            if (xr.status == 200) {
                     returntext = xr.responseText;
                    if(returntext){
                        alert(CreateDataString(returntext));
                        OpenBox("Saving");
                        document.getElementById("dataentry").value = "";
                        OpenBox('SugInnerBox');
                        
                     }
                    else {
                    alert("Error");    
                    }
        
            } // end if
        }// end if
    }// end onreadystatechange function
    xr.send(null);

}// end function



// Valiadate Input Data
//------------------------------------------------------------------------

function ValidateSuggestionInput(){
    //Validate SuggestionInput
    dataentry = document.getElementById("dataentry").value;
    if(!TestLength(dataentry,350,"Your Suggetion is")){return false;}
    //Validate combo box input
    usertype = document.getElementById("usertype").value;
    if(!TestLength(usertype,50,"User Type is")){return false;}
      
  return true;
}

//Test for length
function TestLength(strcontrol,len,strmessage){
if(strcontrol.length > len){
    alert(strmessage  + " to long. " + len + " characters allowed");
    return false;
   }
else{return true;}
}


// Translate data returned from Webservice
//------------------------------------------------------------------------
function CreateDataString(intext)
{                     
    if(intext){
    var intstart = intext.indexOf("^-^");
    var intend = intext.lastIndexOf("^-^");
    var datastring = intext.substring(intstart + 3,intend);
    return datastring
    //return DataString.split("|");
    }
    else{
        alert("No Data returned");
        return "";
    }    
}