
//Global XMLHTTP Request object
var XmlHttp ;
var globalInvalid="false";

//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 ValidateLogin(uname,pword,pagename) 
{	
		
		//alert ("validate");
	
	if ( (uname=='')||(uname==' ')||(pword=='')||(pword==' ') ) {
		//document.getElementById("displayContactsLogin").innerHTML = '<font style="color:red">Please enter login info</font>';
		var dispCnt=document.getElementById("displayContactsLogin");if(dispCnt){document.getElementById("displayContactsLogin").style.display = "block";}
		return;
	}
	//alert(pagename);
	
	
	var AjaxServerPageName;
	if ( pagename == 'search')
	{
		AjaxServerPageName = "../AJAXServer.aspx";
	}
	else
	{
		AjaxServerPageName = "AJAXServer.aspx";
	}
	
	//var flagCheck = document.getElementById("LeftPanel11_chkRememberUidPwd")
	//var flagRemember
	//if (flagCheck.checked)
		flagRemember ="true"
	//else
		//flagRemember ="false"
	
	var requestUrl = AjaxServerPageName + "?uname=" + encodeURIComponent(uname) + "&pword="+encodeURIComponent(pword) +  "&rememberMe="+encodeURIComponent(flagRemember);
	XmlHttp = null;
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("POST", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}


//Called when response comes back from server
function HandleResponse()
{
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		//alert(XmlHttp.status);
		if(XmlHttp.status == 200)
		{	
			ClearAndSetContactsListItems(XmlHttp.responseXML.documentElement);
			XmlHttp = null;
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
			return;
		}
	}
}

// Himanshu code optimized
function ClearAndSetContactsListItems(AgencyContactsNode)
	{var dispCntLg=document.getElementById("displayContactsLogin");if(dispCntLg){document.getElementById("displayContactsLogin").style.display = "none";} 
	var contactsNodes = AgencyContactsNode.getElementsByTagName('Contact');
	var agencyContactId = AgencyContactsNode.getElementsByTagName('agencyContactId');
	var textValue; 
	var textValue1;
	var optionItem;
	if (contactsNodes.length > 0)
	{
	textValue = GetInnerText(contactsNodes[0]);
	if (textValue=='not found')
	{	
		globalInvalid ="true";
		var InvLg=document.getElementById("displayContactsInvalidLogin");if(InvLg){document.getElementById("displayContactsInvalidLogin").style.display = "block";}
		var dispCnt2=document.getElementById("displayContacts2");if(dispCnt2){document.getElementById("displayContacts2").style.display = "none";}	
		return;
	}
	globalInvalid ="false";
	document.getElementById("displayContacts2").style.display = "block";
	document.getElementById("DisplayRemMe").style.display = "block";
	document.getElementById("displayContactsInvalidLogin").style.display = "none";
	document.getElementById("DivNotRegister").style.display = "none";
	//document.getElementById("DivContactInfo").style.display = "none";	
    var contactsList = document.getElementById("LeftPanel11_AgencyContactList");
  	//Clears the state combo box contents.
  	for (var count = contactsList.options.length-1; count >-1; count--)
	{
		contactsList.options[count] = null;
	}
	
	
	//Add new states list to the state combo box.

	for (var count = 0; count < contactsNodes.length; count++)
	{
   		textValue = GetInnerText(contactsNodes[count]);
   		textValue1 = GetInnerText(agencyContactId[count]);
   		textValue = textValue.replace("-A-","&");
   		//alert (textValue);
		optionItem = new Option( textValue, textValue1,  false, false);
		//alert(optionItem)
		contactsList.options[contactsList.length] = optionItem;
	}
	var Lft11=document.getElementById("LeftPanel11_AgencyContactList");if(Lft11){document.getElementById("LeftPanel11_AgencyContactList").focus();}
	var divNew=document.getElementById("DivAddNewContact");if(divNew){document.getElementById("DivAddNewContact").style.display = "block";}
	}
	else
	{
		var InvLg=document.getElementById("displayContactsInvalidLogin");if(InvLg){document.getElementById("displayContactsInvalidLogin").style.display = "block";}
		var dispCnt2=document.getElementById("displayContacts2");if(dispCnt2){document.getElementById("displayContacts2").style.display = "none";}
	
	}
	
	
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function AgencyContactListOnChange(varKey) 
{	
	if (globalInvalid=="true")
	{
		globalInvalid="false";
		return false;
	}
	if (GlobalKey=='true')
	{
	flagRemember ="true"
	var contactList = document.getElementById("LeftPanel11_AgencyContactList");
	var agencyContactId = contactList.options[contactList.selectedIndex].value;
	
	var SContactName = contactList.options[contactList.selectedIndex].text;	
	if(SContactName.indexOf("'")>0)
	{		
	 SContactName = SContactName.replace("'", "`");	
	}
	//selectedContact = contactList.options[contactList.selectedIndex].text;
    selectedContact = SContactName;
        
    if (selectedContact == "ADD OR CHANGE CONTACT") 
	{
		window.open("AddEditContact.aspx" , '','width= 515px,height=400px,resizable=yes,scrollbars=1,status=no,location=no,toolbar=no,menubar=no');
		
		
	}
	else if (selectedContact != "-Select Contact-" && selectedContact!='' )
	{
   		var qs_value=findQueryString('BindRetURL');
		if (qs_value!="")
		{
			var BindRetURL;
			var curr_url=window.location.href;
			if (curr_url.indexOf("test.btisinc.com")>0)
				BindRetURL="https://test.btisinc.com/QSA/test.asp?subid=" + qs_value;
			else
				BindRetURL="https://asap.btisinc.com/QSA/test.asp?subid=" + qs_value;
			window.location.href=BindRetURL;
		}
		else
		{
   			var AjaxServerPageName;
			AjaxServerPageName = "View_Submissions.aspx";
			window.location.href = "View_Submissions.aspx" + "?SelectedContact=" + encodeURIComponent(selectedContact) + "&AgencyContactId="+encodeURIComponent(agencyContactId) +  "&rememberMe="+encodeURIComponent(flagRemember) ;
		}
	}
}
}

function StateListOnChange() 
{	
	var stateList = document.getElementById("LeftPanel11_ddlState1");
	selectedContact = stateList.options[stateList.selectedIndex].value
	if (selectedContact == 'SS') 
	{
		alert ('please select a state');
		return false;
	}
			
}

function findQueryString(name)
{
	name=name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

