//<%--
//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright IBM Corp.  2007
//* All Rights Reserved
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*
//--%>

	
MyAccountDisplay={
	
	//contextChanged boolean
	//		 indicating if an context change happened
	//isHistory boolean
	//		 indicating if the refresh are caused by browser back/forward event.
	contextChanged:false, 
	isHistory:false,
	
	prepareOrderCopy:function(OrderCopyURL){
		///////////////////////////////////////////////////////////////////
		// summary:This method invokes the ordercopy service to copy the old order.
		// Description:  This function sets the url for ordercopy service and, 
		// then it invokes the service.
		//////////////////////////////////////////////////////////////////		
		cursor_wait();
		wc.service.getServiceById("OrderCopy").url=OrderCopyURL;
		wc.service.invoke("OrderCopy");
	},
	loadContentFromURL:function(workAreaModeValue, resultPageURL){

		///////////////////////////////////////////////////////////////////
		// summary: This function loads the content from the view for a given URL.
		// Description: This function will take the "resultPageURL" as input attribute, 
		// and will sets the coresponding view content to the "RefreshArea".
		//////////////////////////////////////////////////////////////////			
		cursor_wait();

		wc.render.getRefreshControllerById("MyAccountCenterLinkDisplay_Controller").url = resultPageURL;

		wc.render.updateContext("MyAccountCenterLinkDisplay_Context", {workAreaMode:workAreaModeValue});
		
	},




	prepareSubmit:function(form,logonPassword,logonPasswordVerify){
		
		///////////////////////////////////////////////////////////////////
		// summary: This function will update the user information.
		// Description: This function will takes "Registrationform" , "logonPassword" and  
		// "logonPasswordVerify" as input arguments, and calls the "UserRegistrationUpdate" command to 
		// update the information and then, it will sets the content of the resulting view to "RefreshArea".
		//////////////////////////////////////////////////////////////////	
		reWhiteSpace = new RegExp(/^\s+$/);
		if(form.firstName != null && reWhiteSpace.test(form.firstName.value) || form.firstName.value == ""){ Common.formErrorHandleClient("firstName",Common.errorMessages["REQUIRED_FIRSTNAME_ENTER"],form.name,'refreshArea'); return;}		
		if(form.lastName != null && reWhiteSpace.test(form.lastName.value) || form.lastName.value == ""){ Common.formErrorHandleClient("lastName",Common.errorMessages["REQUIRED_LASTNAME_ENTER"],form.name,'refreshArea'); return;}
//		if(form.address1 != null && reWhiteSpace.test(form.address1.value) || form.address1.value == ""){ Common.formErrorHandleClient("address1",Common.errorMessages["REQUIRED_FIELD_ENTER"],form.name,'refreshArea'); return;}
//		if(form.city != null && reWhiteSpace.test(form.city.value) || form.city.value == ""){ Common.formErrorHandleClient("city",Common.errorMessages["REQUIRED_FIELD_ENTER"],form.name,'refreshArea'); return;}
//		if(form.state != null && reWhiteSpace.test(form.state.value) || form.state.value == ""){ Common.formErrorHandleClient("state",Common.errorMessages["REQUIRED_FIELD_ENTER"],form.name,'refreshArea'); return;}
//		if(form.country != null && reWhiteSpace.test(form.country.value) || form.country.value == ""){ Common.formErrorHandleClient("country",Common.errorMessages["REQUIRED_FIELD_ENTER"],form.name,'refreshArea'); return;}
//		if(form.zipCode != null && reWhiteSpace.test(form.zipCode.value) || form.zipCode.value == ""){ Common.formErrorHandleClient("zipCode",Common.errorMessages["REQUIRED_FIELD_ENTER"],form.name,'refreshArea'); return;}
//		if(form.email1 != null && reWhiteSpace.test(form.email1.value) || form.email1.value == ""){ Common.formErrorHandleClient("email1",Common.errorMessages["REQUIRED_FIELD_ENTER"],form.name,'refreshArea'); return;}
//		if(!Common.isValidEmail(form.email1.value)){ Common.formErrorHandleClient("email1",Common.errorMessages["WISHLIST_INVALIDEMAILFORMAT"],form.name,'refreshArea');return ;}
		
		if (form.logonPassword_old.value.length != 0)
		{
			form.logonPassword_old.name = "logonPassword";
			form.logonPasswordVerify_old.name = "logonPasswordVerify";
			if(form.logonPassword_old.value != form.logonPasswordVerify_old.value)
			{
				alert(Common.errorMessages["PWDREENTER_DO_NOT_MATCH"],form.storeId.value,form.catalogId.value,form.langId.value);	
				return ; 
			}
			
		}
		
//		if(!Common.isValidEmail(form.email1.value)){ 
//			alertDialog(Common.errorMessages["WISHLIST_INVALIDEMAILFORMAT"],form.storeId.value,form.catalogId.value,form.langId.value);
//			return ;
//		}

		//person services do not handle empty gender
		//change the name of the parameter
		if(form.gender.value=="") {
			form.gender.name="genderTemp";
		}
		
		if(form.sendMeEmail){
			if (form.sendMeEmail.checked){ 
				form.receiveEmail.value = true;
			}else form.receiveEmail.value = false;
		}

		//add for extra field in cubcadet
		//form.userField3.value=form.QUE1.value+'~'+form.QUE2.value+'~'+form.QUE3.value;
		
		wc.service.invoke("UserRegistrationUpdate");

		
	},
	
	processBookmarkURL:function(){
		
		// summary: This function is for handling bookmarked urls
		// Description: If there is bookmark information in the url, extract the url after #. 
		//              update context for MyAccountCenterLinkDisplay_Context with the bookmarked url.
		         
		var bookmarkId = location.hash;			
		if(bookmarkId){					        
			bookmarkId = bookmarkId.substring(1, bookmarkId.length);
			
		}   
		
		if(bookmarkId){
		    //to do the best is to make several var for the urls
		    //and then each time add to history, just add in the workareamode
		    //later have a function called sth, it will say if this is the mode, then url = this
		    //if that mode, url = that
		    //and then call load context from url.
		    //the benifit of this is that even this is page is load from bookmark id, 
		    //we still can have the correct mode for it.
		   
	            var indexOfIdentifier = bookmarkId.indexOf("identifier", 0);
	            if ( indexOfIdentifier >= 0) {
		    		var realUrl = bookmarkId.substring(0, indexOfIdentifier - 1);
		    }
		    MyAccountDisplay.loadContentFromURL("bookmarkedPage", realUrl);
		}
	},
	
	//to do: add a new function add bookmarkid. move the code for bookmark to here from postrefresh handler.	
	initMyAccountDisplayHistory:function(workAreaModeValue, elementId, changeUrl){
	
	// summary: this function sets the initial state of dojo browser history. 
	// Description: This function sets the initial state of dojo browser history for MyAccountDispay page. 
	// dojo API   : dojo.undo.browser.setInitialState
	// workAreaModeValue: String
	//		a value to uniquely identify an context, which is among the following: 
	//              myAccountMain, personalInformation, addressBook, checkoutProfile, wishList, bookmarkedPage
	// elementId:String
	//             the id of the widget.
	// changeUrl:String
	//             the url used to load new context.
				
		var historyObject = new MyAccountDisplay.HistoryTracker(workAreaModeValue, elementId, changeUrl);
		dojo.undo.browser.setInitialState(historyObject);	
	},
	
	
	goBack:function(){
	
	// summary: this function belong to HistoryTracking for receiving Back notifications
	// description: this function belong to HistoryTracking for receiving Back notifications	
			
		MyAccountDisplay.loadContentFromURL(this.workAreaModeValue, this.changeUrl);
		MyAccountDisplay.isHistory=true;
		
        },
        
        goForward:function(){
       
	// summary: this function belong to HistoryTracking for receiving forward notifications
	// description:this function belong to HistoryTracking for receiving forward notifications	
	        	
		MyAccountDisplay.loadContentFromURL(this.workAreaModeValue, this.changeUrl);
		isHistory=true;
        },


	clearallfields:function(){
		formObject = document.Register;
		for(i=0;i<formObject.elements.length;i++){
			if(formObject.elements[i].type == 'text'){
				formObject.elements[i].value="";
			}
		}
    },
		
	
	HistoryTracker:function(workAreaModeValue, elementId, changeUrl){
	
	// summary: History state object for history tracking
	// description:History state object for history tracking
	// workAreaModeValue: String
	//		the html to be displayed
	// elementId: String
	//		the name of the DOM object
	// changeUrl: String
	//		the url for the current state of this page
	
		this.workAreaModeValue = workAreaModeValue;
		this.elementId = elementId; //considering delete it
		//TODO: commenting this out breaks FF 1.5. Others? Can't seem to find iframe id/name?
		this.changeUrl =  changeUrl;
		
		
	}
} 
	MyAccountDisplay.HistoryTracker.prototype.back = MyAccountDisplay.goBack;
	MyAccountDisplay.HistoryTracker.prototype.forward=MyAccountDisplay.goForward;
	

