	function validateQuickSubForm(formObj){

		if( dijit.byId("country").getValue() != "" ) {
			
		   var ctrySelected = dijit.byId("country").getValue();	   
		   var firstIdx = ctrySelected.indexOf("_");	   
		   var priceCode = ctrySelected.substring(0,firstIdx);	   
		   var ccyStr = ctrySelected.substring(firstIdx+1);
		   var defaultCcy = ccyStr.substring(0,ccyStr.indexOf("_"));
		   if ( priceCode == "") {
			alert("Please choose your country");
			return false;
		   }
		   document.quickSubForm.ctycode.value=ctrySelected;
		   return true;
		}
		else		
		{
			alert("Please choose your country");
			return false;
		}

	}



   function saveAddrForm(addrForm, formSuffix){   	
   	//alert(addrForm.name);
	setAllCheckboxHiddenFields(addrForm.name);       	
	var country1Id = "country" + formSuffix + "1";
	var country2Id = "country" + formSuffix + "2";	
   	
   	//alert("about to submit form = " + addrForm.name);
   	if ( validAddr(addrForm, formSuffix ) ){ 
   		addrForm.submit();
   		//alert("going to submit");
   	}
   }


   function validAddr(addrForm, formSuffix) {
   
	   var country1Id = "country" + formSuffix + "1";
	   var country2Id = "country" + formSuffix + "2";
	   var shipCountry = country1Id;
	   var shipCountryIdx = "1";
	   // check which country is the shipping country if two addresses are
	   // available
	  // alert(shipCountry);
	   var shipCB1 = "shipAddrCB" + formSuffix  +"1";
	   var shipCB2 = "shipAddrCB" + formSuffix  +"2";
	   if ( dojo.byId(shipCB2) ) {		
		if ( dojo.byId(shipCB1).checked ) {
			if ( dojo.byId(shipCB2).checked ){
				alert("You have selected two shipping addresses for the subscription. Please select only one. Thanks.");
				return false;
			}
			else {
				shipCountry = country1Id;
				shipCountryIdx="1";
			}
		}
		else if ( dojo.byId(shipCB2).checked ) {
			shipCountry = country2Id;			
			shipCountryIdx="2";
		}
		else {
			alert("Please indicate the shipping country.");
			return false;
		}
	   }
	  // alert(shipCountry);
	   if ( dijit.byId(shipCountry) ) {
		   //console.log(country1Id);
		   //alert(shipCountry);
		   if( dijit.byId(shipCountry).isValid() &&( dijit.byId(shipCountry).getValue() != "" )){   	
			   var ctrySelected = dijit.byId(shipCountry).getValue();
			   var priceCode = ctrySelected.substring(0,ctrySelected.indexOf("_"));
			   if ( priceCode == "") {
				alert("Please choose the country you live in");
				return false;
			   }			
			   else {
			   	if ( ( addrForm.priceZone.value == 'UK' ) && ( priceCode != 'UK') ){
			   		alert("Sorry, cannot change country of the shipping address to outside of the United Kingdom");
			   		return false;
			   	}
			   	else if ( ( addrForm.priceZone.value == 'EU' ) && ( priceCode == 'ROW') ){
			   		alert("Sorry, cannot change country of the shipping address to outside of the EU");
			   		return false;
			   	}
			   	
			   } 
			   //alert(dijit.byId(country1Id).getValue());
			   addrForm.countryName.value=dijit.byId(shipCountry).getValue();
			   
		   }		   
		   else{
			alert("Please choose the country you live in");
			return false;
		   }
		   addrForm.countryName1.value=dijit.byId(country1Id).getDisplayedValue();
		   if ( dijit.byId("country2") ){
			addrForm.countryName2.value=dijit.byId(country2Id).getDisplayedValue();
		   }
		  // alert("countryname" + addrForm.countryName.value);
		   
	   }
	   //alert("returning false");
	   return true;
   
   }		
	
   function showAddr(tableId){
   	dojo.byId(tableId).style.display="block";
   }

   function hideAddr(tableId){
   	dojo.byId(tableId).style.display="none";
   }


   function checkEmail(){
	   if ( dijit.byId("email").getValue().length > 1 ){
		   dojo.xhrGet({
			   url: '/classbar/AccountEditorServlet',
			   handleAs: "json",
			   preventCache: true,
			   load: showEmailCheckStatus,
			   error: showError2,
			   content: { "useraction" :"checkEmail",
			   	          "email": dijit.byId("email").getValue() }
			});
	   }
   }
   
   function showEmailCheckStatus(jdata,ioArgs){
	   dijit.byId("password").setValue("");
	   dijit.byId("retypePass").setValue("");
   	   console.log("email status  = " +jdata);
	   if ( jdata.errormessage ){
		   console.log(jdata.errormessage);
		   dojo.byId("emailCheckMsg").innerHTML="";
	   }
	   else 
	   if ( jdata.userStatus == "exists" ) {
		   dojo.byId("emailCheckMsg").innerHTML="<i>User account for this email already exists. To reset password "+
		    " <a href=\"\/classbar\/main\/classMagHome.jsp?pageId=6\">click here</a></i>";
	   }
	   else
	   if ( jdata.userStatus == "created" ) {
		   dojo.byId("emailCheckMsg").innerHTML="We have detected your email exists in our system. " +
		   		"Your password is being emailed to you. Please login with your password to view/edit your account.";
	   }
	   else
	   {
		   dojo.byId("emailCheckMsg").innerHTML="";
	   }	   
	   
   }
   
   function showPage(){   
	dojo.byId("formHolder").style.visibility="visible";   
   }
   
   function loadAccount(){
	  //  var mode = document.accountEditor.mode.value;
	//	console.log("loadaccount " + mode);
		console.log("visibility = " + dojo.byId("formHolder").style.visibility);
		
		console.log("(plain)Page reload count = " + document.plainform.reloadcount.value);
		//alert("(plain)Page reload count = " + document.plainform.reloadcount.value);
		
		if ( eval(document.plainform.reloadcount.value) > 0){
			console.log("should reload page now");		
			reloadPage();
		}
		else {
			dojo.byId("formHolder").style.visibility="visible";
		}
		document.plainform.reloadcount.value = eval(document.plainform.reloadcount.value) + 1;
		
		/*if ( ( mode ) && (mode == "edit") )
		{
			requestFormDataForLoggedInUser();
		}*/		
	}
	
	function requestFormDataForLoggedInUser()
	{		 
		 //clearMsg();
		 var lookupContactId = document.accountEditor.lookupContactId.value;
		 console.log("requestFormDataForLoggedInUser for contactId " + lookupContactId);
		 dojo.xhrGet({
				   url: '/classbar/AccountEditorServlet',
				   handleAs: "json",
				   preventCache: true,
				   load: fillform,
				   error: showError2,
				   content: { "useraction" :"contactDetails",
				   	      "contactId": lookupContactId }
				});
	}	
	
	function showError2(response, ioArgs){
		console.log(response);
		//dojo.byId("message").innerHTML = "<b>Apologies, an unexpected error has occurred. We will investigate and resolve this shortly.</b>";
	}
	
	function reloadPage(){
		window.location.href="/classbar/main/accountEditor.jsp";
	}
	
	function fillform(data,ioArgs)
	{	
			
		if ( data.refreshPage ){
			if ( data.refreshPage == "ok" ){
				reloadPage();
				return;
			}
		}				
		console.log("fillform");
		console.log(data);
		
		//clearMsg();		
		console.log("useraction");
		setHiddenFieldValue(document.accountEditor.useraction,"save");
		console.log("contactId");
		setHiddenFieldValue(document.accountEditor.contactId,data["contactId"]);
		
		/*var contactId = data["contactId"];
		
		if ( contactId && ( trim(contactId) != '' ) ) {
			document.accountEditor.mode.value = "edit";
		}*/
			
			
		console.log("addressId");
		setHiddenFieldValue(document.accountEditor.addressId,data["addressId"]);			
		setHiddenFieldValue(document.accountEditor.countryName1,data["country1"]);
			

		setTextValue("email",data["email"]);	
		setTextValue("contactName",data["contactName"]);	
		setTextValue("company",data["company"]);
		setTextValue("position",data["position"]);
		
		setCheckBox("homeAddrCB1",data["homeAddrYN1"]);
		setCheckBox("workAddrCB1",data["workAddrYN1"]);
		setHiddenFieldValue("homeAddrYN1", data["homeAddrYN1"]);
		setHiddenFieldValue("workAddrYN1", data["workAddrYN1"]);
		
		setTextValue("street1",data["street1"]);
		setTextValue("district1",data["district1"]);
		setTextValue("city1",data["city1"]);			
		setTextValue("state1",data["state1"]);
		setTextValue("postcode1",data["postcode1"]);
		
		console.log("country1");
		if ( data["country1"] ) {
			dijit.byId("country1").setDisplayedValue(data["country1"]);
		}
		
		setTextValue("workPhone",data["workPhone"]);
		setTextValue("workExt",data["workExt"]);
		setTextValue("homePhone",data["homePhone"]);
		setTextValue("mobile",data["mobile"]);		
		
		setTextValue("altEmail",data["altEmail"]);			
		setTextValue("dob",data["dateOfBirth"]);
		
		setCheckBoxIfCheckedValue("sendEmailYN",data["sendMailYN"],"N");
     }	
	
	
	function setCheckBoxHiddenField(formName, cbName, hiddenFldName){
		var form1 = document.forms[formName];				
		if ( form1[cbName] && form1[cbName].checked ){
			form1[hiddenFldName].value = 'Y';
		}
		else{
			form1[hiddenFldName].value = 'N';
		}
         }
	
      	function setAllCheckboxHiddenFields(formName){    
		setCheckBoxHiddenField(formName,'homeAddrCB1','homeAddrYN1');
		setCheckBoxHiddenField(formName,'workAddrCB1','workAddrYN1');
		setCheckBoxHiddenField(formName,'shipAddrCB1','shipAddrYN1');
		setCheckBoxHiddenField(formName,'billAddrCB1','billAddrYN1');
		
		setCheckBoxHiddenField(formName,'homeAddrCB2','homeAddrYN2');
		setCheckBoxHiddenField(formName,'workAddrCB2','workAddrYN2');
		setCheckBoxHiddenField(formName,'shipAddrCB2','shipAddrYN2');
		setCheckBoxHiddenField(formName,'billAddrCB2','billAddrYN2');		
        }
	
	function saveAccount(){
		//document.accountEditor.useraction.value="save";		
		//clearMsg();
		setAllCheckboxHiddenFields("accountEditor");

		if ( document.accountEditor["sendEmailCB"] && document.accountEditor["sendEmailCB"].checked ){
			document.accountEditor["sendEmailYN"].value = 'N';
		}
		else{
			document.accountEditor["sendEmailYN"].value = 'Y';
		}	

		if ( validateAccountsForm() ) {
			document.accountEditor.dest.value="/classbar/main/classMagHome.jsp?pageId=3&showback=Y";			
			document.accountEditor.submit();
			
		}
		
	}

	function subscribe(){	
		setAllCheckboxHiddenFields("accountEditor");
		if ( validateAccountsForm() ) {
			//alert("about to submit "  + document.accountEditor.countryName.value);
			document.accountEditor.dest.value="/classbar/main/subscribe.jsp";
			document.accountEditor.submit();
		}		
	}
	
	
	function validateAccountsForm()
	{	
		if (!dijit.byId("email").isValid()){
			alert("Please enter a valid email address.");
			return false;
		}		
		if ( document.accountEditor.mode.value == "new" ) {
			if (!dijit.byId("password").isValid()){
				alert("Please enter a password.");
				return false;
			}		
			if (!dijit.byId("retypePass").isValid()){
				alert("Please re-type your password.");
				return false;
			}		
			if (dijit.byId("password").getValue()!=dijit.byId("retypePass").getValue()){
				alert("The two passwords do not match.");
				dijit.byId("retypePass").setValue("");
				return false;
			}
		}
		if (!dijit.byId("contactName").isValid()){
			alert("Please enter a contact name");
			return false;
		}
		else
		{
		   var country1Id = "country1";	
		   if ( dojo.byId("shipAddrCB2") ) {
		   	if ( dojo.byId("shipAddrCB1").checked ) {
		   		if ( dojo.byId("shipAddrCB2").checked ){
		   			alert("You have selected two addresses for shipping. Please select only one. Thanks.");
					return false;
		   		}
		   		else {
		   			country1Id = "country1";
		   		}
		   	}
		   	else if ( dojo.byId("shipAddrCB2").checked ) {
		   		country1Id = "country2";		 
		   	}
		   	else {
		   		alert("Please indicate the shipping country.");
		   		return false;
		   	}
		   }
		   
		   if ( dijit.byId(country1Id) ) {
			   //console.log(country1Id);
			   //alert(country1Id);
			   if( dijit.byId(country1Id).isValid() &&( dijit.byId(country1Id).getValue() != "" )){   	
				   var ctrySelected = dijit.byId(country1Id).getValue();
				   var priceCode = ctrySelected.substring(0,ctrySelected.indexOf("_"));
				   if ( priceCode == "") {
					alert("Please choose the country you live in");
					return false;
				   }			
				   else if ( document.accountEditor.priceZone ){ /* this is an update to existing sub */
				   	/* Cannot change pricing zone if current zone is uk as uk sub is cheapest.*/
					if ( ( document.accountEditor.priceZone.value == 'UK' ) && ( priceCode != 'UK') ){
						alert("Sorry, cannot change country of the shipping address to outside of the United Kingdom");
						return false;
					}/* Cannot change pricing zone to outside of uk if current zone is EU as EU sub is cheaper. */
					else if ( ( document.accountEditor.priceZone.value == 'EU' ) && ( priceCode == 'ROW') ){
						alert("Sorry, cannot change country of the shipping address to outside of the EU");
						return false;
					}
					/* If pricing zone is ROW then any change allowed as ROW is the costliest. */
				   			   	
			   	   } 
				   //alert(dijit.byId(country1Id).getValue());
				   document.accountEditor.countryName.value=dijit.byId(country1Id).getValue();
				   //alert(document.accountEditor.countryName.value);
			   }		   
			   else{
				alert("Please choose the country you live in");
				return false;
			   }
			   document.accountEditor.countryName1.value=dijit.byId("country1").getDisplayedValue();
			   if ( dijit.byId("country2") ){
				document.accountEditor.countryName2.value=dijit.byId("country2").getDisplayedValue();
			   }
		   }
		  
		}	
		if ( ( dijit.byId("dob").getValue()!="") && (!dijit.byId("dob").isValid() ) ) {
			alert("Please enter a date of birth in dd/mm/yyyy format. Thanks. ");
			return false;
		}
		console.log("returning true");
		return true;
	}
	
	function submitAcctForm(formName, saveUrl, saveFunction, errorFunction)
	{
		 alert("doing io send !!!");
		 console.log("doing io send");
		  dojo.xhrPost({
			   url: saveUrl,				   
			   handleAs: "json",
			   method:"POST",
			   preventCache: true,
			   form: formName,		
			   load: saveFunction,
			   error: errorFunction				   				   
		});
	}
	
	function saveDone(jdata, ioArgs){
		console.log("save done");
		console.log(" jdata.message ");
		dijit.byId("password").setValue("");
		dijit.byId("retypePass").setValue("");
		if ( jdata.message ) {
			dojo.byId("message").innerHTML = "<b>"+jdata.message+"</b>";
		}		
	}
	
	
