// JavaScript Document
window.onload = Load;
window.onresize = Load;

global_lang = 'ru';

function Load(){
	assignSubmenu();
}

function jump_to_page(obj){
	val = obj.value;	
	
	if(/[0-9]/.test(val)){
		location.href = 'show.page.php?page_id='+val;		
	}
	else{
		location.href = val;
	}
	
}

function sendCV(){
      
	  if($('agree').checked){
	  
		  params = {
		  pq_position:$('pq_position').value,
		  pq_type_of_work:$RF('pq_type_of_work'),
		  pq_exp_nobel:$RF('pq_exp_nobel'),
		  pq_position_title:$('pq_position_title').value,
		  pq_position_dates:$('pq_position_dates').value,
		  pq_supervisor_name:$('pq_supervisor_name').value,
		  pq_salary:$('pq_salary').value,
		  pd_name:$('pd_name').value,
		  pd_last_name:$('pd_last_name').value,
		  pd_date_of_birth:$('pd_date_of_birth').value,
		  pd_nationality_and_citizenship:$('pd_nationality_and_citizenship').value,
		  pd_home_address:$('pd_home_address').value,
		  pd_phone_number:$('pd_phone_number').value,
		  pd_email:$('pd_email').value,
		  pd_driving_license:$RF('pd_driving_license'),
		  e_institute_1:$('e_institute_1').value,
		  e_speciality_1:$('e_speciality_1').value,
		  e_degree_1:$('e_degree_1').value,
		  e_dates_1:$('e_dates_1').value,
		  e_institute_2:$('e_institute_2').value,
		  e_speciality_2:$('e_speciality_2').value,
		  e_degree_2:$('e_degree_2').value,
		  e_dates_2:$('e_dates_2').value,
		  e_institute_3:$('e_institute_3').value,
		  e_speciality_3:$('e_speciality_3').value,
		  e_degree_3:$('e_degree_3').value,
		  e_dates_3:$('e_dates_3').value,
		  skills_and_qualifications:$('skills_and_qualifications').value,
		  employer_1:$('employer_1').value,
		  position_title_1:$('position_title_1').value,
		  employment_dates_1:$('employment_dates_1').value,
		  duties_1:$('duties_1').value,
		  salary_1:$('salary_1').value,
		  reasons_for_leaving_1:$('reasons_for_leaving_1').value,
		  employer_2:$('employer_2').value,
		  position_title_2:$('position_title_2').value,
		  employment_dates_2:$('employment_dates_2').value,
		  duties_2:$('duties_2').value,
		  salary_2:$('salary_2').value,
		  reasons_for_leaving_2:$('reasons_for_leaving_2').value,
		  employer_3:$('employer_3').value,
		  position_title_3:$('position_title_3').value,
		  employment_dates_3:$('employment_dates_3').value,
		  duties_3:$('duties_3').value,
		  salary_3:$('salary_3').value,
		  reasons_for_leaving_3:$('reasons_for_leaving_3').value,
		  ref_name_1:$('ref_name_1').value,
		  ref_occupation_1:$('ref_occupation_1').value,
		  ref_contact_information_1:$('ref_contact_information_1').value,
		  ref_name_2:$('ref_name_2').value,
		  ref_occupation_2:$('ref_occupation_2').value,
		  ref_contact_information_2:$('ref_contact_information_2').value,
		  ref_name_3:$('ref_name_3').value,
		  ref_occupation_3:$('ref_occupation_3').value,
		  ref_contact_information_3:$('ref_contact_information_3').value
		  }
		  
		  		  
		  new Ajax.Request('sendCV.php',
			  {
				  method:'post',
				  onSuccess: function(transport){ 
				  alert(transport.responseText); 
				  },
				  parameters: params
			  }
		  );
	  }
	  else{
	  	alert('Please, check the agreement');
	  }
}

function sendMail(){	
	
	if($('name').value.length>0 && $('surname').value.length && $('company').value.length && $('subject').value.length>0 && $('message').value.length>0 && $('telephone').value.length>0){
		params = {
		name : $('name').value,
		surname: $('surname').value,
		company: $('company').value,
		telephone: $('telephone').value,
		subject: $('subject').value,
		message: $('message').value
		}
		
		new Ajax.Request('sendMail.php',{ 
			method: 'post',
			parameters: params,
			onSuccess: function(transport){
				alert(transport.responseText);
			}
		});		
	}
	else{	
		
		if($('name').value.length==0){
			alert('Please, enter your name.');
		}
		
		if($('surname').value.length==0){
			alert('Please, enter your surname.');
		}
		
		if($('company').value.length==0){
			alert('Please, enter your company.');
		}
		
		if($('telephone').value.length==0){
			alert('Please, enter your telephone.');
		}
		
		if($('subject').value.length==0){
			alert('Please, enter your subject.');
		}
		
		if($('message').value.length==0){
			alert('Please, enter your message.');
		}
	}
}


function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Ваш маил неправильный (проверьте сиволы @, .)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("Имя пользователя неправильно.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("IP указанный в вашем маиле!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Название домена неправильное.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("Конец имени домена не может окончиваться более чем на три буквы.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="В адресе отсутствует имя хоста!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function showmenu(obj, color, num){	
    
	/*
    submenu.style.display = 'block';
	submenu.style.color = 'white';
	submenu.style.width = '200px';
	for(keyVar in obj){
		submenu.innerHTML += 'obj['+keyVar+'] = '+obj[keyVar]+'<br>';
	}
	*/
	
	run = false;
	if(menu[num]['title'][0].length!=0) { run = true; } else { run = false; }
	left_right = findPos(obj);
	left_right = left_right.split("|");
	left = left_right[0]; right = left_right[1];
	
		with(document.getElementById('submenu')){
			if(run){
			//filters[0].apply();
			style.left = String(parseInt(left)+150)+'px';
			style.top = String(parseInt(right))+'px';			
			style.background = color;
			style.display = 'block';
			//filters[0].play();
			innerHTML = generateMenu(menu[num]);
			}
		}
		
		document.getElementById('submenu').onmouseleave = function(){
			hidemenu();
		}
	
}

function hidemenu(){
	   with(document.getElementById('submenu')){
			//filters[0].apply();			
			style.background = 'none';
			style.display = 'none';
			//filters[0].play();			
		}
}

function generateMenu(menu){
	menu_ = "<table class='submenu'>";	
    for(i=0;i<menu['title'].length;i++){
		menu_ += "<tr><td onmouseover=\"this.style.background='#DA261D'\" onmouseout=\"this.style.background=''\"><a href=\""+menu['link'][i]+"\">&nbsp;<font color=white>"+menu['title'][i]+"</font></a></td></tr>";
	}
	menu_+= "</table>";
	menu_+="<img src='pictures/submenu_bg.gif'>";
	return menu_;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return curleft+"|"+curtop;
}

function assignSubmenu(){

	links = document.getElementById('navigation').getElementsByTagName('a');
	colors = new Array('#999','#999','#999','#999','#999','#999');
	
	k=0;
	for(i=0;i<links.length;i++){
		
			links[i].setAttribute("num", k)
			links[i].onmouseover = function(){			
				showmenu(this, colors[this.getAttribute("num")], this.getAttribute("num"));
			}
			k++;
		
	}
	

}

