//Global XMLHTTP Request object
var XmlHttp;
var AjaxCreditorPageName;
AjaxCreditorPageName = "/readcomments.aspx";
var AjaxAddCommentPage;
AjaxAddCommentPage = "/AddComments.aspx";
 
var QUERY_PAGE_ID;
var QUERY_DOMAIN_ID;
QUERY_PAGE_ID = "pid";
QUERY_DOMAIN_ID = "did";

var COMMENT_DIV_ID;
COMMENT_DIV_ID = "divcommentservice";

var DIV_ERROR_ID;
DIV_ERROR_ID = "errordiv";


var ERROR_MESSAGE;
ERROR_MESSAGE = "Error Accessing the Comments Service. Please try again Later."

var MENU_1;
var MENU_2;
var MENU_3;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//Gets called when country combo box selection changes
function PopulateComments(pid,  did)
{
    
    if (pid <=0 )
        pid =  getPageID();
    
    if (!pid || pid <= 0 )
	{
		DisplayErrorMessage(ERROR_MESSAGE);
		return;
	}
	
	if (did <= 0)
	{
		DisplayErrorMessage(ERROR_MESSAGE);
		return;
	}
	
	
	StartProgressDisplay();
	// URL to get states for a given country
	var requestUrl = AjaxCreditorPageName + "?" + QUERY_PAGE_ID  +"=" + encodeURIComponent(pid) + "&" + QUERY_DOMAIN_ID + "=" + encodeURIComponent(did);
        //alert(requestUrl);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleCommentResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}


//Called when response comes back from server
function HandleCommentResponse()
{
	
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		EndProgressDisplay();
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
			document.getElementById(COMMENT_DIV_ID).innerHTML = XmlHttp.responseText;
			//select_innerHTML('ddlcreditor', XmlHttp.responseText);
			//SetDataEntryVisible(true);
		}
		else
		{
			DisplayErrorMessage(ERROR_MESSAGE);
		}
	}
}

function StartProgressDisplay()
{
 
 var btn = document.getElementById("btnSearchPayeee")
 if (btn != null )
 {
    btn.disabled = true;
 }
}

function EndProgressDisplay()
{
    var btn = document.getElementById("btnSearchPayeee")
	if (btn != null )
	{
		btn.disabled = false;
	}
}

function DisplayErrorMessage(errormessage)
{
	var div = document.getElementById(DIV_ERROR_ID);
	if (div != null)
		div.innerHTML  = "<span style='FONT-SIZE: 14px; COLOR: red; FONT-VARIANT: normal'>"+ errormessage +"</span>";
		
	//SetDataEntryVisible(false);
}

//Function to parse the querystring argument
function getQueryStringArgs() {
            var args = new Object();
            var query = location.search.substring(1);
            var pairs = query.split("&");
            for(var i = 0; i < pairs.length; i++) 
            {
                var pos = pairs[i].indexOf('=');
                if (pos == -1) continue;
                var argname = pairs[i].substring(0,pos);
                var value = pairs[i].substring(pos+1);
                args[argname] = unescape(value);
            }
    return args;
}

function getPageID()
{
    var args = getQueryStringArgs();
   
    if (args.m) 
    {
        MENU_1 = parseInt(args.m); 
    }
    if (args.mm)
    {
        MENU_2 = parseInt(args.mm);
    }
    if (args.mmm)
    {
      MENU_3 = parseInt(args.mmm);
    }
    
    if (MENU_3 > 0 )
        return MENU_3;
        
    if (MENU_2 > 0)
        return MENU_2;
        
    if (MENU_1 != "0" && MENU_1 != "-1")
        return MENU_1;
        
    
    
    return -1;
}

//Gets called when country combo box selection changes
function AddComment()
{
    var param = ReadUserInput();
    //alert(param);
    if (!param || param.length <=0 )
	{
		DisplayErrorMessage("Invalid Comments! Please fill all the comment including Name, Email and Comment Description");
		return;
	}

	StartProgressDisplay();
	// URL to get states for a given country
	var requestUrl = AjaxAddCommentPage;
	CreateXmlHttp();
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("POST", requestUrl,  true);
		XmlHttp.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
		XmlHttp.setRequestHeader('Content-length',param.length);
		XmlHttp.setRequestHeader('Connection',"close");
		
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleAddCommentResponse;
		
		//Sends the request to server
		XmlHttp.send(param);		
	}
}

//Called when response comes back from server
function HandleAddCommentResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		EndProgressDisplay();
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{	
		    DisplayErrorMessage( XmlHttp.responseText);
			//select_innerHTML('ddlcreditor', XmlHttp.responseText);
			//SetDataEntryVisible(true);
		}
		else
		{
			DisplayErrorMessage(ERROR_MESSAGE);
		}
	}
}

function ReadUserInput()
{
    
   var params = "author=" + escape(encodeURI(document.getElementById("txtauthor").value)) + 
                 "&email=" + escape(encodeURI(document.getElementById("txtemail").value)) +
                 "&desc=" + escape(encodeURI(document.getElementById("txtdesc").value)) +
                 "&pid=" + escape(encodeURI(getPageID()));
                 
    return params;                 
}