﻿//javaScript
var XmlReq;
var AjaxServerPageName = "../AjaxServer.aspx";
var Lbl;
var object;
var SelectedItem;
var TableName;
var checkAlert;
var CheckStatus;   


function chkAvailability(Table,obj1,obj2,Label)
//function chkAvailability(Table,obj,objvalue,Label)
{

    object=obj1;
	Lbl=Label;
	TableName=Table;
	var requestUrl = AjaxServerPageName + "?Choice1=" + encodeURIComponent(obj1.value)+"&Choice2=" + encodeURIComponent(obj2)+"&Table=" + encodeURIComponent(Table) + "&rankey=" + Math.floor ( Math.random ( ) * 1000 + 1 );
	
		CreateXmlReq();
	//alert(XmlReq);
if(XmlReq)
	{
	
		XmlReq.onreadystatechange = HandleResponse;
		XmlReq.open("GET", requestUrl,  true);
		XmlReq.send();	
			
	}
   
	return false;
}

function HandleResponse()
{
//alert(XmlReq.readyState);
//4 means connection has been established successfully and ready to use
	if(XmlReq.readyState == 4)
	{
	
	//200 means successful reterive the data
		if(XmlReq.status == 200)
		{			
		
			// Before filling new contents into the DataGrid, clear the cotents by calling this function
			//ClearTable();
			// Fill the cleared Datagrid with new XML Reponse
			GetandCheckResult(XmlReq.responseXML.documentElement);
			
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function CreateXmlReq()
{
	try
	{
		XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlReq = null;
		}
	}
	if(!XmlReq && typeof XMLHttpRequest != "undefined") 
	{
	    
		XmlReq = new XMLHttpRequest();
	}
}


//Clears the contents of state combo box and adds the states of currently selected country
function GetandCheckResult(ResultNode)
{
 // alert(CheckStatus);
	var ResultNodes = ResultNode.getElementsByTagName('Available');
	checkAlert = true;
	
	 
	for (var count = 0; count < ResultNodes.length; count++)
	{
	    if(ResultNodes[0].text=='1')
	    {
	       checkAlert = false;
	        Lbl.innerHTML=TableName+" ID already exists. Please try again with another "+TableName +  " ID.";
	        object.focus();
	        CheckStatus='N';
	      
	    }
	    else if(ResultNodes[0].text=='2')
	    {
	       checkAlert = false;
	       Lbl.innerHTML=TableName+" ID already exists. Please try again with another "+TableName +  " ID.";
	       //Lbl.innerHTML=TableName+" ID is in cancel status. Please try again with another "+TableName + " ID.";
	       object.focus();
	       CheckStatus='N';  
	    }
	    else if(ResultNodes[0].text=='3')
	    {
	        checkAlert = false;
	        Lbl.innerHTML=TableName+" ID already exists. Please try again with another "+TableName +  " ID.";
	        //Lbl.innerHTML=TableName+" ID is in disconnect status. Please try again with another "+TableName + " ID.";
	        object.focus();
	        CheckStatus='N';
	    }
	    else if(ResultNodes[0].text=='4')
	    {
	        checkAlert = false;
	        Lbl.innerHTML=TableName+" alias already exists. Please try again with another alias.";
	        //Lbl.innerHTML=TableName+" ID is in disconnect status. Please try again with another "+TableName + " ID.";
	        object.focus();
	        CheckStatus='N';
	    }
	    else if(ResultNodes[0].text=='99')
	    {
	        checkAlert = true; 
	        Lbl.innerHTML="";
	        //object.focus();
	        CheckStatus='Y';
	    }
	    else
	    {	       
	        Lbl.innerHTML="";
	        CheckStatus='Y';
	        
	    }
	  
	}
	
   
}
function pagerefresh()
{
    alert('inside pagerefresh');
    window.opener.location.reload();

}