var jm_MUST_FILL_REQUIRED = {
    "nl" : "U moet alle verplichte velden invoeren!\n",
    "en" : "You must fill all obligatory fields!\n"
};

var jm_NUMBER_NOT_VALID = {
    "nl" : "Voer een juist telefoonnummer in!\n",
    "en" : "Number is not valid!\n"
};

var  jm_EMAIL_NOT_VALID = {
    "nl" : "Voer een juist emailadres in!\n",
    "en" : "Email is not valid!\n"
};

var jm_DATE_NOT_VALID = {
    "nl" : "Voer een correcte datum in!\n",
    "en" : "Date is not valid!\n"
};

var jm_CHOOSE_DEPARTMENT = {
    "nl" : "U moet minstens een departement kiezen\n",
    "en" : "You must choose at least one department\n"
};

var jm_ENTER_WORD_SALARY = {
    "nl" : "Vul een zoekwoord in\n",
    "en" : "You must enter a search word\n"
};

var err_mess = "";
var rt = new Object();

function department_vacancies_submit(id){
	job_form.mode.value = "department_vacancies";
	job_form.dep_id.value = id;
	job_form.submit();
}

function backToGeneral(){
	job_form.mode.value = "general";
	job_form.submit();
}

function postDepApplication(department_id, vacancy_id){
	job_form.mode.value = "post_application";
	job_form.dep_id.value = department_id;
	job_form.vac_id.value = vacancy_id;
	job_form.submit();
}

function postDepApplicationBack(department_id){
	job_form.mode.value = "department_vacancies";
	job_form.dep_id.value = department_id;
	job_form.submit();
}

function validateJobForm(frm){
	for (var i=0; i<frm.childNodes.length; i++){
	    validateField(frm.childNodes(i));
	}
}

function validateField(obj){
	if (obj.tagName=="INPUT" && (obj.ftype=="text" || obj.ftype=="number"
	    || obj.ftype=="file" || obj.ftype=="email" || obj.ftype=="date") ){
	    obj.style.backgroundColor="white";
	    if(obj.required=="true" && (obj.value==null || obj.value=="")){
	        obj.style.backgroundColor="red";
	        rt.req = false;
	    }
	    if (obj.value!=null && obj.value!=""){
	        if (obj.ftype=="number"){
	            if (!checkNum(obj.value)){
	                rt.num = false;
	                obj.style.backgroundColor="red";
	            }
	        }
	        else if (obj.ftype=="email"){
	            if (!checkEmail(obj.value)){
	                rt.email = false;
	                obj.style.backgroundColor="red";
	            }
	        }
	        else if (obj.ftype=="date"){
	            if (!checkDate(obj.value)){
	                rt.date = false;
	                obj.style.backgroundColor="red";
	            }
	        }
	    }
	}
	else if(obj.tagName=="TEXTAREA"){
	    obj.style.backgroundColor="white";
	    if (obj.required=="true" && (obj.value==null || obj.value=="")){
	        obj.style.backgroundColor="red";
	        rt.req = false;
	    }
	}
	for (var i=0; i<obj.childNodes.length; i++){
	    validateField(obj.childNodes(i));
	}
}

function checkNum(num1){
	var num = new Number(num1);
	if (num.toString()=="" || num.toString()=="NaN"){
	    return false;
	}
	return true;
}
	
function isLeapYear(year) {
	return (year%4) == 0 && ((year%100) != 0 || (year%400) == 0);
}


function checkEmail(email){
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0){
	    var pindex = theStr.indexOf(".",index);
	    if ((pindex > index+1) && (theStr.length > pindex+1))
	    result = true;
	}
	return result;
}

function checkDate(dat){
	if(dat.length==10 && dat.substring(2,3)=="-" && dat.substring(5,6)=="-"){
	    var year = getNum(dat.substring(6,10), 1, 3000);
	    var month = getNum(dat.substring(3,5), 1, 12);
	    var day = getNum(dat.substring(0,2), 1, 31);
	    if(year==-1 || month==-1 || day==-1){
	        return false;
	    }
	    if (day==31 && (month==4 || month==6 || month==9 || month==11)){
	        return false;
	    }
	    if (day>28 && isLeapYear(year) && month==2){
	        return false;
	    }
	    return true;
	}
	return false;
}

function getNum(num1, min, max){
	var num = new Number(num1);
	if (num.toString()=="" || num.toString()=="NaN"){
	    return -1;
	}
	if (num.valueOf()>=min && num.valueOf()<=max){
	    return num.valueOf();
	}
	return -1;
}

