/*
Spot Image SYRIUS Online - JS file 
Copyright (c) 2005-2006 WS Interactive
Author: Sebastien Pauchet <sebastien.pauchet@ws-interactive.fr>
Created : 02/03/2006
$Id: login.api.js,v 1.4 2007/09/25 16:12:32 sebastien Exp $
*/
var onloadFunctions=new Array();
onloadFunctions[0]='xmlTreatLoginParams';
onloadFunctions[1]='addButtonEvents';
var xmlPageDefinition="Interface/Ajax/XMLPageLogin.ashx";
/**
  * Declare all alerts vars
  */
var loginDefinitions = new Array();
var hasAddressField = false;

/**********************************************
* XML FUNCTIONS (prefs specific)              *
**********************************************/ 

/* Display alerts*/
function xmlTreatLoginParams(params) {
	//Preferences
	loadPreference(params);
	//Languages
	loadLanguage(params);
	//Menu
	loadMenu(params);
	//Login detail
	if (params.getElementsByTagName('profilperso').length > 0) {
		var profilParamsObjects = params.getElementsByTagName('profilperso').item(0).childNodes;
		loadXMLAccount(profilParamsObjects);
		//show accountLoginTable
		show('accountLoginTable');
	}
	//User message
	if (params.getElementsByTagName('userMessage').length > 0) {
		var userMessage = params.getElementsByTagName('userMessage').item(0);
		userConfirm(userMessage);
	}
	if (userPreference['authenticateduser'] != 'true') {
		//show user login
		show('loginTable');
		//show login nav
		show('loginNav');
		//show table head
		getE('accountName').innerHTML = applicationMessages['identification'].toUpperCase() + '<span>' + applicationMessages['createsiriusaccount'] + '</span>';
	} else {
		//show nav
		show('accountNav');
		//show subnav
		show('accountSubNav');
		//show table head
		getE('accountName').innerHTML = applicationMessages['personalinfos'].toUpperCase() + '<span>' + applicationMessages['yoursiriusaccount'] + '</span>';
		//show delete button
		show(getE('deleteAccount'));
	}
	//add some events on menu for IE compatibility only
	startMenu();
	return true;
}

/*Load an account detail*/
function loadXMLAccount(params) {
	var profilDetailFields = params;
	if (profilDetailFields.length > 0) {
		var used = false;
		for (var m = 0; m < profilDetailFields.length; m++) {
			if (typeof profilDetailFields[m].tagName != "undefined") {
				switch (profilDetailFields[m].tagName) {
					//common fields type
					case "textbox":
						used = populateTextBox(profilDetailFields[m].getAttribute('id'),profilDetailFields[m]);
					break;
					case "listbox":
						used = populateListBox(profilDetailFields[m].getAttribute('id'),profilDetailFields[m]);
					break;
					case "radiobutton":
						used = populateRadioButton(profilDetailFields[m].getAttribute('id'),profilDetailFields[m]);
					break;
					case "addresses":
						used = loadXMLAddresses(profilDetailFields[m]);
						//show addressFields
						show('addressFields');
						hasAddressField = true;
					break;
				}
			}
		}
	}
	return true;
}
/*Load an account detail*/
function loadXMLAddresses(params) {
	var addressDetailFields = params.getElementsByTagName('address').item(0).childNodes;
	var addressID = params.getElementsByTagName('address').item(0).getAttribute('id');
	var addressType = params.getElementsByTagName('address').item(0).getAttribute('type');
	getE('addressID').value = addressID;
	getE('addressType').value = addressType;
	if (addressDetailFields.length > 0) {
		var used = false;
		for (var n = 0; n < addressDetailFields.length; n++) {
			if (typeof addressDetailFields[n].tagName != "undefined") {
				switch (addressDetailFields[n].tagName) {
					//common fields type
					case "textbox":
						used = populateTextBox(addressDetailFields[n].getAttribute('id'),addressDetailFields[n]);
					break;
					case "listbox":
						used = populateListBox(addressDetailFields[n].getAttribute('id'),addressDetailFields[n]);
					break;
					case "radiobutton":
						used = populateRadioButton(addressDetailFields[n].getAttribute('id'),addressDetailFields[n]);
					break;
				}
			}
		}
	}
	return true;
}
/**********************************************
* Sensitive IO FUNCTIONS  (alerts specific)   *
**********************************************/
/* Return label of a given field ID (use label tag field delimiter)
 * can use a DOM element as parameter instead of id to point an out window object
 */
