var thankYouWin = null;
var loginWin = null;

Ext.QuickTips.init();

/*** Get center position of Browser ***/

	var winw = "400"; //############# How wide should your window be (pixels)
	var winh = "300"; //############# How tall should the window be (pixels)
	var ie=(document.all);
	var ns=(document.layers);
	var ns6=(document.getElementById && !ie);

	function centerElement(){
	  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	}



/*** Popup for Login form ***/

      var loginWin;
	  loginWin = new Ext.Window({
	  	    el:'login-win',
		    layout:'fit',
		    width:300,
		    height: 250,
		    closeAction:'hide',
		    plain: true,

		    items: new Ext.TabPanel({
		    	el: 'login-tabs',
		    	autoTabs:true,
		    	activeTab:0,
		    	deferredRender:false,
		    	border:false
		   }),

    	   buttons: [{
			    text: 'Login',
			    handler: function(){
			    	submitLoginForm();
			    }
		   },{
			    text: 'Cancel',
			    handler: function(){
			    	loginWin.hide();
			    }
		   }]
	});
/*
 * Login Form Popup.
 */

    function showLoginForm() {
        try {
           document.getElementById("sessLogMsg").innerHTML = "";
        } catch(e) {}
        try {
           document.brokerForm.pwd.value = "";
        } catch(e) {}
	if (ie){
		documentWidth = (centerElement().offsetWidth)/2+centerElement().scrollLeft-(winw/2);
		documentHeight = (centerElement().offsetHeight)/2+centerElement().scrollTop-(winh/2);
	}
	else if (ns){
		documentWidth=window.innerWidth/2+window.pageXOffset-(winw/2);
		documentHeight=window.innerHeight/2+window.pageYOffset-(winh/2);
	}
	else if (ns6){
		documentWidth=self.innerWidth/2+window.pageXOffset-(winw/2);
		documentHeight=self.innerHeight/2+window.pageYOffset-(winh/2);
	}     

        try {
            loginWin.setPosition( documentWidth,documentHeight );
        } catch(e) {}
        try {
            loginWin.show();
        } catch(e) {}
    }

/*
 * Validate Login Form.
 */

    function submitLoginForm() {
        var valForm = false;
        var email = "";
        valForm = validateLoginForm();
        if(valForm) {
            try {
                email = document.modPostForm.userName.value;
            } catch(e) {}
            setCookieBorrower(email);
            try {
                document.getElementById("sessLogMsg").innerHTML = "Please Wait... <img src=\"images/large-loading.gif\">";
            } catch(e) {}
            setTimeout("submitBorrowerForm()","100");
        }
        
    }

/*
 * Check and Submit Login Form.
 */
    function submitBorrowerForm() {

         var xmlDoc = "";
         var email = "";
         var pwd = "";
         var status = 0;
         var msg = "";
         var uType = "";
         try {
             email = document.modPostForm.userName.value;
         } catch(e) {}
         try {
             pwd = document.modPostForm.pwd.value;
         } catch(e) {}
         var url = "checkBorrowerLogin.php";
         var qstr = "email="+email+"&pwd="+pwd;
         try {
             xmlDoc = getXMLDoc(url,qstr);
         } catch(e) {}
         try {
             status = xmlDoc.getElementsByTagName("status")[0].firstChild.nodeValue;
         } catch (e) {}
         try {
             msg = xmlDoc.getElementsByTagName("msg")[0].firstChild.nodeValue;
         } catch (e) {}
         if(status == 1) {
             document.modPostForm.submit();
         } else {
             try {
                 document.getElementById("sessLogMsg").innerHTML = msg;
             } catch(e) {}
         }
    }

/*
 * Go to Pre-Qualify Form when user select any lender.
 */    
    function goToRegPage() {
        var cnt = 0, len = 0;
        var chk = false, j=0;
        try {
            eval("len = document.productForm.lender.length");
        } catch(e) {}
        for(var i=0;i< len;i++) {
            var chk = false;
            try {
                 eval("chk = document.productForm.lender["+i+"].selected");
            } catch(e) {}
            if(chk) {
                cnt++;
            }
        }
        if(cnt > 0) {
            window.location.href='modpostReg.html';
        }      
    
    }

/*
 * To Set URL in session.
 */    
    function updateSessMsg(sessQstr) {
        var xmlDoc = "";
        var email = "";
        var uType = "br";
        var msg = "";
        var url = "updateSessionVariables.php";
        var qstr = "qstr="+sessQstr;
        try {
            xmlDoc = getXMLDoc(url,qstr);
        } catch(e) {}
        try {
            status = xmlDoc.getElementsByTagName("msg")[0].firstChild.nodeValue;
        } catch (e) {}
        showLoginForm();
    }    
/*
 * To Include the Script OnClick.
 */    
    function include(file) {     
        var script  = document.createElement('script');  
        script.src  = file;  
        script.language = 'JavaScript';  
        script.type = 'text/javascript';  
        document.getElementsByTagName('head').item(0).appendChild(script);  
    }  

    function includeScript(fileName) {
         var scriptTag  = document.createElement('script');  
         scriptTag.src  = fileName;  
         scriptTag.id  = "accountID";  
         scriptTag.type = 'text/javascript';  
         scriptTag.language = 'JavaScript';  
         document.getElementsByTagName('head').item(0).appendChild(scriptTag);  
    }        
    
