/*

	Customized by Parveen Vashishth
     General AJAX technique --how to talk to the server + return data and do stuff with it.
	 this AJAX is synchronous AJAX request instead of asynchronous, because we need to check for the duplicate in the name and till we havto wait. If it will be asynchronous then it will not wait.
	 For synchronous AJAX request  we set the sync flag to false. 
	
 
*/


BASE_URL = "http://192.168.9.133:8080/inhouse7/captivate/leadflow";
var duplicateflag = false;
var alertMsg  = "";
var isIEbrowser = true ;
var _ms_XMLHttpRequest_ActiveX = "";

/**
This is a callback method for synchronous AJAX request, which  works only in IE.
becuase in MOzilla and others for synchronous AJAX request readyState property does not work
*/
function executeCheckDuplicateReturn( AJAXCheckDuplicate ) {



    if (AJAXCheckDuplicate.readyState == 4) {
        if (AJAXCheckDuplicate.status == 200) {
			if ( AJAXCheckDuplicate.responseText ) {  		  
				// eval(AJAXCheckDuplicate.responseText );
				var response_text=AJAXCheckDuplicate.responseText;
				if(response_text.indexOf('<input type=hidden name="pageflagfromRequest"') != -1 ){
					response_text="returnAccessCode();"
					eval(response_text);
				}else{
				 eval(AJAXCheckDuplicate.responseText);
				}
		  }
	}
    }
}


function AJAXCheckDuplicateRequest( method, url, data, process, async, dosend) {
   var self = this;

    // check the dom to see if this is IE or not
    if (window.XMLHttpRequest) {
	// Not IE
    	if(!document.all)	  isIEbrowser = false;
        self.AJAXCheckDuplicate = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
	// Hello IE!
	 isIEbrowser = true;
        // Instantiate the latest MS ActiveX Objects
        if (_ms_XMLHttpRequest_ActiveX) {
            self.AJAXCheckDuplicate = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
        } else {
	    // loops through the various versions of XMLHTTP to ensure we're using the latest
	    var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];

            for (var i = 0; i < versions.length ; i++) {
                try {
		    // try to create the object
		    // if it doesn't work, we'll try again
		    // if it does work, we'll save a reference to the proper one to speed up future instantiations
                    self.AJAXCheckDuplicate = new ActiveXObject(versions[i]);

                    if (self.AJAXCheckDuplicate) {
                        _ms_XMLHttpRequest_ActiveX = versions[i];
                        break;
                    }
                }
                catch (objException) {
                // trap; try next one
                } ;
            }

            ;
        }
    }
    

	 /**
setting the callback method for synchronous AJAX request, which  works only in IE.
becuase in MOzilla and others for synchronous AJAX request readyState property does not work
*/
	 if(isIEbrowser){
		
	// if no callback process is specified, then assing a default which executes the code returned by the server
    if (typeof process == 'undefined' || process == null) {
        process = executeCheckDuplicateReturn;
    }

    self.process = process;

    // create an anonymous function to log state changes
    self.AJAXCheckDuplicate.onreadystatechange = function( ) {
        
		
        self.process(self.AJAXCheckDuplicate);
    }

	 }

  

    

    // if no method specified, then default to POST
    if (!method) {
        method = "POST";
    }

    method = method.toUpperCase();

    async = false;
   
   

    self.AJAXCheckDuplicate.open(method, url, async);

    if (method == "POST") {
        self.AJAXCheckDuplicate.setRequestHeader("Connection", "close");
        self.AJAXCheckDuplicate.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        self.AJAXCheckDuplicate.setRequestHeader("Method", "POST " + url + "HTTP/1.1");
    }

    // if dosend is true or undefined, send the request
    // only fails is dosend is false
    // you'd do this to set special request headers
    if ( dosend || typeof dosend == 'undefined' ) {
	    if ( !data ) data=""; 
	    self.AJAXCheckDuplicate.send(data);
    }
     

	  /**
 In MOzilla and others for synchronous AJAX request readyState property does not work
 We need to directly get the AJAXCheckDuplicate.responseText directly after making the  request*/
	if(!isIEbrowser){
		 
	/*var  response_text_now =   self.AJAXCheckDuplicate.responseText;
					
				response_text_now1 = 	response_text_now.replace(/flash2/, "flash3");
									
									 eval(response_text_now1);
	 */
	var  response_text_now =   self.AJAXCheckDuplicate.responseText;
	 if(navigator.userAgent.indexOf('MSIE')>0){
		 response_text_now1 = 	response_text_now
	 }else{
		 response_text_now1 = 	response_text_now.replace(/flash2/, "flash3");
	 }
	if(response_text_now1.indexOf('<input type=hidden name="pageflagfromRequest"') != -1 ){
		response_text_now1="returnAccessCode();"
		eval(response_text_now1);
	} else{
		eval(response_text_now1);
	 }

}
}


function receiveWeburlFromFlash() 
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL + "/flashLeadFlow.jsp?activity=setWebUrl", "");
}

function checkAccessCode(accessCode) 
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL+"/flashLeadFlow.jsp?activity=validate", "accessCode=" + accessCode);
}

function returnLeadID(leadID)
{
	if ( leadID == "wrongPassword" )
		document.getElementById('wrongMsg').style.display='';
	else
		location.href='main.html?correctID=' + leadID;
}



function receiveWeburlFromFlash() 
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL + "/flashLeadFlow.jsp?activity=setWebUrl", "");
}

function checkAccessCode(accessCode) 
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL+"/flashLeadFlow.jsp?activity=validate", "accessCode=" + accessCode);
}

function returnLeadID(leadID)
{
	if ( leadID == "wrongPassword" )
		document.getElementById('wrongMsg').style.display='';
	else
		location.href='main.html?correctID=' + leadID;
}

function goToGenericHandler( dataset, key, target,selected,event,comments) 
{
	new AJAXCheckDuplicateRequest("POST", key+"/control/genericHandler", "dataset=" + dataset + "&key=" + key + "&target=" + target + "&selected=" + selected+"&comments="+comments[0]);
}

function returnAccessCode()
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL + "/flashLeadFlow.jsp?activity=returnAccessCode","dataset=returnAccessCode");
}

function recieveLeadIdFromFlash(fname,lname,companyName,email,phone,comments)
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL + "/addLead.jsp", "fname="+fname+"&lname="+lname+"&email="+email+"&phone="+phone+"&companyName="+companyName+"&comments="+comments);
}

function leadEnteredinFrameajax(pageID,leadID)
{
	new AJAXCheckDuplicateRequest("POST", BASE_URL + "/flashLeadFlow.jsp?activity=timings", "pageID=" + pageID+"&leadID=" + leadID );
}

