var errors, action, edtImage, edtName, edtSurname, edtEmail, edtCell, edtPhone, hddnUserCode, edtPwd, edtConfirm, cbAgree, selAddress, selPosition;
var module = baseRootURL + "user/";

var addressItem = new addSelectionList('addressItem', 'address', 'addressElement');

function joinUser()
{
	"use strict";
	errors = false;
	action = 6; 
	
	validate_edtName();
	validate_edtSurname();
	validate_edtEmail();
	validate_edtBirthDate();
	validate_edtCellPhone();
	validate_edtPwd(1);
	validate_edtConfirm();
	
	if (!errors)
	{
		validate_cbAgree();
		if (!errors)
		{
			var data = {};
			data.dummy 			= ""; //we seem to get errors from the first value, so always add a dummy value first.
			data.action 		= action;
			data.edtName 		= edtName;
			data.edtSurname		 = edtSurname;
			data.edtEmail 		= edtEmail;
			data.edtCell 		= edtCell;
			data.edtPhone 		= edtPhone;
			data.edtBirthDate	= edtBirthDate;
			data.edtPwd 		= edtPwd;
			data.edtConfirm 	= edtConfirm;
			callModule("user/user_val.php", data);
		}
	}	
}

function registerUser(postAction)
{
	errors = false;
	action = postAction;
	
	validate_edtImage();
	validate_edtName();
	validate_edtSurname();
	validate_edtEmail();
	validate_selPosition();
	validate_edtCellPhone();
	validate_hddnUserCode();
	validate_edtPwd(action);
	validate_edtConfirm();
	
	checkAddresses();

	if (!errors)
	{
		var data = {};
		data.dummy 				= ""; //we seem to get errors from the first value, so always add a dummy value first.
		data.action 			= action;
		data.edtImage 			= edtImage;
		data.edtName 			= edtName;
		data.edtSurname 		= edtSurname;
		data.edtEmail 			= edtEmail;
		data.edtCell 			= edtCell;
		data.edtPhone 			= edtPhone;
		data.selPosition 		= selPosition;
		data.addresses  		= addressItem.getItemsAsString();
		if (document.getElementById('hddnUserCode') !== null) data.hddnUserCode = hddnUserCode;
		data.edtPwd 			= edtPwd;
		data.edtConfirm 		= edtConfirm;
		callModule("user/user_val.php", data);
	}
}

function insertUser(){registerUser(0)}

function modifyUser(){registerUser(1)}

function validate_edtImage(){edtImage=validateImage("edtImage", baseRootURL+"media/images/profile/profile.png");}
function validate_edtName(){edtName=validateRequiredText("edtName", "This first name is required.");}
function validate_edtSurname(){edtSurname=validateRequiredText("edtSurname", "The surname is required.");}
function validate_edtEmail(){edtEmail=validateEmail("edtEmail", "The email address is required.");}
function validate_edtCell(){edtCell=validatePhoneNumber("edtCell", "Cell number is required.", false);}
function validate_edtPhone(){edtPhone=validatePhoneNumber("edtPhone", "Phone number is required.", false);}
function validate_selPosition(){selPosition=validateRequiredSelection("selPosition", "Please select a role.");}

function validate_edtBirthDate()
{
	edtBirthDate=validateRequiredText("edtBirthDate", "Please specify the date of birth.");
}

function validate_hddnUserCode(){if (document.getElementById('hddnUserCode') !== null) hddnUserCode=document.getElementById("hddnUserCode").value;}
function validate_edtPwd(action){edtPwd=validatePassword("edtPwd", "edtConfirm", false);}
function validate_edtConfirm(){/*password confirmation taken care of above.*/}
function validate_cbAgree(){cbAgree = validateRequiredCheckbox("cbAgree", "Please agree to our terms & conditions.");}

function validate_edtCellPhone()
{
	validate_edtCell();
	validate_edtPhone();
	if ((!edtCell) && (!edtPhone))
	{
		errors = true;
		handleError("edtCell", "Please provide at least 1 number.");		
	}
}


function validate_edtAddress(){edtAddress=validateRequiredText("edtAddress", "Please enter an address.");}
function checkAddresses(){addressItem.validateItems('0', 'edtAddress', 'address');}
function validate_selAddress(){selAddress = validateRequiredSelection("selAddress", "Please select the address type.");}

function viewUser(id)
{
	var data = {}
	data.dummy = "";
	data.hddnUserCode = id;
	data.action = 2;
	callModule("user/user_val.php", data);
}

function editUser(id)
{
	var data = {}
	data.dummy = "";
	data.hddnUserCode = id;
	data.action = 3;
	callModule("user/user_val.php", data);
}

function copyUser(id, desc)
{
	if (confirm("Are you sure you want to duplicate "+desc+"?"))
	{
	var data = {}
	data.dummy = "";
	data.hddnUserCode = id;
	data.action = 5;
	callModule("user/user_val.php", data);
	}
}

function deleteUser(id, desc)
{
	if (confirm("Are you sure you want to delete "+desc+" and all associated records?"))
	{
	var data = {}
	data.dummy = "";
	data.hddnUserCode = id;
	data.action = 4;
	callModule("user/user_val.php", data);
	}
}

function addAddress()
{
	var se = document.getElementById('selAddress');
	validate_selAddress();
	validate_edtAddress();
	//document.getElementById('selAddress');
	var addType = se.options[se.selectedIndex].text;
	if (se.selectedIndex)
	{
		addressItem.setTag(edtAddress);
		addressItem.setColWidth(6);
		addressItem.setDisplayText(addType+': '+edtAddress);
		addressItem.addItem(se);
	}
}