﻿function trim(str) {
	if (!str || typeof str != 'string')
	{ return null; }

	return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}

function ClearOptions(object) {
	object.options.length = 0;
}

function ClearFirstOptionIfEmpty(id) {
	var list = document.getElementById(id);
	if (!list || !list.options || !list.options[0])
		return;
    var zeroindex = list.options[0].text;
    if (zeroindex.match("Επιλογή") != null)
	    list.remove(0);
}

function SetSelection(id,index) {
	var list = document.getElementById(id);
	if (list)
		list.selectedIndex = index;
}

function GetListSelection(id) {
	var list = document.getElementById(id);
	if (!list || list.selectedIndex < 0)
		return "";
	return list.options[list.selectedIndex].text;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function ShowMilitaryOptions() {
	var checked = $("input:radio[value='Male']").attr("checked");
	if (checked)
		ShowElement("militaryDiv");
	else
		HideElement("militaryDiv");
}
function ShowElement(id) {
    var element = $("#" + id);
	element.show("fast");
}

function HideElement(id) {
	$("#" + id).hide("fast");
}

function SetFirstOptionSelected(id) {
	SetSelection(id, 0);
}

function FillList(id, values) {
	var array = values.split("|");
	var list = document.getElementById(id);
	ClearOptions(list);
	for (i = 0; i < array.length; i++) {
		list.options[list.options.length] = new Option(array[i], array[i]);
	}
	ShowElement(id);
}

//PERSON

function CheckPostponed(id) {
	var selection = $('#' + id + ' :selected').val();
	if (selection == "postponed") {
		ShowElement("militaryPostponedSpan");
	}
	else
		HideElement("militaryPostponedSpan");
}

//POSITION INTERESTED

function HideSecondListOnOtherSelected(firstListId, secondListId) {
	var selection = $('#' + firstListId + ' :selected').val();
	if (selection == "other") {
		SetFirstOptionSelected(secondListId);
		HideElement(firstListId);
		ShowElement(secondListId);
	}
}

//WORK

function HideFollowUpWorkLists(id) {
    var fromWorkPanel = id[id.length - 1];
    var lastindexofunderscore = id.lastIndexOf('_');
    var clientIdPrefix = id.substring(0, lastindexofunderscore + 1);
    var field = clientIdPrefix + "workfield";
    var position = clientIdPrefix + "workposition";
    var years = clientIdPrefix + "workyears";
    var fieldtext = clientIdPrefix + "workfieldtext";
    var positiontext = clientIdPrefix + "workpositiontext";
    var yearstext = clientIdPrefix + "workyearstext";
    var lists = new Array();
    lists[0] = field;
    lists[1] = position;
    lists[2] = years;
    var texts = new Array();
    texts[0] = fieldtext;
    texts[1] = positiontext;
    texts[2] = yearstext;
    var found = false;
    var count = lists.length;
    for (var i = 0; i < lists.length; i++) {
        if (lists[i] == id) {
            SetFirstOptionSelected(lists[i]);
            ShowElement(lists[i]);
            ShowElement(texts[i]);
            found = true;
        }
        else if (found) {
            HideElement(lists[i]);
            HideElement(texts[i]);
            SetFirstOptionSelected(lists[i]);
        }
    }
}

function SetWorkDisplay(workEmployerTextId, workfieldListId, workfieldTextId) {
    var text = document.getElementById(workEmployerTextId).value;
    if (text == "") {
        HideFollowUpWorkLists(workfieldListId);
        HideElement(workfieldListId);
        HideElement(workfieldTextId);
    }
    else {
        ShowElement(workfieldListId);
        ShowElement(workfieldTextId);
    }
}

// LANGUAGES
function CheckLanguageOption(langId, defineId) {
    var selection = GetListSelection(langId);
    if (selection == "ΑΛΛΗ ΞΕΝΗ ΓΛΩΣΣΑ") {
        ShowElement(defineId);
    }
    else
        HideElement(defineId);
}