function getFieldName(id) {
	if (typeof(id) == 'string') {
		if (!getE(id)) {
			pr("Unknown element ID in document : " + id);
			return false;
		}
		var element = getE(id);
	} else {
		var element = id;
	}
	var label = '';
	if (getE('label'+element.id)) {
		label = getE('label'+element.id).innerHTML;
	}
	label += (element.previousSibling && element.previousSibling.nodeName=="#text") ? element.previousSibling.nodeValue + ' ':'';
	if (element.tagName == 'SELECT') {
		label += (element.selectedIndex != -1) ? element.options[element.selectedIndex].text + ' ':'-- ';
	} else {
		if (element.id == 'telefon') {
			label += (getE('telefonsuffix').value) ? getE('telefonsuffix').value + ' ':'-- ';
		}
		if (element.id == 'fax') {
			label += (getE('faxsuffix').value) ? getE('faxsuffix').value + ' ':'-- ';
		}
		label += (element.value) ? element.value + ' ':'-- ';
	}
	label += (element.nextSibling && element.nextSibling.nodeName=="#text") ? element.nextSibling.nodeValue:'';
	return label;
}
/**********************************************
* BUTTONS FUNCTIONS                           *
* Functions used when a button is clicked     *
**********************************************/
/*enter login*/
function buttonLogin() {
	//first check fields values
	var incorrect = new Array();
	var unfilled = new Array();
	//username
	if (!getE('username').value) {
		unfilled[unfilled.length] = 'username';
	} else if (!isEmail(getE('username').value)) {
		incorrect[incorrect.length] = 'username';
	}
	//password
	if (!getE('password').value) {
		unfilled[unfilled.length] = 'password';
	}
	//Then display errors messages if any or submit form
	if (incorrect.length == 0 && unfilled.length == 0) {
		//all is good
		
		/**** Commenté par ALI (marche pas)*****/
		//if (isIE) {
			//force form autocomplete save fields for IE to avoid bugs
			//window.external.AutoCompleteSaveForm(getE('loginForm'));
		//}
		
		
		//submit form
		getE('loginForm').submit();
		return true;
	} else {
		//some fields are not good, so inform user
		var message = '';
		var label;
		var regReplace=new RegExp("(%s)");
		if (incorrect.length > 0) {
			for (var n = 0; n < incorrect.length; n++) {
				label = getFieldName(incorrect[n]);
				message += applicationMessages['incorrectfieldformat'].replace(regReplace,label) + '<br />';
				highlightField(incorrect[n]);
			}
		}
		if (unfilled.length > 0) {
			for (var n = 0; n < unfilled.length; n++) {
				label = getFieldName(unfilled[n]);
				message += applicationMessages['entervalue'].replace(regReplace,label) + '<br />';
				highlightField(unfilled[n]);
			}
		}
		userMessage(message,true);
		return true;
	}
}
/*password forgotten*/
function forgetPassword() {
	//first check fields values
	var incorrect = new Array();
	var unfilled = new Array();
	var regReplace=new RegExp("(%s)");
	//username
	if (!getE('username').value) {
		unfilled[unfilled.length] = 'username';
	} else if (!isEmail(getE('username').value)) {
		incorrect[incorrect.length] = 'username';
	}
	//Then display errors messages if any or submit form
	if (incorrect.length == 0 && unfilled.length == 0) {
		if (confirm(applicationMessages['forgetpassword'].replace(regReplace,getE('username').value))) {
			//all is good
			getE('action').value = "forgetPassword";
			//submit form
			getE('loginForm').submit();
		}
		return true;
	} else {
		//some fields are not good, so inform user
		var message = '';
		var label;
		if (incorrect.length > 0) {
			for (var n = 0; n < incorrect.length; n++) {
				label = getFieldName(incorrect[n]);
				message += applicationMessages['incorrectfieldformat'].replace(regReplace,label) + '<br />';
				highlightField(incorrect[n]);
			}
		}
		if (unfilled.length > 0) {
			for (var n = 0; n < unfilled.length; n++) {
				label = getFieldName(unfilled[n]);
				message += applicationMessages['entervalue'].replace(regReplace,label) + '<br />';
				highlightField(unfilled[n]);
			}
		}
		userMessage(message,true);
		return true;
	}
}
/*edit/create current account*/
function buttonCreateAccount() {
	//first check fields values
	var incorrect = new Array();
	var unfilled = new Array();
	//fieldCivility
	var fieldsCivility = document.getElementsByName('fieldCivility');
	var civilityChecked = false;
	for (var n = 0; n < fieldsCivility.length; n++) {
		civilityChecked = (fieldsCivility[n].checked == true) ? true : civilityChecked;
	}
	if (!civilityChecked) {
		unfilled[unfilled.length] = 'fieldCivility';
	}
	//firstname
	if (!getE('firstname').value) {
		unfilled[unfilled.length] = 'firstname';
	}
	//lastname
	if (!getE('lastname').value) {
		unfilled[unfilled.length] = 'lastname';
	}
	//email
	if (!getE('emailcreate').value) {
		unfilled[unfilled.length] = 'emailcreate';
	} else if (!isEmail(getE('emailcreate').value)) {
		incorrect[incorrect.length] = 'emailcreate';
	}
	//fieldLanguage
	if (getE('fieldLanguage').value == '0') {
		unfilled[unfilled.length] = 'fieldLanguage';
	}
	//telefon
	if (getE('telefon').value && (!isPhone(getE('telefon').value) || !isPhone(getE('telefonsuffix').value))) {
		incorrect[incorrect.length] = 'telefon';
	}
	//fax
	if (getE('fax').value && (!isPhone(getE('fax').value) || !isPhone(getE('faxsuffix').value))) {
		incorrect[incorrect.length] = 'fax';
	}
	if (hasAddressField/* && !getE('clientnumber').value*/) { //DM20509
		//company
		if (!getE('company').value) {
			unfilled[unfilled.length] = 'company';
		}
		//address1
		if (!getE('address1').value) {
			unfilled[unfilled.length] = 'address1';
		} else if (getE('address1').value.length > 50) {
			incorrect[incorrect.length] = 'address1';
		}
		//address2
		if (getE('address2').value && getE('address2').value.length > 50) {
			incorrect[incorrect.length] = 'address2';
		}
		//address3
		if (getE('address3').value && getE('address3').value.length > 50) {
			incorrect[incorrect.length] = 'address3';
		}
		//city
		if (!getE('city').value) {
			unfilled[unfilled.length] = 'city';
		}
		//fieldCountry
		if (getE('fieldCountry').value == '0') {
			unfilled[unfilled.length] = 'fieldCountry';
		}
	}
	//password1
	if (!getE('password1').value) {
		unfilled[unfilled.length] = 'password1';
	}
	//password2
	if (!getE('password2').value) {
		unfilled[unfilled.length] = 'password2';
	}
	if (getE('password1').value && getE('password2').value) {
		//password1 == password2 and length >= 6 characters
		if (getE('password1').value != getE('password2').value) {
			incorrect[incorrect.length] = 'password1';
			incorrect[incorrect.length] = 'password2';
		} else if (getE('password1').value.length < 6) {
			incorrect[incorrect.length] = 'password1';
			incorrect[incorrect.length] = 'password2';
		}
	}
	//Then display errors messages if any or submit form
	if (incorrect.length == 0 && unfilled.length == 0) {
		//all is good
		
		//submit form
		getE('accountLoginForm').submit();
		return true;
	} else {
		//some fields are not good, so inform user
		var message = '';
		var label;
		var regReplace=new RegExp("(%s)");
		if (incorrect.length > 0) {
			for (var n = 0; n < incorrect.length; n++) {
				label = getFieldName(incorrect[n]);
				if (incorrect[n] == 'address1' || incorrect[n] == 'address2' || incorrect[n] == 'address3') {
					message += applicationMessages['incorrectfieldformat'].replace(regReplace,label) + ' ' + applicationMessages['incorrectaddressformat'] + '<br />';
				} else {
					message += applicationMessages['incorrectfieldformat'].replace(regReplace,label) + '<br />';
				}
				if (incorrect[n] == 'telefon') {
					highlightField(incorrect[n]);
					highlightField('telefonsuffix');
				} else if (incorrect[n] == 'fax') {
					highlightField(incorrect[n]);
					highlightField('faxsuffix');
				} else {
					highlightField(incorrect[n]);
				}
			}
		}
		if (unfilled.length > 0) {
			for (var n = 0; n < unfilled.length; n++) {
				if (unfilled[n] == 'fieldCivility') {
					label = getE('labelfieldCivility').innerHTML + ' -- ';
				} else {
					label = getFieldName(unfilled[n]);
				}
				message += applicationMessages['entervalue'].replace(regReplace,label) + '<br />';
				if (unfilled[n] == 'telefon') {
					highlightField(unfilled[n]);
					highlightField('telefonsuffix');
				} else if (unfilled[n] == 'fax') {
					highlightField(unfilled[n]);
					highlightField('faxsuffix');
				} else {
					highlightField(unfilled[n]);
				}
			}
		}
		userMessage(message,true);
		return true;
	}
}
/*delete current account*/
function buttonDeleteAccount() {
	//need confirmation then, send to deleteAccount function
	userMessage(applicationMessages['confirmaccountdeletion'],'deleteAccount()');
}
function deleteAccount() {
	//send form
	getE('action').value = "deleteAccount";
	//submit form
	getE('loginForm').submit();
}

