/**
 * ====================================================================
 * About
 * ====================================================================
 * UserRegister is an ECMAScript library acting as a cross-browser wrapper for generating the registration form for MobileRota.com.

 * @version 1
 * @author: David Clouter
/**
 * <p>UserRegister is a utility class. Provides "static" methods for EdGE XML RPC calls,
 * @constructor
 */
var UserRegister = Class.extend({
	construct:function() {
		this.debugMode=false;
		this.editMode=false;
		this.formCont=null;
		this.previewCont=null;
		this.formObject=null;
		this.userQuery=null;
		this.companyQuery=null;
		this.fname="";
		this.lname="";
		this.mobileNumber="";
		this.mobileNumber2="";
		this.phoneType="7";
		this.companyName="";
		this.email="";
		this.email2="";
		this.postcode="";
		this.country="GB";
		this.countryCode=null;
		this.password="";
		this.password2="";
		this.subscribe=true;
		this.security="";
		this.companyAddress="";
		this.companyIdArray=new Array();
		this.companyNameArray=new Array();
		this.companyAddressArray=new Array();
		this.companyShiftArray=new Array();
		this.companyChange=0;
		this.terms=false;
		this.startDate="12-Jul-2008";
		this.confirmationCode=null;
		this.confirmationQuery=null;
		this.patternName="New Pattern";
		this.pattern=1;
		this.newPattern=""
		this.shiftsIdArray=null;
		this.shiftsNameArray=null;
		this.PrevxmlHttp=null;
		this.preview=null;
		this.patternQuery=null;
		this.sendQuery=null;
		this.previewShift=null;
		this.country="GB";

		this.pattern=null;
		this.stateHolidays=null;

		this.confirmationQuery=null;
		this.codeValidated=false;
		this.patternConfirmed=null;

		this.companiesList=new Array();
		this.countriesIdList=new Array();
		this.countriesList=new Array();
		this.countriesCodeList=new Array();

		this.phoneTypeList=new Array('Please Select','Standard Java','Android','Blackberry','Blackberry Touchscreen','IPhone - Coming Soon');
		this.phoneTypeIdList=new Array(null,'7','1','2','3','4');
		this.phoneSelected=false;
		this.compQuery=null;
		this.countryQuery=null;
		this.phonesArray=null;
		this.userId=null;
		//showMask();
	},

	parseResults:function(user)
	{
		this.userId=user.getAttribute("userId");
		this.fname=user.getAttribute("fname");
		this.lname=user.getAttribute("lname");
		this.email=user.getAttribute("email");
		this.email2=user.getAttribute("email2");
		var phones=user.selectSingleNode("//mobilePhones/phone");
		this.mobileNumber=phones.getAttribute("mobileNumber");
		this.mobileNumber2=phones.getAttribute("mobileNumber");
		this.country=user.getAttribute("country");
		this.password=user.getAttribute("password");
		this.password2=user.getAttribute("password");
		this.postcode=user.getAttribute("postcode");
		this.country=user.getAttribute("country");

		this.companyName=user.getAttribute("companyName");
		var newsSubscribed=user.getAttribute("subscribed");

		if(newsSubscribed=="1") {
			this.subscribe=true;
		}
		var termsAccepted=user.getAttribute("termsAccepted");
		if(termsAccepted=="1") {
			this.terms=true;
		}

	},

	launchWapWarning:function()
	{
		launchWapWarning(noWapRegister,hideWarning,"My");
	},

	generatePhase1:function()
	{
		document.getElementById("phase1").className="activeSection";
		//document.getElementById("phase1link").href="#";
		//document.getElementById("phase1link").onclick=delegate(this,this.generateForm1);

	},

	generatePhase2:function()
	{
		this.generatePhase1();
//
			document.getElementById("phase2").className="activeSection";
		//	document.getElementById("phase1link").innerHTML="Edit Your Details";
			//document.getElementById("phase2").innerHTML="Terms Accepted";
		//

	},

	generatePhase3:function()
	{
		this.generatePhase1();
		this.generatePhase2();
		document.getElementById("phase3").className="activeSection";
		//document.getElementById("phase3link").href="#";
		//document.getElementById("phase3link").onclick=delegate(this,this.generatePreview);
	},

	generatePhase4:function()
	{
		this.generatePhase1();
		this.generatePhase2();
		this.generatePhase3();
		document.getElementById("phase4").className="activeSection";
		document.getElementById("phase4link").href="#";
		document.getElementById("phase4link").onclick=delegate(this,this.generateAddPhones);
	},

	getCompaniesList:function()
	{
		this.compQuery=new WebEngine();
		var query=createXmlElement("getCompaniesList");
		this.compQuery.handleXML=delegate(this,this.parseCompaniesList);
		this.compQuery.postRequest(query,"Loading Companies");
	},

	parseCompaniesList:function()
	{

		if(this.compQuery.responseXML !=null) {
			var companies=this.compQuery.responseXML.selectSingleNode("//getCompaniesListResponse");
			var companiesList=companies.getElementsByTagName("company");

			for(var i=0; i < companiesList.length;i++) {
				this.companiesList.push(companiesList[i].getAttribute("companyName"));
			}
		}
		this.getCountriesList();
	},

	getCountriesList:function()
	{

		this.countryQuery=new WebEngine();
		var query=createXmlElement("getCountriesList");
		this.countryQuery.handleXML=delegate(this,this.parseCountriesList);
		this.countryQuery.postRequest(query,"Loading Countries");
	},

	parseCountriesList:function()
	{

		if(this.countryQuery.responseXML !=null) {
			var countries=this.countryQuery.responseXML.selectSingleNode("//getCountriesListResponse");
			var countriesList=countries.getElementsByTagName("country");
			for(var i=0; i < countriesList.length;i++) {
				this.countriesIdList.push(countriesList[i].getAttribute("id"));
				this.countriesList.push(countriesList[i].getAttribute("name"));
				this.countriesCodeList.push(countriesList[i].getAttribute("code"));
			}
		}
		this.generateForm1();
	},

	generateForm1:function()
	{
		//alert("HEre we go phase 1");
		if(this.companiesList.length==0) {
		//	this.getCompaniesList();
		}else{

			//this.generatePhase1();
			document.getElementById("formPanel").innerHTML="";
			document.getElementById("instructionsHeader").innerHTML="Important Information";
			document.getElementById("instructionsPanel").innerHTML='<h3>Please Check that your Mobile Phone is WAP (Wireless Application Protocol) enabled</h3><p><strong>If you are unsure and need further help contact your Network Supplier who will be able to assist you.<br /><br /> You will also find some useful links on our <br /> \'<a href="faq.php">Having Problems</a>\' page.</strong></p><br /><p>If you would like assistance setting up your shift pattern or installing the MobileRota App then please contact us at <a href="mailto:support@mobilerota.com" />support@mobilerota.com</a><br /><br /><strong>We will be only too pleased to help you.</strong></p>';
			document.getElementById("info2").innerHTML='<p><span style="color:#FF0000;">Fields shown in red are mandatory</span>, and must be supplied to complete the registration process.</p><p><strong>User Details</strong></p><p>We use these details purely to supply you with the MobileRota software.  Any personal information is securely encrypted, and will not be used to send unsolicited calls, texts or emails.  We will not share this information with any third parties<br />(see our <a href="privacy.php">Privacy Policy</a> for more information).</p>';
			document.getElementById("instructionsHeader").style.display="block";
			document.getElementById("instructionsPanel").style.display="block";
			document.getElementById("terms").style.display="none";
			document.getElementById("specialInstructions").style.display="none";
			this.formObject=new FormObject(this,document.getElementById("formPanel"));
			this.formObject.form.submitText="Next";
			this.formObject.form.method="POST";
			this.formObject.form.close=false;
			this.formObject.form.action=delegate(this,this.submitUserForm1);

			var fieldset=this.formObject.form.addFieldset("User","User Details","Please Enter Your Details");
			var item=this.formObject.form.addItem(fieldset);
			item.parent=fieldset;
			item.type="text";
			item.id="fname";
			item.label="First Name";
			item.value=this.fname;
			item.required=true;

			var item=this.formObject.form.addItem(fieldset);
			item.parent=fieldset;
			item.type="text";
			item.id="lname";
			item.label="Last Name";
			item.value=this.lname;
			item.required=true;

			var item=this.formObject.form.addItem(fieldset);
			item.parent=fieldset;
			item.type="test";
			item.id="postcode";
			item.label="Zip/Post Code";
			item.value=this.postcode;
			item.required=true;

			var item=this.formObject.form.addItem(fieldset);
			item.parent=fieldset;
			item.type="select";
			item.id="country";
			item.label="Country";
			item.optVal=this.countriesIdList.join();
			item.optDisp=this.countriesList.join();
			item.value=this.country;
			item.jsfunction=delegate(this,this.showCountryCode);
			item.required=true;


			var fieldset2=this.formObject.form.addFieldset("user2",null,"Please Enter Your Mobile Phone Details");
				var item=this.formObject.form.addItem(fieldset2);
				item.parent=fieldset;
				item.type="select";
				item.id="mobileType";
				item.label="What type of phone do you have?";
				item.optVal=this.phoneTypeIdList.join();
				item.optDisp=this.phoneTypeList.join();
				item.jsfunction=delegate(this,this.checkPhoneType);
				item.required=true;

				var item=this.formObject.form.addItem(fieldset2);
				item.parent=fieldset;
				item.type="mobileNumber";
				item.id="mobileNumber";
				item.label="Mobile Number";
				item.value=this.mobileNumber;
				item.required=true;

				var item=this.formObject.form.addItem(fieldset2);
				item.parent=fieldset;
				item.type="mobileNumber";
				item.id="mobileNumber2";
				item.label="Re-enter Mobile Number";
				item.value=this.mobileNumber2;
				item.required=true;

				var fieldset3=this.formObject.form.addFieldset("user3",null,"Please Enter Your Login Details");
				var item=this.formObject.form.addItem(fieldset3);
				item.parent=fieldset;
				item.type="email";
				item.id="email";
				item.label="Email Address";
				item.value=this.email;
				item.required=true;

				var item=this.formObject.form.addItem(fieldset3);
				item.parent=fieldset;
				item.type="email";
				item.id="email2";
				item.label="Re-enter Email Address";
				item.value=this.email;
				item.required=true;

		if(this.editMode ==false) {


				var item=this.formObject.form.addItem(fieldset3);
				item.parent=fieldset;
				item.type="password";
				item.id="password";
				item.label="Password";
				item.value=this.password;
				item.required=true;

				var item=this.formObject.form.addItem(fieldset3);
				item.parent=fieldset;
				item.type="password";
				item.id="password2";
				item.label="Re-enter Password";
				item.value=this.password2;
				item.required=true;
			}

			var fieldset4=this.formObject.form.addFieldset("user4",null,"Please Enter Your Company Name");

			var item=this.formObject.form.addItem(fieldset4);
			item.parent=fieldset;
			item.type="autoComplete";
			item.id="companyName";
			item.label="Company/Organisation";
			item.value=this.companyName;
			item.optVal=this.companiesList.join();
			item.required=true;

			if(this.editMode ==false) {
				var fieldset5=this.formObject.form.addFieldset("user5","Security","To stop automated registration attempts, please enter the four-character security code shown in the graphic");
				var item=this.formObject.form.addItem(fieldset5);
				item.parent=fieldset;
				item.type="captcha";
				item.id="security";
				item.label="Security Code";
				item.value="";
				item.required=true;
			}

			var fieldset6=this.formObject.form.addFieldset("user6","Subscribe To The MobileRota Newsletter","Please uncheck the box if you would  prefer <strong style='font-size:11px;'>not</strong> to receive our quarterly newsletter.");
			var item=this.formObject.form.addItem(fieldset6);
			item.parent=fieldset;
			item.type="checkbox";
			item.id="subscribe";
			item.label="Subscribe To Newsletter";
			item.checked=this.subscribe;
			item.required=false;

			this.formObject.drawForm();
		    this.formObject.showForm();

			if(this.editMode==false) {
				//showMask();
				//this.launchWapWarning();
			}
		}
	},
	checkPhoneType:function()
	{
		var selected=document.getElementById("mobileType").selectedIndex;
		this.phoneType=this.phoneTypeIdList[selected];
		this.phoneSelected=true;
		if(this.phoneType.toLowerCase()=="iphone") {
			if(confirm("MobileRota does not yet support the IPhone\nDo you wish to continue registering so that we can inform you when an IPhone version is made available?")==false){
				alert("Thank you for visiting MobileRota.");
				window.location="index.php";
			}
		}
	},

	showCountryCode:function()
	{
		var selected=document.getElementById("country").selectedIndex;
		this.countryCode=this.countriesCodeList[selected];
		if(document.getElementById("mobileNumberCode") !=null) {
			document.getElementById("mobileNumberCode").value=this.countriesCodeList[selected];
			document.getElementById("mobileNumber2Code").value=this.countriesCodeList[selected];
		}
	},

	submitUserForm1:function()
	{
		this.fname=document.getElementById("fname").value;
		this.lname=document.getElementById("lname").value;
		this.mobileNumber=document.getElementById("mobileNumber").value;
		this.mobileNumber2=document.getElementById("mobileNumber2").value;
		this.companyName=document.getElementById("companyName").value;
		this.email=document.getElementById("email").value;
		this.email2=document.getElementById("email2").value;

		if(this.phoneSelected==false) {
			alert("You must select a phone type");
			return
		}
		if(this.editMode==false) {

			this.password=document.getElementById("password").value;
			this.password2=document.getElementById("password2").value;
		}
		var subsc=document.getElementById("subscribe").checked;
		if(subsc==false) {
			this.subscribe="false";
		}else{
			this.subscribe="true";
		}
		this.postcode=document.getElementById("postcode").value;
		this.country=document.getElementById("country").value;
		if(this.editMode==false) {
			this.security=document.getElementById("security").value;
		}


		if(this.mobileNumber != this.mobileNumber2) {
			alert("The two mobile Numbers that you have entered do not match");
			return
		}

		if(this.email != this.email2) {
			alert("The two email addresses that you have entered do not match");
			return
		}

		if(this.password==this.password2) {
			if(this.editMode==false) {
				this.sendForm1();
			}else{
				this.updateForm1();
			}
		}else{
			alert("the two passwords that you have entered do not match");
		}
	},



	sendForm1:function()
	{
		if(this.mobileNumber=="5678") {
			this.debugMode=true;
			this.userAdded();
		}else{
			this.formObject.closeForm();
			this.userQuery=new WebEngine();

			var query=createXmlElement("addUser");
			query.setAttribute("fname",this.fname);
			query.setAttribute("lname",this.lname);
			query.setAttribute("mobile",this.mobileNumber);
			query.setAttribute("phoneType",this.phoneType);
			query.setAttribute("email",this.email);
			query.setAttribute("password",this.password);
			query.setAttribute("subscribe",this.subscribe);
			query.setAttribute("postcode",this.postcode);
			query.setAttribute("country",this.country);
			query.setAttribute("countryCode",this.countryCode);
			query.setAttribute("security",this.security);
			query.setAttribute("companyName",this.companyName);

			this.userQuery.handleXML=delegate(this,this.userAdded);
			this.userQuery.postRequest(query,"Loading User Information");
		}
	},

	updateForm1:function()
	{
		this.formObject.closeForm();
		this.userQuery=new WebEngine();

		var query=createXmlElement("updateUser");
		query.setAttribute("userId",this.userId);
		query.setAttribute("fname",this.fname);
		query.setAttribute("lname",this.lname);
		query.setAttribute("mobile",this.mobileNumber);
		query.setAttribute("email",this.email);
		query.setAttribute("subscribe",this.subscribe);
		query.setAttribute("postcode",this.postcode);
		query.setAttribute("country",this.country);
		query.setAttribute("companyName",this.companyName);

		this.userQuery.handleXML=delegate(this,this.userUpdated);
		this.userQuery.postRequest(query,"Updating User Information");
	},

	userUpdated:function() {
		var user=this.userQuery.responseXML.selectSingleNode("//updateUserResponse");
		this.parseResults(user);
		if(this.terms==true) {
			this.termsAccepted();
		}else{
			alert("go to terms");
		}
	},

	userAdded:function()
	{

		if(this.userQuery !=null) {
			if(this.userQuery.responseXML.selectSingleNode("//CompositeResponse").getAttribute("errorCode")=="10") {
				alert("Our records show that a user with either this phone number, or email address, is already registered.");
				return;
			}
			if(this.userQuery.responseXML.selectSingleNode("//CompositeResponse").getAttribute("errorCode")=="8") {
				alert("Invalid Security Code\nPlease Try Again");
				return;
			}
			if(this.userQuery.responseXML.selectSingleNode("//CompositeResponse").getAttribute("errorCode")=="6") {
				alert("Server Error");
				return;
			}
			document.getElementById("phase1").className="activeSection";
			document.getElementById("phase1link").href="#";
			document.getElementById("phase1link").innerHTML="Edit Your Details";
			document.getElementById("phase1link").onclick=delegate(this,this.generateForm1);
			this.userId=this.userQuery.responseXML.selectSingleNode("//addUserResponse").getAttribute("userID");
		}
		this.generatePhase2();
		document.getElementById("formPanel").innerHTML="";
		document.getElementById("instructionsHeader").style.display="none";
		document.getElementById("instructionsPanel").style.display="none";
		document.getElementById("terms").style.display="block";
		this.formObject=new FormObject(this,document.getElementById("formPanel"));
		this.formObject.form.submitText="Next";
		this.formObject.form.method="POST";
		this.formObject.form.close=false;
		this.formObject.form.action=delegate(this,this.submitTermsForm);
		var fieldset=this.formObject.form.addFieldset("User","Terms And Conditions","Please Check the select box to accept our terms and conditions, we will then send a small application to your phone which will display a five character code, please enter this into the confirmation box on the next form");

		var item=this.formObject.form.addItem(fieldset);
		item.parent=fieldset;
		item.type="checkbox";
		item.id="termsField";
		item.label="I accept the terms and conditions";
		item.checked=this.terms;
		item.required=false;
		this.formObject.drawForm();
	    this.formObject.showForm();
	},

	submitTermsForm:function()
	{
		this.terms=document.getElementById("termsField").checked;
		if(this.debugMode==false) {
			if(this.terms==true) {

				var termsQuery=new WebEngine();
				var query=createXmlElement("setTermsAccepted");
				query.setAttribute("userId",this.userId);
				termsQuery.handleXML=delegate(this,this.termsAccepted);
				termsQuery.postRequest(query,"Loading User Information");
			}
		}else{
			this.termsAccepted();
		}
	},




	termsAccepted:function()
	{

			this.companyQuery=new WebEngine();
			var query=createXmlElement("findCompany");
			query.setAttribute("companyName",this.companyName);
			this.companyQuery.handleXML=delegate(this,this.getCompanyShifts);
			this.companyQuery.postRequest(query,"Loading User Information");
	},

	doNothing:function() {


	},

	getCompanyShifts:function()
	{

		this.companyQuery=new WebEngine();
		var query=createXmlElement("findShifts");
		query.setAttribute("companyName",this.companyName);
		this.companyQuery.handleXML=delegate(this,this.parseShifts);
		this.companyQuery.postRequest(query,"Loading Shift Information");
	},

	parseShifts:function()
	{
		this.companyShiftArray=new Array();
		this.shiftsIdArray=new Array();
		this.shiftsNameArray=new Array();
		this.shiftsIdArray.push("null");
			this.shiftsNameArray.push("Generate New Shift Pattern");
		if(this.companyQuery.responseXML) {
			var shifts=this.companyQuery.responseXML.getElementsByTagName("pattern");
			var shiftArray=new Array();
			for(var s=0 ;s < shifts.length; s++) {
				this.shiftsIdArray.push(shifts[s].getAttribute("patternId"));
				this.shiftsNameArray.push(shifts[s].getAttribute("patternname"));
			}/*End For Loop*/

		}
		this.generatePreview();
	},


	generatePreview:function(existingPattern)
	{
		this.generatePhase3();
		document.getElementById("formPanel").innerHTML="";
		document.getElementById("instructionsHeader").style.display="none";
		document.getElementById("instructionsPanel").style.display="none";
		document.getElementById("terms").style.display="none";
		document.getElementById("specialInstructions").style.display="none";
		launchShiftHelp();
		var previa=new Preview();
		//prev.setStartDate(this.startDate);
		previa.setParent(document.getElementById("formPanel"));
		previa.demoMode=false;
		previa.existingPattern=existingPattern;
		previa.userId=this.userId;
		previa.companyName=this.companyName;
		previa.parentForm=this;
		previa.userId=this.userId;
		previa.country=this.country;
		previa.shiftsId=this.shiftsIdArray;
		previa.shiftsNames=this.shiftsNameArray;
		var date=new Date();
		date.setDate(1);
		previa.startDate=convertDatetoString(date);
		previa.patternName=this.patternName;
		if(this.pattern !=null) {
			previa.pattern=this.pattern.split("");
		}else{
			previa.pattern=new Array("X","X","X","X","X","X","X","X");
		}
		previa.generatePreview();
	},

	shiftPatternSubmitted:function() {
		this.sendQuery=new WebEngine();
		var query=createXmlElement("sendMobileRota");
		query.setAttribute("country",this.country);
		query.setAttribute("mobileNumber",this.mobileNumber);
		query.setAttribute("userId",this.userId);
		this.sendQuery.handleXML=delegate(this,this.phoneSent);
		this.sendQuery.postRequest(query,"Sending MobileRota");
	},

	phoneSent:function() {
		this.sendQuery=null;
		window.scrollTo(0,0);
		launchThankyouWarning(this.email,this.password);
	},

	generateThankyou:function() {
		window.location="thankyou2.php";
	}







});



function noWapEdit() {
	hideWarning();
}

function noWapRegister() {
	window.location="index.php";
}
