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 of een zoekwoord of een salarisindicatie in\n",
    "en" : "You must enter a search word or salary\n"
};

var err_mess = "";
var rt = new Object();

function department_vacancies_submit(id){
	document.job_form.mode.value = "department_vacancies";
	document.job_form.dep_id.value = id;
	document.job_form.submit();
}

function backToGeneral(){
	document.job_form.mode.value = "general";
	document.job_form.submit();
}

function postDepApplication(department_id, vacancy_id){
	document.job_form.mode.value = "post_application";
	document.job_form.dep_id.value = department_id;
	document.job_form.vac_id.value = vacancy_id;
	document.job_form.submit();
}

function postDepApplicationBack(department_id){
	document.job_form.mode.value = "department_vacancies";
	document.job_form.dep_id.value = department_id;
	document.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.type=='text' || obj.type=='number'
	    || obj.type=='file'|| obj.type=='email' || obj.type=='date') ){
	    obj.style.backgroundColor="white";
	    if(obj.getAttribute("required") == "true" && (obj.value==null || obj.value=='')){
	        obj.style.backgroundColor="red";
	        rt.req = false;
	    }
	    if (obj.value!=null && obj.value!=""){
	        if (obj.type=='number'){
	            if (!checkNum(obj.value)){
	                rt.num = false;
	                obj.style.backgroundColor="red";
	            }
	        }
	        else if (obj.type=='email'){
	            if (!checkEmail(obj.value)){
	                rt.email = false;
	                obj.style.backgroundColor="red";
	            }
	        }
	        else if (obj.type=='date'){
	            if (!checkDate(obj.value)){
	                rt.date = false;
	                obj.style.backgroundColor="red";
	            }
	        }
	    }
	}
	else if(obj.tagName=="TEXTAREA"){
	    obj.style.backgroundColor="white";
	    if (obj.getAttribute("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)){
	        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(document.job_form);
	
	if (createErrorMessage()){		
	    document.job_form.mode.value = "save_application";
	    document.job_form.dep_id.value = department_id;
	    document.job_form.vac_id.value = vacancy_id;
	    document.job_form.submit();
	}
	else{
	    alert(err_mess);
		return false;
	}
}

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(){
	document.job_form.mode.value = "show_all_vacancies";
	if (document.job_form.type_id)
		document.job_form.type_id.value = "";	
	document.job_form.submit();
}

function backToGeneral(){
	document.job_form.mode.value = "general";
	document.job_form.submit();
}

function vacancyDetailedView(vac_id, dep_id){
	document.job_form.mode.value = "vacancy_details";
	document.job_form.dep_id.value = dep_id;
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}

function postVacApplication(dep_id, vac_id){
	document.job_form.mode.value = "post_vac_application";
	document.job_form.dep_id.value = dep_id;
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}

function postVacApplicationBack(dep_id, vac_id){
	document.job_form.mode.value = "vacancy_details";
	document.job_form.dep_id.value = dep_id;
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}

function saveVacApplication(dep_id, vac_id){
	rt.req = true;
	rt.num = true;
	rt.email = true;
	rt.date = true;
	validateJobForm(document.job_form);
	if (createErrorMessage()){
	    document.job_form.mode.value = "save_vac_application";
	    document.job_form.dep_id.value = dep_id;
	    document.job_form.vac_id.value = vac_id;
	    document.job_form.submit();
	}
	else{
	    alert(err_mess);
		return false;
	}

}

function postGeneralApplication(){
	document.job_form.mode.value = "post_gen_application";
	document.job_form.submit();
}

function postGeneralForm(vac_id){
	if (validateDep()){
	    rt.req = true;
	    rt.num = true;
	    rt.email = true;
	    rt.date = true;
	    validateJobForm(document.job_form);
	    if (createErrorMessage()){
	        document.job_form.mode.value = "save_gen_application";
	        document.job_form.vac_id.value = vac_id;
	        document.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(){
	document.job_form.mode.value = "search";
	document.job_form.submit();
}

function doSearch(){
	if (validateSearch()){
	    document.job_form.mode.value = "do_search";
	    document.job_form.submit();
	}
}


function validateSearch(){
	if(document.job_form.search_word.value==null || document.job_form.search_word.value=='') {
	    alert(jm_ENTER_WORD_SALARY[lang]);
	    return false;
	}
	return true;
}

function searchDetails(vac_id){
	document.job_form.mode.value = "search_result";
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}

function goToDoSearch(){
	document.job_form.mode.value = "do_search";
	document.job_form.submit();
}

function postSearchResult(vac_id){
	document.job_form.mode.value = "post_search_result";
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}

function goToSearchResult(vac_id){
	document.job_form.mode.value = "search_result";
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}

function saveSearchApplication(vac_id, dep_id){
	document.job_form.mode.value = "save_search_result";
	document.job_form.dep_id.value = dep_id;
	document.job_form.vac_id.value = vac_id;
	document.job_form.submit();
}


function submitSearchJobForm() {
	if(document.job_form.dept.value==""){
		document.job_form.mode.value = "show_all_vacancies";
	}else{
		document.job_form.mode.value = "department_vacancies";		
	}	
	document.job_form.dep_id.value = document.job_form.dept.value;
//	job_form.vac_id.value = vac_id;
//	alert(jobSearchForm.action);
//	alert(document.jobSearchForm.ditDepId.value);
//      jobSearchForm.action = "job.html?mode=department_vacancies&amp;dep_id=" + document.jobSearchForm.ditDepId.value;
//	jobSearchForm.action = "job.html?mode=department_vacancies&amp;dep_id=" + document.job_form.dep_id.value;
}
