// JS Objects

function JS_Company(partyId, partyName, branches){
	this.partyId = partyId;
	this.partyName = partyName;
	this.branches = branches;
}
function JS_Branch(branchId, branchName, main, clients){
	this.branchId = branchId;
	this.branchName = branchName;
	this.main = main;
	this.clients = clients;
}
function JS_Client(partyId, partyName, mainBranchId){
	this.partyId = partyId;
	this.partyName = partyName;
	this.mainBranchId = mainBranchId;
}

// JS Util Functions

function changeCompany(f, branchIdSelected, clientPartyIdSelected){
	
	if (f.branchId == null) return;
	
	// 1. Init branches combo and Init clients combo, if it exists
	emptyCombo(f.branchId, select_a_branch_txt);
	if (f.clientPartyId != null) {
		emptyCombo(f.clientPartyId, select_a_client_txt);
	}	
	
	// 2. Retrieve company data
	var company = searchCompany(f.partyId.value);
	if (company == null) { hideInfoDiv('selectionInfo'); return; }	
	
	var selected;
	// 3. Change branches Combo
	for (var i = 0; i < company.branches.length; i++) {
		selected = (((branchIdSelected != null) && (branchIdSelected == company.branches[i].branchId)) || company.branches.length == 1);
		f.branchId.appendChild(createOption(company.branches[i].branchName, company.branches[i].branchId, selected));
	}
	
	if (f.clientPartyId != null) {
		changeBranch(f, clientPartyIdSelected);
	}
	
	showInfo();
}

function changeBranch(f, clientPartyIdSelected){

	// 1. Retrieve company data
	var company = searchCompany(f.partyId.value);
	if (company == null) { emptyCombo(f.clientPartyId, select_a_client_txt);hideInfoDiv('selectionInfo'); return; }

	// 2. Retrieve company data
	var branch = searchBranch(company, f.branchId.value);
	if (branch == null) { emptyCombo(f.clientPartyId, select_a_client_txt);hideInfoDiv('selectionInfo'); return; }
	
	var selected;	
	// 3. Change clients Combo
	if (f.clientPartyId != null) {
		if (branch.clients == null) {
			emptyCombo(f.clientPartyId, no_clients_txt);
			f.clientPartyId.disabled = true;
		} else {  
			emptyCombo(f.clientPartyId, select_a_client_txt);
			for (var i = 0; i < branch.clients.length; i++) {
				selected = (((clientPartyIdSelected != null) && (clientPartyIdSelected == branch.clients[i].partyId)) || branch.clients.length == 1);
				f.clientPartyId.appendChild(createOption(branch.clients[i].partyName, branch.clients[i].partyId , selected));
			}
		}
	}
}

function emptyCombo(combo, txt){
	combo.disabled = false;	
	combo.innerHTML = "";
	combo.appendChild(createOption(txt, ''));
}

function getClientBranchId(partyId, branchId, clientPartyId) {
	var company = searchCompany(partyId);
	if (company == null) return "";
	
	var branch = searchBranch(company, branchId);
	if (branch == null) return "";
	
	// 5. Change clients Combo
	if (clientPartyId != null && branch.clients != null) { 
		for (var i = 0; i < branch.clients.length; i++) {
			if (branch.clients[i].partyId == clientPartyId) {
				return branch.clients[i].mainBranchId;
			} 
		}
	}
	return "";
}

function searchCompany(selectedPartyId) {
	if (companies == null) return; 
	for (var i = 0; i < companies.length; i++) {
		if (companies[i].partyId == selectedPartyId) return companies[i];
	}
	return null;
}

function searchBranch(company, selectedBranchId) {
	if (company == null) return; 
	for (var i = 0; i < company.branches.length; i++) {
		if (company.branches[i].branchId == selectedBranchId) return company.branches[i];
	}
	return null;
}

function searchClient(branch, selectedClientId) {
	if (branch == null) return; 
	for (var i = 0; i < branch.clients.length; i++) {
		if (branch.clients[i].partyId == selectedClientId) return branch.clients[i];
	}
	return null;
}

function createOption(text, value, selected) {
	var op = document.createElement("option");
	op.setAttribute("value", value);
	op.appendChild(document.createTextNode(shrinkTxt(text)));
	if (selected == true) op.selected = 'selected';
	return op;
}

function shrinkTxt(txt) {
	var limit = 45;
	return (txt.length > limit)?((txt.substr(0, limit)) + "..."):txt;
}

function showInfo() {
	var f = document.searchForm;
	var eleToCalculatePos = f.branchId;
	var company = searchCompany(f.partyId.value);
	var branch = null;
	var client = null;
	
	if (f.branchId.value != '') {
		branch = searchBranch(company, f.branchId.value);
	}
	if (f.clientPartyId != null && f.clientPartyId.value != '') {
		client = searchClient(branch, f.clientPartyId.value);
	}
	showInfoDiv('selectionInfo', company, branch, client, f.branchId);
}

function showInfoDiv(divName, company, branch, client) {
	var f = document.searchForm;
	if (company == null) return;
	var selectionInfo = document.getElementById(divName);
	// empty div content
	selectionInfo.innerHTML = "";
	
	// COMPANY
	selectionInfo.innerHTML+= '<p class="title">' + companyTxt + "</p> [Id: " + company.partyId + "]<br>";
	selectionInfo.innerHTML+= "<em>" + company.partyName + "</em>";
	
	// BRANCH
	selectionInfo.innerHTML+= "<br><br>";
	selectionInfo.innerHTML+= '<p class="title">' + branchTxt + "</p>";
	if (branch != null) {
		selectionInfo.innerHTML+= " [Id: " + branch.branchId + "]<br>";
		//selectionInfo.innerHTML+= "Main: " + ((branch.main == 'true')?"y":"n") + "<br>";  
		selectionInfo.innerHTML+= "<em>" + branch.branchName + "</em>";
	} else {
		selectionInfo.innerHTML+= '<p class="title" style="color:darkred">-' + selectTxt + "-</p>";
	}
	
	// CLIENT
	if (f.clientPartyId != null) { // Client combo exist
		if (client != null) {
			selectionInfo.innerHTML+= "<br><br>";
			selectionInfo.innerHTML+= '<p class="title">' + clientTxt + "</p>";
			selectionInfo.innerHTML+= " [Id: " + client.partyId + "]<br>";
			selectionInfo.innerHTML+= "<em>" + client.partyName + "</em>";
		}
	}
	
	if (branch != null || client != null)
		selectionInfo.style.display = "block";
	
	if (cptpZone == 'home') { // Home page position
		// nothing to do
	} else { // Result page position
		if (selectionInfo.style.left == '') {			
			selectionInfo.style.left = (selectionInfo.offsetLeft - selectionInfo.offsetWidth - 15) +"px"; // First left Page Position - real width - little padding
		} 
	}
	
}	
function hideInfoDiv(divName) {
	var selectionInfo = document.getElementById(divName);
	// empty div content
	selectionInfo.innerHTML = "";
	selectionInfo.style.display = "none";
}

function getAttSize(attValue) {
	return parseInt(attValue.substr(0, attValue.indexOf('px')));
}

function setAgentSpecialRate(rate, url) {
	var f = document.searchForm;
	f.specialRate.value = rate;
	f.action = url;
	f.submit();
}