/*
 * Copyright 2008 Web Service Solutions, Inc.  All rights reserved.
 */
/*jslint evil: true */

var syncSWorld = new SyncSWorld();

function initPage() {

	// focus
	var user = document.getElementById("firstID");
	user.focus();
	syncSWorld.startup();

	var localeSelect = document.getElementById("localeID");

	var tmpSort = [];
	var localKey = null;
	for (localeKey in jsLocaleData) {
		if (jsLocaleData.hasOwnProperty(localeKey)) {
			tmpSort.push(localeKey);
		}
	}
	function localeComparator(a, b) {
		var x = jsLocaleData[a].displayLanguageUS.toLowerCase();
		var y = jsLocaleData[b].displayLanguageUS.toLowerCase();
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	}
	tmpSort.sort(localeComparator);

	var i=0;
	var option = null;
	for (; i < tmpSort.length; ++i) {
		localeKey = tmpSort[i];
		var locale = jsLocaleData[localeKey];
		var display = locale.displayLanguageUS;
		if (locale.displayCountryUS.length > 0) {
			display += " ";
			display += locale.displayCountryUS;
		}
		var left = display;
		display += " / " + locale.displayLanguage;
		var right = locale.displayLanguage;
		if (locale.displayCountry.length > 0) {
			display += " ";
			display += locale.displayCountry;
			right += " " + locale.displayCountry;
		}
		if (left == right) {
			display = left;
		}
	
		//localeOptions.push( { label: display, value: localeKey });
		option = document.createElement('option');
		option.setAttribute('value', localeKey);
		if (localeKey == "en-CA") {
			option.setAttribute('selected', 'selected');
		}
		option.appendChild(document.createTextNode(display));
		localeSelect.appendChild(option);
	}

	// timezone
	var tmp = [];
	for (var timeZoneID in swTZdata.zoneM) {
		if (swTZdata.zoneM.hasOwnProperty(timeZoneID)) {
			tmp.push(timeZoneID);
		}
	}
	tmp.sort();
	var tzSelect = document.getElementById("timeZoneID");
	for (i=0; i < tmp.length; ++i) {
		var tz = tmp[i];
		option = document.createElement('option');
		option.setAttribute('value', tz);
		if (tz == "America/Toronto") {
			option.setAttribute('selected', 'selected');
		}
		option.appendChild(document.createTextNode(tz));
		tzSelect.appendChild(option);
	}

	// get the license.txt file
	this.getLicenseCallback = function(answer) {
		
		if (!answer.success) {
			alert(opener.swApp.i18n("Failed to get license.txt."));
			return;
		}
		//this.purchaseTextArea.setValue(answer.text);
	    var license = document.getElementById("license");
        license.value = answer.text;
	};
	
	var url = "/tg/license.txt";
	var rpc = new DlRPC( {
		url			: url,
		callback	: this.getLicenseCallback.$(this)
	} );
	rpc.call();

}

function handleResult(answer) {

	if (!answer.success) {
		alert("Communicating with the server failed.");
		return;
	}

	var response = eval("(" + answer.text + ")");
	if (response.message == "Success") {
		alert("You have been emailed your password.");
		syncSWorld.wipeStorage();
		var swCookie = {};
		var email = document.getElementById("emailID");
		swCookie.calUserID = email.value;
		syncSWorld.saveObject(syncSWorld.SW_COOKIE, swCookie);
		var url = "indexDev.html";
		var hrefLC = window.location.href.toLowerCase();
		if (hrefLC.indexOf("scheduleworld") >= 0) {
			url = "index.html";
		}
		window.location = url;
	}
	else if (response.failedI18nKey) {
		alert(response.failedI18nKey);
	}
	else if (response.message) {
		alert("Failed to create user:" + response.message);
	}
	else {
		alert("Failed to create user.");
	}
}

function iAgree() {
	// MUST have a first name
	var first = document.getElementById("firstID");
	if ( (!first) || first.value === "") {
		alert("First name is required.");
		return false;
	}

	// MUST have a last name
	var last = document.getElementById("lastID");
	if ( (!last) || last.value === "") {
		alert("Last name is required.");
		return false;
	}

	// MUST have an email
	var emailID = document.getElementById("emailID");
	if ( (!emailID) || emailID.value === "") {
		alert("Email is required.");
		return false;
	}
	if (emailID.value.length < 5 || emailID.value.indexOf("@") < 1) {
		alert("Improperly formatted email address.");
		return false;
	}

	// 1. createUser
	var url = "/sw2/commandV3";
	var command = {};
	command.email = emailID.value;
	command.firstName = first.value;
	command.lastName = last.value;
	var tzSelect = document.getElementById("timeZoneID");
	command.timeZoneID = tzSelect.value;
	var localeSelect = document.getElementById("localeID");
	command.localeKey = localeSelect.value;
	var xml = Sarissa.xmlize(command, "createNewUser");

	var agreeButton = document.getElementById("AgreeID");
	agreeButton.style.display = "none";
	var rpc = new DlRPC( {
		url			: url,
		callback	: handleResult,
		data		: xml } );
	rpc.call();

	// 2. send back to the home page on success

	// 3. or display the error message.

}

