/*
Change Log
	
	Date		Programmer		Project	Issue	Subtask	Description
	----		----------		-------	-----	-------	-----------
	01/15/2006	Andrew Leaf			8	4193	5641	Changed message to display on policy services if policy not correct.
	03/05/2007	Adrian J. Moreno	8	5643	6117	updated sessionResult() to use _baseHrefLocation
	05/07/2008	Jim Ames			W00807803			updated reValid() and added invalidZipCodeMessage()
	10/07/2008	Jim Ames			WBSPT4020			Updating support number
*/

/******************************************************************************************/
// Global Ajax functions 
/******************************************************************************************/


/******************************************************************************************/
//  validatePolicyNumber(): Validates the Policy Number.

function validatePolicyNumber(baseUrl, flag)
{
	var x 			= document.policyLookup;
	var polNumber 	= '';
	var zipCode		= 0;
	var isValid 	= false;
	
	setMessage("policyValidationError", "");
	showObject("policyValidationError", 0);
	enableObject("doPolicyLookup", 0, "Searching");
	
	polNumber 	= x.elements["policyNumber"].value;
	if( x.elements["zipCode"] )
	{
		zipCode		= x.elements["zipCode"].value;
		
		if( checkPolicyFormat( polNumber ) && checkZipCode( zipCode ) )
		{
			/* 
			ANDREW - you're probably going to have to change the last parameter of this call 
			based on the flag to call something like resValidAgent() 
			*/
			DWREngine._execute(baseUrl + '/common/ajax/baseAjaxFunctions.cfm', null, 'validatePolicyNumber', polNumber, zipCode, resValid );
		}
		else
		{
			if (flag == 0)
			{
				invalidPolicyNumberMessage();
			}
			else 
			{
				invalidAgentPolicyNumberMessage();
			}
			
		}
		
	}
	else if( checkPolicyFormat( polNumber ) )
	{
		/* 
		ANDREW - you're probably going to have to change the last parameter of this call 
		based on the flag to call something like resValidAgent() 
		*/
		DWREngine._execute(baseUrl + '/common/ajax/baseAjaxFunctions.cfm', null, 'validatePolicyNumber', polNumber, zipCode, resValid );	
	}
	else
	{
		if (flag == 0)
		{
			invalidPolicyNumberMessage();
		}
		else 
		{
			invalidAgentPolicyNumberMessage();
		}
	}
	
	return isValid;
	
}

/******************************************************************************************/
//  validatePolicyNumber(): Validates the Policy Number.

function resValid(result)
{
		
		var i = 0;
		var isValid = true;
		if( result[0].VALUE == 'NO' )
			if( result[1].VALUE == 'WRONG ZIP CODE' )
			{
				invalidZipCodeMessage();	
				isValid = false;
			}
			else
			{
				invalidPolicyNumberMessage();	
				isValid = false;
			}
		else
		{
			enableObject("doPolicyLookup", 0, "Searching");
			document.policyLookup.submit();
		}
		
		
}

/******************************************************************************************/
//  validatePolicyNumber(): Validates the Policy Number.
// Angie Ng W00801985 change the message, set the button to disable while return with unmatch
function invalidPolicyNumberMessage()
{
	var msg = 'We are unable to verify your information. Please check your entries for accuracy. If you need assistance, please contact Customer Service at 866-859-5090<br />';
	setMessage("policyValidationError", msg);
	showObject("policyValidationError", 1);
	enableObject("doPolicyLookup", 0, "Updates Required");
}


function invalidZipCodeMessage()
{
	var msg = 'The mailing zip code entered does not match the mailing zip code on the policy. Please correct and continue.';
	setMessage("policyValidationError", msg);
	showObject("policyValidationError", 1);
	enableObject("doPolicyLookup", 0, "Updates Required");
}





function invalidAgentPolicyNumberMessage()
{
	var msg = 'We are unable to verify the policy number you have entered.  Please verify the policy number for accuracy and resubmit.<br />';
	setMessage("policyValidationError", msg);
	showObject("policyValidationError", 1);
	enableObject("doPolicyLookup", 0, "Updates Required");
}

function checkZipCode( num )
{
	var msg5digit 	= "Please use a valid 5 digit zip code.";
	var msgNumeric 	= "Zipcode is not numeric <br />";
	var isValid 	= true;
	
	if( isNaN( num ) )
	{
		setMessage("policyValidationError", msgNumeric);
		isValid = false;
	}
	
	if( num.length != 5 ){
		setMessage("policyValidationError", msg5digit);
		isValid = false;	
	}
	
	if( isValid ){
		return true;
	}
	else
	{
		showObject("policyValidationError", 1);	
		enableObject("doPolicyLookup", 0, "Updates Required");
		return false;
	}
}

/******************************************************************************************/
//  checkSession(): Checks to see if there is a session.
function checkSession(baseUrl)
{
	DWREngine._execute(baseUrl + '/common/ajax/baseAjaxFunctions.cfm', null, 'checkSession', null, sessionResult );
}

function sessionResult( result )
{
	if( result == "NO" )  
	{
		alert( "Your session has ended. This page is being redirected to the login screen. ");
		window.location.replace( _baseHrefLocation );
	}
}
/********************************************************************************************/
/* launching application by adding or removing the basePolicy from session */
/* must add the following line to the cf page calling the below functions.    */
/* _baseHrefLocation = '#request.base.sf.getCurrentBaseHref()#/';*/

	/*
	@function 	= 	nonValidationLaunch()
	@purpose 	= 	launch application like batch payments were a policy is not needed to be loaded into session.
	@parameters = 	applicationPath: string path to application
	@returns	= 	void
	@usage		=	nonValidationLaunch('/application/index.cfm')
	*/
	function launchPageAndWithoutBasePolicy(applicationPath)
	{
		this.url = applicationPath;
		//ajax call to clear base policy
		DWREngine._execute( _baseHrefLocation + 'common/ajax/baseAjaxFunctions.cfm', null, 'clearPolicyNumber', submitApplication );
	}
	function submitApplication()
	{
		document.location.href = this.url;
	}
	/*
	@function 	= 	launchApplication()
	@purpose 	= 	enable application buttons
	@parameters = 	path: string path to application
	@returns	= 	void
	@usage		=	launchApplication('/application')
	*/
	