var dateInit = false;
var str;
var ins_str;


function validateForm() 	{
	// Validating the full Name
 var firstName = document.getElementById("firstName");
 if ( (firstName.value == "") || 
		(firstName.value == null)  
	) {
		alert("Please enter your first Name.");return;
	}

 var lastName = document.getElementById("lastName");
 if ( (lastName.value == "") || 
		(lastName.value == null)  
	) {
		alert("Please enter your Last Name.");return;
	}

 // validating date
    var birthdDay = document.getElementById("birthDay");
	if ( birthdDay.selectedIndex == 0 )
		{alert("Please fill your birthday"); return;}

 //Validating the emails
  var pEmail = document.getElementById("pEmail"); 
  if (
		(getPosOfLastAt(pEmail.value) + 5 < pEmail.value.length) ||
		(getPosOfLastAt(pEmail.value) + 2 >=  pEmail.value.length ) ||
		(pEmail.value.indexOf(".") == -1 ) ||
		(pEmail.value.indexOf("@") + 1 >=   getPosOfLastAt(pEmail.value) )
     ) {
		alert("Please enter a valid email in the contact information section."); return;
	}	
  var eEmail = document.getElementById("eEmail"); 
  if (
		(getPosOfLastAt(eEmail.value) + 5 < eEmail.value.length) ||
		(getPosOfLastAt(eEmail.value) + 2 >=  eEmail.value.length ) ||
		(eEmail.value.indexOf(".") == -1 ) ||
		(eEmail.value.indexOf("@") + 1 >=   getPosOfLastAt(eEmail.value) )
     )
		{alert("Please enter a valid email in the emergency contact information section.");return;}
  
   // Validating the phone numbers
    if (!isNumber(document.getElementById("personalPhone")) )
		{alert("Please enter a valid phone number"); return;}
	if (!isNumber(document.getElementById("workPhone")) )
		{alert("Please enter a valid work number"); return;}
	if (!isNumber(document.getElementById("faxPhone")) )
		{alert("Please enter a valid fax number"); return;}
		
	if (!isNumber(document.getElementById("conPersonalPhone")) )
		{alert("Please enter a valid phone number"); return;}
	if (!isNumber(document.getElementById("conWorkPhone")) )
		{alert("Please enter a valid work number"); return;}
	if (!isNumber(document.getElementById("conWorkPhone")) )
		{alert("Please enter a valid fax number"); return;}
	
	// validitaing nationality
    var select = document.getElementById("nationality");
	if (select.selectedIndex == 0) {
		alert("Please enter your nationality");
	}
	
	// validating photo 
	var photo = document.getElementById("photo");
	var length = photo.value.length;
	
	 if ( (photo.value == "") || 
		(photo.value == null)  
	) {
		alert("Please insert your Photo.");return;
	}
	if (
		   (photo.value.charAt(length-3) != 'j' ) ||
		   (photo.value.charAt(length-2) != 'p'  ) ||
		   (photo.value.charAt(length-1) != 'g'  ) 
		) {
		 alert("Please insert a photo of jpg type.");return;
		}
	
	
	var cv = document.getElementById("cv"); 
	var cvTA = document.getElementById("cvTA");
	
	 if (	( (cv.value == "") || (cv.value == null) ) &&
			( (cvTA.value == "") || (cvTA.value == null) )  
		) {
		alert("Please attach a CV file or write your CV.");return;
	}
	
	if ( (cv.value != "") && 
		(cv.value != null)  )  
	 {
		var lengthCV = cv.value.length;
	
		if (
			   (cv.value.charAt(lengthCV-3) != 'p' ) ||
			   (cv.value.charAt(lengthCV-2) != 'd'  ) ||
			   (cv.value.charAt(lengthCV-1) != 'f'  ) 
			) {
			 alert("Please attach a CV file of type pdf.");return;
			}
	}
		
// Validating the Occupation
 var occu = document.getElementById("occu");
 if ( (occu.value == "") || 
		(occu.value == null)  
	) {
		alert("Please Insert your occupation.");return;
	}
	
	
}
function getPosOfLastAt(string) {
	var pos;
	var i;
	for ( i = 0; i < string.length; i++ )
      {
		if (string.charAt(i) == '.' )
		     pos = i;
	  }
	return pos;
}


function isNumber(elem) {
    var str = elem.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString( );
    if (!str.match(re)) {
		//eval(falseAction);
        //alert("Enter only numbers into the field.");
        return false;
    }
	//eval(trueAction);
    return true;
}

// Date -----
// ------------------------------------------------------ Date ------------------------------------------------------ 
 function getYears(elementID,min,max){
				var birthYear = document.getElementById(elementID);
				var currentDate = new Date();
				var currentYear = currentDate.getFullYear();
				for (var i=(currentYear-min); i>(currentYear-max); i--){
					newOption = document.createElement('OPTION');
					newOption.setAttribute("value",i+"");
					textNode = document.createTextNode(i+"");
					newOption.appendChild(textNode);
					birthYear.appendChild(newOption);
				}
			}

function getMonths(elementID){
				var birthMonth = document.getElementById(elementID);
				for (var i=1; i<=12; i++){
					newOption = document.createElement('OPTION');
					newOption.setAttribute("value",i+"");
					textNode = document.createTextNode(i+"");
					newOption.appendChild(textNode);
					birthMonth.appendChild(newOption);				
				}
			}
			
function getDays(elementID,monthID,yearID){
				var birthDay = document.getElementById(elementID);
				var monthElement = document.getElementById(monthID);
				var yearElement = document.getElementById(yearID);
				var month = monthElement.value;
				var year = yearElement.value;
				var lastDayInMonth = 31;
				if (month != -1 && year != -1){
					while (birthDay.hasChildNodes()) { 
						birthDay.removeChild(birthDay.firstChild);
					}
					textNode = document.createTextNode("day");
					firstOption = document.createElement("OPTION");
					firstOption.setAttribute("value","-1");
					firstOption.appendChild(textNode);
					birthDay.appendChild(firstOption);
					if (month == 2){
						if ( ((year%4) == 0) && (((year%100) != 0) || ((year%400) == 0)) ){
							lastDayInMonth = 29;
						}
						else{
							lastDayInMonth = 28;
						}
					}
					else if ( month <= 7){
						if ( (month % 2) == 0){
							lastDayInMonth = 30;
						}
						else {
							lastDayInMonth = 31;
						}
					}
					else if ( month > 7) {
						if ( (month % 2) == 0){
							lastDayInMonth = 31;
						}
						else {
							lastDayInMonth = 30;
						}
					}
					for (var i=1; i<=lastDayInMonth; i++){
						newOption = document.createElement('OPTION');
						newOption.setAttribute("value",i+"");
						textNode = document.createTextNode(i+"");
						newOption.appendChild(textNode);
						birthDay.appendChild(newOption);		
					}
				}
}

function initializeDates(){
				if ( !dateInit) {
				getYears('birthYear',0,70);
				getMonths('birthMonth');
				getDays('birthDay','birthMonth','birthYear');
				dateInit = true;
				}
			}

