/*****************************************************************
* File: loginWidget.js
* Description: This javascript file contains all the scripts 
*              related to the login functionalities from the 
*              Login widget at the header section. The login 
*              widget uses AJAX technologies.
*******************************************************************/



/*------------------------------------------------------------------
*  Global Variables
**------------------------------------------------------------------*/

/** Variable: imgURL
* This variable is used to hold the value of the root image location.
* This variable is initialized in a way that if the value is null we can hardcode it to the default value.
* The value is populated in the script block of the jsp where this js file is included.
*/
imgURL = (imgURL == null)?'/CDA/images/':imgURL;

/** Variable: errUIDmsg, errPWDmsg
* These variables are used to hold the value of MC driven error messages.
* These variables are initialized in a way that if we get null from MC we can hardcode it to the default value.
* Theese values are populated in the script block of the jsp where this js file is included.
*/
errUIDmsg = (errUIDmsg == null)?'Please input a valid username':errUIDmsg;
errPWDmsg = (errPWDmsg == null)?'Please input a valid password':errPWDmsg;
errLogin = (errLogin == null)?'There has been an error while Login. Please try again.':errLogin;

/* Variable: gSub
*  This variable is used to keep a check on multiple clicks on the login button. 
*  You should be able to click on the login button effectively only once, any 
*  further click should not be acted upon.
*  
*  Used By: Function: ajaxStateChanged(), validateWidgetForm(mT)
*/
var gSub = 0;


/* Variable: xmlHttpWidget
*  This variable is the main xmlHTTP object used for AJAX submit.
*  
*  Used By: Function: ajaxSubmitForm()
*/
var xmlHttpWidget;


/* Variable: isLoginWidgetVisible
*  This variable is the flag to indicate the visibility state of the Login Widget. 
*  Default value: false
*  
*  Used By: Function: toggleLoginWidget()
*/
var isLoginWidgetVisible = false;
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var isIE = (navigator.appName.indexOf("Internet Explorer")!=-1);







/*------------------------------------------------------------------
*  Functions
**------------------------------------------------------------------*/

