﻿///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////JavaScript functions for QuickQuoteLogin control. See QuickQuoteLogin.onInit() /////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var eventSource;

/*** Function to check user name on Forgot Password click ***/ 
function ForgotPasswordClick(txtUserName)
{
    if (document.getElementById(txtUserName) && document.getElementById(txtUserName).value.replace(" ", "").length > 0)
        return IsValidUserName(txtUserName);
    else
    {
        alert("Enter user name and click on Forgot Password");
        document.getElementById(txtUserName).focus();
        return false;
    }
}

/*** Function toValidate LeadTraveller Email ***/ 
function IsValidUserName(txtUserName)
{
        if (document.getElementById(txtUserName).value.length > 0)
        {
            var email = document.getElementById(txtUserName).value;
            var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/gi;
            
            //alert(emailRegEx.test(email));
            
            var emailRegEx2 = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/gi;
            
            //alert(emailRegEx2.test(email));
            
            if ( emailRegEx.test(email) || emailRegEx2.test(email) )
                return true;
            else
            {
                alert("Enter a valid email address.");
                document.getElementById(txtUserName).focus();
                return false;
            }
        }
        
        return true;
}

/* Function to check whether the user has agreed to terms and conditions before registering*/
function UserAgreeToTermsAndConditions(chkIAgreeToTerms)
{
    if (! document.getElementById(chkIAgreeToTerms).checked)
    {
        alert("Please agree to the terms and conditions before proceeding.");
        return false;
    }
    
       return true;
}

/* Function to confirm old and new passwords are not same */
function ConfirmNewPwd(oldPwd, newPwd, newPwdRepeat)
{
    if((oldPwd.length > 0 && newPwd.length > 0 && newPwdRepeat.length > 0) && oldPwd == newPwd)
    {
        alert("New password can not be same as the old one.");
        return false;
    }
    return true;
}

/* function to show registration successful message in modal dialog or alert messagebox. User will be redirected to QQ onclik of "OK" */
function PopupRegistrationSuccessfulMessage(message)
{
    if (ShowNewStyle)
    {
        message = "<div class='clear spacer_10'></div><div class='float-left' style='text-align:justify;'>" + message + ".</div>";
        $("#vemdErrorList").html(message);
        centerVEMD(100);
        loadVEMD();
    }
    else
    {
        alert(message);
        RedirectToQuickQuote();
    }
}

/* # function to redirect user to QQ after excluding "register" querystring parameter. 
   # Called from PopupRegistrationSuccessfulMessage() and also onclick of OK button in modal dialog. 
   # See QuickQuoteLogin.cs.CreateControlsForRegistrationSuccessfulModalDialog()
*/
function RedirectToQuickQuote()
{
    //if (isPQE) //redirect to QQ with all the querystring parameters   
    //{
        var queryString = location.search;
        var newQueryString = "";
        var url = location.href.replace(location.search, "");

        //alert(queryString);

        if (queryString != undefined && queryString != "undefined" && queryString != "")
        {
	        queryString = queryString.split("?");
	        queryString = queryString[1].split("&");
	        newQueryString = "?";
    	    
	        for(var i=0; i<queryString.length; i++)
	        {
                        if (queryString[i].split("=")[0].toLowerCase() != "register")
		        	        newQueryString += queryString[i] + "&";
	        }
        }
        
        if (newQueryString.length > 0)
            newQueryString = newQueryString.substring(0,newQueryString.length-1); //trim last &
        
         url = url + newQueryString;    
         //alert(url)
         location.href = url;
    //}
    //else
    //    location.href = location.protocol + "//" + location.hostname;
}
