function checkNotEmpty(el_id) {
	if ($(el_id).value=="") {
		$(el_id + '_img').src="/img/lp/oil_gas/decline_icon.png";
		return false;
	} else {
		$(el_id + '_img').src="/img/lp/oil_gas/accept_icon.png";
		return true;
	}
}
function checkNotEmpty2(el_id1,el_id2) {
	// check day,month,year
	var a = ['day','month','year'];
	if ($(el_id1).value=="") {
		$(el_id2 + '_img').src="/img/lp/oil_gas/decline_icon.png";
		return false;
	} else {
		var good = true;
		for (i in a) {
			if ($(a[i]).value =="") { good=false; }
		}
		if (good) {
			$(el_id2 + '_img').src="/img/lp/oil_gas/accept_icon.png";
			return true;
		}
		return false;
	}
}
function checkValidEmail(el_id) {
	fieldValue = $(el_id).value;
	apos=fieldValue.indexOf("@");
  	dotpos=fieldValue.lastIndexOf(".");
  	plength = fieldValue.length;
	if ((apos<1||dotpos-apos<2) || (dotpos>(plength-3))) {
		$(el_id + '_img').src="/img/lp/oil_gas/decline_icon.png";
		return false;
	} else {
		$(el_id + '_img').src="/img/lp/oil_gas/accept_icon.png";
		return true;
	}
}
function checkPassword1(el_id1,el_id2) {
	if ($(el_id1).value.length<7) {
		$(el_id1 + '_img').src="/img/lp/oil_gas/decline_icon.png";
		retval = false;
	} else {
		$(el_id1 + '_img').src="/img/lp/oil_gas/accept_icon.png";
		retval = true;
	}
	if (retval) {
		retval = checkPassword2(el_id2,el_id1);
	}
	return retval;
}
function checkPassword2(el_id1,el_id2) {
	if (( $(el_id1).value!=$(el_id2).value ) || ($(el_id2).value.length<7)) {
		$(el_id1 + '_img').src="/img/lp/oil_gas/decline_icon.png";
		return false;
	} else {
		$(el_id1 + '_img').src="/img/lp/oil_gas/accept_icon.png";
		return true;
	}
}
function checkCV(el_id) {
	fieldValue = $(el_id).value;
	fieldValueLength = fieldValue.length;
	if (fieldValue.substring((fieldValueLength-3)) !="doc" && fieldValue.substring((fieldValueLength-3)) != "pdf" && fieldValue.substring((fieldValueLength-4)) !="docx" && fieldValue.substring((fieldValueLength-3)) != "xls") {
		$(el_id + '_img').src="/img/lp/oil_gas/decline_icon.png";
		return false;
	} else {
		$(el_id + '_img').src="/img/lp/oil_gas/accept_icon.png";
		return true;
	}
}
function checkform() {
	errors=0;
	// go through all the fields
		
	// firstname
	field = 'firstname';
	if (checkNotEmpty(field)==false) 
		{ errors = errors + 1 }
	
	// lastname
	field = 'lastname';
	if (checkNotEmpty(field)==false) 
		{ errors = errors + 1 }
	
	// email
	field = 'email';
	if (checkValidEmail(field)==false) 
		{ errors = errors + 1 }
		
	// password
	field1 = 'password';
	field2 = 'password2';
	if (checkPassword1(field1,field2)==false) 
		{ errors = errors + 1 }
	if (checkPassword2(field2,field1)==false) 
		{ errors = errors + 1 }
	
	
	// nationality
	field = 'nationality';
	if (checkNotEmpty(field)==false) 
		{ errors = errors + 1 }
	
	// job title
	field = 'jobtitle';
	if (checkNotEmpty(field)==false) 
		{ errors = errors + 1 }
		
	// CV
	field = 'uploadcv';
	if (checkCV(field)==false) 
		{ errors = errors + 1 }
	
	// DOB
	if (checkNotEmpty2('day','date_of_birth')==false) 
		{ errors = errors + 1 }
	if (checkNotEmpty2('month','date_of_birth')==false) 
		{ errors = errors + 1 }
	if (checkNotEmpty2('year','date_of_birth')==false) 
		{ errors = errors + 1 }

	// privacy statement
 	if ($('privacy').checked==false) { 
 		alert('Please accept the privacy statement');
 	}

	// check if email already registered
	url = '/lp/check_email?email='+$('email').value;
	temp = new Ajax.Request(url, {
	  method: 'get',
	  asynchronous: false,
	  onComplete: function(t) {
	  	a = t.responseText;
	  	a=a.replace(/^\s+|\s+$/g,"");
	    if (a == '1') {
	    	el_id = 'email';
	    	$(el_id + '_img').src="/img/lp/oil_gas/decline_icon.png";
	    	$(el_id + '_img').title='This email address is already taken. Please login to the Atlas4Jobs.com-website';
			errors=errors+1;
	    }
	  }
	});
	a = temp.transport.responseText;
	a=a.replace(/^\s+|\s+$/g,"");

    if (a == '1') {
    	el_id = 'email';
    	$(el_id + '_img').src="/img/lp/oil_gas/decline_icon.png";
    	$(el_id + '_img').title='This email address is already taken. Please login to the Atlas4Jobs.com-website';
		errors=errors+1;
    }

	if (errors>0) {
		return false;
	} else {
		document.register_form.submit();
		return true;
	}
}
function changeImage(el_id){
	$(el_id + '_img').src="/img/lp/oil_gas/decline_icon.png";
	if (el_id=='email') {
		$(el_id + '_img').title='This email address is already taken. Please login to the Atlas4Jobs.com-website';
	}
	return false;
}
function changeServer(url){
	window.location=url;
}