function sendApplication(department_id, vacancy_id){
	rt.req = true;
	rt.num = true;
	rt.email = true;
	rt.date = true;
	validateJobForm(job_form);
	if (createErrorMessage()){
	    job_form.mode.value = "save_application";
	    job_form.dep_id.value = department_id;
	    job_form.vac_id.value = vacancy_id;
	    job_form.submit();
	}
	else{
	    alert(err_mess);
	}
}

function createErrorMessage(){
	err_mess = "";
	if (rt.req == false){
	    err_mess += jm_MUST_FILL_REQUIRED[lang];
	}
	if (rt.num == false){
	    err_mess += jm_NUMBER_NOT_VALID[lang];
	}
	if (rt.email == false){
	    err_mess += jm_EMAIL_NOT_VALID[lang];
	}
	if (rt.date == false){
	    err_mess += jm_DATE_NOT_VALID[lang];
	}
	if (err_mess == ""){
	    return true;
	}
	else{
	    return false;
	}
}

function showAllVacs(){
	job_form.mode.value = "show_all_vacancies";
	job_form.submit();
}

function backToGeneral(){
	job_form.mode.value = "general";
	job_form.submit();
}

function vacancyDetailedView(vac_id, dep_id){
	job_form.mode.value = "vacancy_details";
	job_form.dep_id.value = dep_id;
	job_form.vac_id.value = vac_id;
	job_form.submit();
}

function postVacApplication(dep_id, vac_id){
	job_form.mode.value = "post_vac_application";
	job_form.dep_id.value = dep_id;
	job_form.vac_id.value = vac_id;
	job_form.submit();
}

function postVacApplicationBack(dep_id, vac_id){
	job_form.mode.value = "vacancy_details";
	job_form.dep_id.value = dep_id;
	job_form.vac_id.value = vac_id;
	job_form.submit();
}

function saveVacApplication(dep_id, vac_id){
	rt.req = true;
	rt.num = true;
	rt.email = true;
	rt.date = true;
	validateJobForm(job_form);
	if (createErrorMessage()){
	    job_form.mode.value = "save_vac_application";
	    job_form.dep_id.value = dep_id;
	    job_form.vac_id.value = vac_id;
	    job_form.submit();
	}
	else{
	    alert(err_mess);
	}

}

function postGeneralApplication(){
	job_form.mode.value = "post_gen_application";
	job_form.submit();
}

function postGeneralForm(vac_id){
	if (validateDep()){
	    rt.req = true;
	    rt.num = true;
	    rt.email = true;
	    rt.date = true;
	    validateJobForm(job_form);
	    if (createErrorMessage()){
	        job_form.mode.value = "save_gen_application";
	        job_form.vac_id.value = vac_id;
	        job_form.submit();
	    }
	    else{
	        alert(err_mess);
	    }
	}
}

function validateDep(){
	var flag = false;
	var deps=document.getElementsByName("_dep");
	for (var i=0; i<deps.length; i++){
	    if (deps[i].checked){
	        flag=true;
	        break;
	    }
	}
	if(!flag){
	    alert(jm_CHOOSE_DEPARTMENT[lang]);
	}
	return flag;
}

function goToSearch(){
	job_form.mode.value = "search";
	job_form.submit();
}

function doSearch(){
	if (validateSearch()){
	    job_form.mode.value = "do_search";
	    job_form.submit();
	}
}


function validateSearch(){
	if(job_form.search_word.value==null || job_form.search_word.value=='') {
	    alert(jm_ENTER_WORD_SALARY[lang]);
	    return false;
	}
	return true;
}

function searchDetails(vac_id){
	job_form.mode.value = "search_result";
	job_form.vac_id.value = vac_id;
	job_form.submit();
}

function goToDoSearch(){
	job_form.mode.value = "do_search";
	job_form.submit();
}

function postSearchResult(vac_id){
	job_form.mode.value = "post_search_result";
	job_form.vac_id.value = vac_id;
	job_form.submit();
}

function goToSearchResult(vac_id){
	job_form.mode.value = "search_result";
	job_form.vac_id.value = vac_id;
	job_form.submit();
}

function saveSearchApplication(vac_id, dep_id){
	job_form.mode.value = "save_search_result";
	job_form.dep_id.value = dep_id;
	job_form.vac_id.value = vac_id;
	job_form.submit();
}