/*
 * To Show Bubble Guru.
 */    
    function showBubbleGuru() {
         includeScript ("http://www.bubbleguru.com/bubbleguru_UA/05_left.js?account=modpost_1&amp;"); 
         include ("http://www.bubbleguru.com/bubbleguru_files/05_on_ws_rpt.js");
    }
    
    
    
/*
 * To Validate Email Form - Public Page.
 */    
    function validateEmailForm() {
        if(isBlankEmail('emailForm','email')) {
            checkRequestEmailExist();    
        }
    }
    
    
    

/*** Popup for Thank You ***/

      var thankYouWin;
	  thankYouWin = new Ext.Window({
	  	    el:'thankYou-win',
		    layout:'fit',
		    width:500,
		    height: 320,
		    closeAction:'hide',
		    plain: true,

		    items: new Ext.TabPanel({
		    	el: 'thankYou-tabs',
		    	autoTabs:true,
		    	activeTab:0,
		    	deferredRender:false,
		    	border:false
		   }),

    	   buttons: [{
			    text: 'Close',
			    handler: function(){
			    	thankYouWin.hide();
			    }
		   }]
	});
/*
 * Thank You Popup.
 */

    function showThankYouPopup() {
        try {
           document.getElementById("sessLogMsg").innerHTML = "";
        } catch(e) {}
        try {
           document.brokerForm.pwd.value = "";
        } catch(e) {}
	if (ie){
		documentWidth = (centerElement().offsetWidth)/2+centerElement().scrollLeft-(winw/2);
		documentHeight = (centerElement().offsetHeight)/2+centerElement().scrollTop-(winh/2);
	}
	else if (ns){
		documentWidth=window.innerWidth/2+window.pageXOffset-(winw/2);
		documentHeight=window.innerHeight/2+window.pageYOffset-(winh/2);
	}
	else if (ns6){
		documentWidth=self.innerWidth/2+window.pageXOffset-(winw/2);
		documentHeight=self.innerHeight/2+window.pageYOffset-(winh/2);
	}     

        try {
            thankYouWin.setPosition( documentWidth,documentHeight );
        } catch(e) {}
        try {
            thankYouWin.show();
        } catch(e) {}
    }    
    
    function preQualifyEmailDocs() {
        var email = "";
        var xmlDoc = "";
        try {
            email = document.emailForm.email.value;
        } catch(e) {}
        
        if(email !="") {
            window.location.href = "modpostReg.php?email="+email;
        }
    } 
    
    function updateEmailDocs() {
        var email = "";
        var xmlDoc = "";
        try {
            email = document.emailForm.email.value;
        } catch(e) {}
        var url = "insertNewEmailDocs.php";
        var qstr = "email="+email;
        var xmlDoc = "";
        
        if(email !="") {
            try {
                xmlDoc = getXMLDoc(url,qstr);
            } catch (e) {}
        }  
        try {
            status = xmlDoc.getElementsByTagName("status")[0].firstChild.nodeValue;
        } catch (e) {}
    } 

/*
 * Check Email Already Exists.
 */

    function checkRequestEmailExist() {
        var borrowerDetails = "";
        var email = "",status = "";
    
        email = document.emailForm.email.value;
        try {
           email = trim(email);
        } catch(e) {}
        
        var url = "getBorrEmailInfo.php";
        var qstr = "email="+email;
        var xmlDoc = "";
        
        if(email !="") {
            try {
                xmlDoc = getXMLDoc(url,qstr);
            } catch (e) {}
        }  
        try {
            borrowerDetails = xmlDoc.getElementsByTagName("borrowerDetails");
            status = xmlDoc.getElementsByTagName("status")[0].firstChild.nodeValue;
        } catch (e) {}
        
        if(status == "Exist") {
            showLoginForm();
            try {
                document.emailForm.email.value = email;
            } catch(e) {}
            setCookieBorrower(email);
            try {
               document.getElementById("sessLogMsg").innerHTML = "<b>Please Login</b>";
            } catch(e) {}
        } else {
            updateEmailDocs();
            showThankYouPopup();
        }
    }
    function forgotPwd() {
       if(isEmailOk('modPostForm','userName')) {
           try {
               document.getElementById("sessLogMsg").innerHTML = "Please Wait... <img src=\"images/large-loading.gif\">";
           } catch(e) {}
           setTimeout("sendBorrowerPwd()","100");
       }
    }
    function sendBorrowerPwd() {
           var xmlDoc = "";
           var email = "";
           var uType = "br";
           var msg = "";
           try {
               email = document.modPostForm.userName.value;
           } catch(e) {}
           var url = "forgotPwdXML.php";
           var qstr = "email="+email;
           try {
               xmlDoc = getXMLDoc(url,qstr);
           } catch(e) {}
           try {
               msg = xmlDoc.getElementsByTagName("msg")[0].firstChild.nodeValue;
           } catch (e) {}
           try {
               document.getElementById("sessLogMsg").innerHTML = msg;
           } catch(e) {}
    }  