/*
* Function: GetXmlHttpObject()
* This function creates a handle to the XMLHTTP object based on the 
* Browser category
*/
function GetXmlHttpObject(){

	var xmlHttp=null;
	try{ // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}catch (e){  // Internet Explorer
	  try{
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }catch (e){
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	return xmlHttp;
} 


/*
* Function: ajaxSubmitForm()
* This function submits (POST) the Mainform using AJAX XMLHTTP and transfers 
* the control to the function: ajaxStateChanged() thereafter
* 
*/
function ajaxSubmitForm(){ 

	xmlHttpWidget=GetXmlHttpObject();
	if (xmlHttpWidget==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var signUrl=document.LoginWidgetForm.loginURL.value;
	var data = "action=" + document.LoginWidgetForm.action.value;
	data += "&tempuser=" + document.LoginWidgetForm.tempuser.value;
	data += "&membershipType=" + document.LoginWidgetForm.membershipType.value;
	data += "&Login=" + document.LoginWidgetForm.Login.value;
	data += "&Password=" + document.LoginWidgetForm.Password.value;
	
	
	xmlHttpWidget.onreadystatechange=ajaxStateChanged;
	xmlHttpWidget.open("POST", signUrl, true);
    xmlHttpWidget.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttpWidget.send(data);
}


/*
* Function: ajaxStateChanged()
* This function polls the response after sumbit of the xmlHTTP request and does 
* the following things
*  -->It checks if the HTTP response code is not 200 (Success) -- This is an error 
*     condition
*  -->It checks whether the response contains a particular string of text which 
*     comes back in case of a login error in the standard login flow
* On error a standard error msg is displayed inside the widget. Else the response 
* is rendered in the main window.
*/
function ajaxStateChanged(){ 
	
	if (xmlHttpWidget.readyState==4){
		document.getElementById('imgLoadingW').style.display="none";
		document.body.style.cursor = 'default';
//		setLowerDiv();
		
		var error = (xmlHttpWidget.status != 200) 
				|| (xmlHttpWidget.responseText.indexOf('<div class="errorMessage" style="position:absolute;top:187;left:304;width:455;z-index:10">') != -1)

		if (error && document.LoginWidgetForm.action.value == 'login'){
			if(isLoginWidgetVisible){
				document.getElementById('errorMsg').innerHTML = errLogin
				document.getElementById('errorMsg').style.display="block";
//				setLowerDiv();
			}
			gSub = 0 // this is to reset the login flag else 
					 // system would consider the next attempt 
					 // to be a doubleclick on the login button
		}else{
			if(!isNN && isIE){
				if(document.LoginWidgetForm.membershipType.value == 'points')
					document.URL = secURL + '/GPN2/CDA/Functional/Navigation/GPN2_index?body=GPN2_home';
				else if(document.LoginWidgetForm.membershipType.value == 'weeks')
					 document.URL = secURL + '/RCIW/RCIW_index?body=RCIW_MarketingOffers';
			}
			else if(isNN && !isIE){
				if(document.LoginWidgetForm.membershipType.value == 'points')
					document.location.href = secURL + '/GPN2/CDA/Functional/Navigation/GPN2_index?body=GPN2_home';
				else if(document.LoginWidgetForm.membershipType.value == 'weeks')
					 document.location.href = secURL + '/RCIW/RCIW_index?body=RCIW_MarketingOffers';
			}
		}
		
	}
}

/*
* Function: ajaxSubmitForm()
* This function submits (POST) the Mainform with a action value as 'forgot' 
* to invoke the forgot password txn
* 
*/
function submitForgotPasswordWidget(x) {	

	document.LoginWidgetForm.action.value='forgot'
	document.LoginWidgetForm.membershipType.value = x
	document.LoginWidgetForm.submit();		//Here the main form should submit synchronously 
									//so that the forgot password page comes back in 
									//the main window.
	}


/*
* Function: validateWidgetForm(mT)
* This function validates the Mainform inside the widget. This function is remodelled 
* after the existing Function: validateForm(mT,val) with some modifications like AJAX 
* form submit et al. 
*
* parameters: mT -- Membership Type ['weeks'|'points']
*/
function validateWidgetForm(mT) {	

	if(gSub > 0) return false;
	if(document.LoginWidgetForm.Login.value == '') 	{
		if(isLoginWidgetVisible){
			document.getElementById('errorMsg').innerHTML = errUIDmsg
			document.getElementById('errorMsg').style.display="block";
//			setLowerDiv();
		}
		//alert("Please input a valid login id")
		document.LoginWidgetForm.Password.value = ''
		document.LoginWidgetForm.Login.focus()
		return false
	}
	
	if (document.LoginWidgetForm.Password.value == '') 	{
		if(isLoginWidgetVisible){
			document.getElementById('errorMsg').innerHTML = errPWDmsg
			document.getElementById('errorMsg').style.display="block";
//			setLowerDiv();
		}
		//alert("Please input a valid password")
		document.LoginWidgetForm.Password.focus()
		return false
	}
	document.body.style.cursor = 'wait';
	document.getElementById('imgLoadingW').style.display="block";
	document.getElementById('errorMsg').style.display="none";
	document.LoginWidgetForm.action.value = 'login'
	document.LoginWidgetForm.membershipType.value=mT

	gSub++;
	
//	setLowerDiv();
	
	ajaxSubmitForm();
	
	//return true;
	
	}

/*
* Functions: hilite(el), unhilite(el)
* These function-pair toggles the style of the Sign-In buttons 
*/
function hilite(el) {
	el.style.cursor="hand"	
	el.style.backgroundImage='url(' +imgURL + 'buttons/homeButtonBackHilite.gif)'
	}
function unhilite(el) {
	el.style.cursor="pointer"		
	el.style.backgroundImage='url(' +imgURL + 'buttons/homeButtonBack.gif)'
	}
		
		
		
		
/*
* Functions: displayLogin()
* This function toggles the visibility and style of the Login Widget Div
*/

function displayLogin(tblobj) {

	if(!isLoginWidgetVisible){
//		document.getElementById("loginWidget").style.opacity='0.64';
//		document.getElementById("loginWidgetB").style.opacity='0.64';
		document.getElementById("tblWidget").style.opacity='0.99';
//		divobj.style.top = 24;
//		divobj.style.left = 637;
	
//		divobj.style.display="block";
//		tblobj.style.display="block";
//		divobjB.style.display="block";
				
//		setLowerDiv();
		
		document.getElementById('spanSignIn').className = 'WidgetLinkUp';
		document.getElementById('imgWidgetDown').src = imgURL + 'widget_up.gif';
		isLoginWidgetVisible = true;
		if (xmlHttpWidget != null && (xmlHttpWidget.readyState==1 || xmlHttpWidget.readyState==2 || xmlHttpWidget.readyState==3)){
			document.getElementById('imgLoadingW').style.display="block";
			document.body.style.cursor = 'wait';
//			setLowerDiv();
		}
		if (xmlHttpWidget != null && xmlHttpWidget.readyState==4)
			if ((xmlHttpWidget.status != 200) 
				|| (xmlHttpWidget.responseText.indexOf('<div class="errorMessage" style="position:absolute;top:187;left:304;width:455;z-index:10">') != -1)
 				&& document.LoginWidgetForm.action.value == 'login'){
 					document.getElementById('errorMsg').style.display="block";
// 					setLowerDiv();
 			}
			doAnimation(2);
	}
	
	else {
//		document.getElementById("loginWidget").style.opacity='0.75';
//		document.getElementById("loginWidgetB").style.opacity='0.75';
		document.getElementById("tblWidget").style.opacity='0.99';
//		divobj.style.top = 24;
//		divobj.style.left = 637;
	
//		setLowerDiv();
	
//		divobj.style.display="none";
//		tblobj.style.display="none";
//		divobjB.style.display="none";
		document.getElementById('spanSignIn').className = 'WidgetLinkDn';
		document.getElementById('imgWidgetDown').src = imgURL + 'widget_down.gif';
		isLoginWidgetVisible = false;
		document.getElementById('imgLoadingW').style.display="none";
		document.getElementById('errorMsg').style.display="none";
		doAnimation(1);

	}
	
}

function setLowerDiv(){
	var tbltop = document.getElementById('tblWidget').style.top.substring(0,document.getElementById('tblWidget').style.top.indexOf("px"));
	var tblheight = document.getElementById('tblWidget').clientHeight;
	document.getElementById('loginWidgetB').style.top = parseInt(tblheight) +parseInt(tbltop) + "px";
}

		