// JavaScript Document
var background_ok='#ffffff';
var background_ok1='#ffffff';
var background_ok2='#F1F4F4';
var background_nok='#ffe6e6';
var border_nok='#ff6666 1px solid';
var not_null_objs=new Array();
var who_button;
//script for language
function all_not_null_obj(doc_name)
{
	 if(doc_name=="vacancy")
	 {
		who_button="sub_vacancy";
		not_null_objs.push(new Array("post",false));
		//not_null_objs.push(new Array("dateend",false));
		not_null_objs.push(new Array("doljnost",false));
		not_null_objs.push(new Array("kapcha_id",false));				
		return; 
	 }	  
    
    if(doc_name=="user")
    {
		who_button="sub_master";
		not_null_objs.push(new Array("lastname",false));
		not_null_objs.push(new Array("firstname",false));
		not_null_objs.push(new Array("password",false));
		not_null_objs.push(new Array("password_repeat",false));
		not_null_objs.push(new Array("email",false));
		not_null_objs.push(new Array("email_repeat",false));
                not_null_objs.push(new Array("want_work",false));

        not_null_objs.push(new Array("birth_day",false));
        //not_null_objs.push(new Array("speciality",false));
        not_null_objs.push(new Array("knowledge_comp",false));
        not_null_objs.push(new Array("phone_mob",false));
        not_null_objs.push(new Array("phone_home",false));
		not_null_objs.push(new Array("kapcha_id",false));
		not_null_objs.push(new Array("education",false));
		return;
    }
    else
    {
		if(doc_name=="company")
		{
		    who_button="sub_company";
			not_null_objs.push(new Array("password",false));
			not_null_objs.push(new Array("password_repeat",false));
			not_null_objs.push(new Array("email",false));
			not_null_objs.push(new Array("email_repeat",false));
			not_null_objs.push(new Array("lastname",false));
			not_null_objs.push(new Array("fristname",false));
			not_null_objs.push(new Array("company_name",false));
			not_null_objs.push(new Array("region_job",false));
			not_null_objs.push(new Array("post",false));
			not_null_objs.push(new Array("address",false));
			not_null_objs.push(new Array("phone",false));
			not_null_objs.push(new Array("kapcha_id",false));			
		 }
    }   
}

function all_not_null_edit(doc_name)
{
	 if(doc_name=="vacancy")
	 {
		who_button="sub_vacancy";
		not_null_objs.push(new Array("post",true));
		//not_null_objs.push(new Array("dateend",true));
		not_null_objs.push(new Array("doljnost",true));
		not_null_objs.push(new Array("kapcha_id",false));				
		return; 
	 }	  
    
    if(doc_name=="user")
    {
		who_button="sub_master";
		not_null_objs.push(new Array("lastname",true));
		not_null_objs.push(new Array("firstname",true));
                not_null_objs.push(new Array("birth_day",true));
                not_null_objs.push(new Array("want_work",true));
                not_null_objs.push(new Array("knowledge_comp",true));
                not_null_objs.push(new Array("phone_mob",true));
                not_null_objs.push(new Array("phone_home",true));
		not_null_objs.push(new Array("education",true));
		not_null_objs.push(new Array("kapcha_id",false));
		return;
    }
    else
    {
		if(doc_name=="company")
		{
		    who_button="sub_company";
			not_null_objs.push(new Array("lastname",true));
			not_null_objs.push(new Array("fristname",true));
			not_null_objs.push(new Array("company_name",true));
			not_null_objs.push(new Array("region_job",true));
			not_null_objs.push(new Array("post",true));
			not_null_objs.push(new Array("address",true));
			not_null_objs.push(new Array("phone",true));
			not_null_objs.push(new Array("kapcha_id",false));			
		 }
    }   
}


function true_lang_tool(lang_number)
{
	
		if(document.getElementById("langs"+lang_number).value=='null') return false;
		var radio_list=document.getElementsByName("lang"+lang_number);
        var	have_checked_radio=false;
		var index;
		for(index=0;index<radio_list.length;index++)
		{
			if(radio_list[index].checked==true) return true;
		}
		return false;
	
}
function change_lang(lang_number)
{
		//var lang_table=document.getElementById("lang_table");
	
		if(true_lang_tool(lang_number)){
			change_obj("langs"+lang_number,true);
		
		}
		else
		{
			change_obj("langs"+lang_number,false);
		
		}
	
	
	
}	

//script for birthday
function DaysInMonth(Y, M) {
    with (new Date(Y, M, 1, 12)) {
        setDate(0);
        return getDate();
    }
}

function change_day()
{
 var mount_obj=document.getElementById("month");
 var day_count=DaysInMonth(document.getElementById("year").value,document.getElementById("month").value);	
 if(day_count>0)
{ 
	var day_obj=document.getElementById("day");
 	count=day_count-(day_obj.length-1);
   
	if(count>0)
	{
	  	var i;
        var n= count+day_obj.length;
		for(i=day_obj.length;i<n;i++)
			{   
               try
               {
                day_obj.add(new Option(i, i),  null);
               }
               catch(exx)
               {
                day_obj.add(new Option(i, i));
               }
  	         }
	}
	if(count<0)
	{
		var i;
		for(i=Math.abs(count)-1;i>=0;i--)
				day_obj.remove(day_obj.length-1-i);	
	}
}
 
}
function find_element_arrays(array,element_name)
{

 for(i=0;i<array.length;i++)
    if(	not_null_objs[i][0]==element_name)
        return i;
}


function change_obj(element_id,is_default)
{
	var  i;
		var alls_notNull=false;
        for(i=0;i<not_null_objs.length;i++)
        {
            if(	not_null_objs[i][0]==element_id)  not_null_objs[i][1]=is_default;   
            if(	not_null_objs[i][1]==false){ alls_notNull=true; }
        }
        //when all elements not null
		
		
        document.getElementById(who_button).disabled=alls_notNull;
		var col1_id=document.getElementById(element_id+"_col1");
		var col2_id=document.getElementById(element_id+"_col2");
		var hidden_div=document.getElementById("div_"+element_id);
	 if(is_default)
	 {
		  if(col1_id!=null){
		   col1_id.style.border='none';
		   col1_id.style.background=background_ok;
		  }
		  if(col2_id!=null){
		   col2_id.style.border='none';
			col2_id.style.background=background_ok ;
		 }
		 if(hidden_div!=null){
		   hidden_div.style.visibility='hidden';
		   hidden_div.style.display='none';
		 }
	  

	  
	   return;
	 }
	if(col1_id!=null){
		 col1_id.style.background=background_nok;
		 col1_id.style.borderLeft=border_nok;
		 col1_id.style.borderTop=border_nok;
		 col1_id.style.borderBottom=border_nok;
	}
	if(col2_id!=null)
	{
		 col2_id.style.background=background_nok;
		 col2_id.style.borderRight=border_nok;
		 col2_id.style.borderTop=border_nok;
		 col2_id.style.borderBottom=border_nok;
	}
	else
	col1_id.style.borderRight=border_nok;
	
	if(hidden_div!=null){
		 hidden_div.style.visibility='visible';
		 hidden_div.style.display='block';
	}
}

 function isValidEmail(email,element_id)
 {
  var this_index=find_element_arrays(not_null_objs,element_id);
  var re = /^[a-z0-9_\.-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
  if(!re.test(email)){not_null_objs[this_index][1]=false; change_obj(element_id,false); return ;}
  var repeat_index=find_element_arrays(not_null_objs,"email_repeat");
  
  
 //if other is null	 
 if(document.getElementById("email").value!="" && document.getElementById("email_repeat").value!="")
{  if(document.getElementById("email").value!=document.getElementById("email_repeat").value)
  {
   not_null_objs[this_index][1]=true;
   not_null_objs[repeat_index][1]=false;
   change_obj("email_repeat",false);
   change_obj("email",false);
  
  }
  else
  {
	not_null_objs[repeat_index][1]=true;
	change_obj("email_repeat",true);
	not_null_objs[this_index][1]=true;
	change_obj("email",true);
  }
}
 else
  {

	change_obj(element_id,true);

  }
  return ;
 }

function isValidPhone(phone)
 {
 	var validPhone=/^[0-9]+$/;
	return validPhone.test(phone);
 }
function isValidPassword(password,element_id)
{
	
	var this_index=find_element_arrays(not_null_objs,element_id);
// var validPasswd = /^[a-zA-Z]\w{3,14}$/i;
  var validPasswd=/^[A-Za-z0-9]+$/;
 if(!validPasswd.test(password)){not_null_objs[this_index][1]=false;  change_obj(element_id,false); return ;}
 var repeat_index=find_element_arrays(not_null_objs,"password_repeat");
  
 //if other is null	 
 if(document.getElementById("password").value!="" && document.getElementById("password_repeat").value!="")
 { if(document.getElementById("password").value!=document.getElementById("password_repeat").value)
  {
 
   not_null_objs[this_index][1]=true;
   change_obj("password",true);
   not_null_objs[repeat_index][1]=false;
   change_obj("password_repeat",false);
 
  
  }
  else
  {
	not_null_objs[repeat_index][1]=true;
	not_null_objs[this_index][1]=true;
	change_obj("password_repeat",true);
	change_obj("password",true);
	
  }
 }
 else
 {
	 	change_obj(element_id,true);
 }


  return ;
}

function isValidConfPass(pass)
{
	if(document.getElementById('password').value == "") 
		return true;
	if(document.getElementById('password').value == pass)
		return true;
	return false;
}

function isValidConfMail(pass)
{
	if(document.getElementById('email').value == "") 
		return true;
	if(document.getElementById('email_repeat').value == pass)
		return true;
	return false;
}


function isValidConfMail(mail)
{
	if(document.getElementById('email').value == mail)
		return true;
	return false;
}

var date=new Array();
date[0]="day";
date[1]="month";
date[2]="year";
function true_date_tool()
{
	if(document.getElementById("day").value=="null" || document.getElementById("month").value=="null" || document.getElementById("year").value=="null") return false;
	return true;
}


function nofocus_date(element_id)
{
 var obj=document.getElementById(element_id);
 if(obj.value=="" || obj.value=="null")
 {
 change_obj("birth_day",false);
 }
 else
 {
 	if(true_date_tool())
	{
		change_obj("birth_day",true);
	}
 }
}


function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}


function nofocus(element_id)
{

 	var obj=document.getElementById(element_id);
	 if(trim(obj.value)=="" || obj.value=="null")
	{
		
			
			 if(element_id=="date_end")
			{setTimeout("nofocus('"+element_id+"')",200); return;}
			else
			{change_obj(element_id,false);}
	}
	 else
	 {
		 var obj=document.getElementById(element_id);
		 if(element_id=="email" || element_id=="email_repeat")
		 { isValidEmail(obj.value,element_id); return;}
		//if onblur password obj
		 if(element_id=="password" || element_id=="password_repeat")
		{	isValidPassword(obj.value,element_id); return;}
		//if onblur phone obj
	//	if(element_id=="phone_home" || element_id=="phone_mob" || element_id=="phone")
	//	   if(!isValidPhone(obj.value)){change_obj(element_id,false); return;}
		  //if onblur langs obj 
		if(element_id.match("langs")!=null)
			if(!true_lang_tool(element_id.substr(5,1))) {change_obj(element_id,false); return;}
	
	
		
		
		   ///////////////////////
				 change_obj(element_id,true);
	 }
	
}
/*---------------------ajax----------------------------------------------*/
var request = null;
function createRequest()
{
	try
	{
		request = new XMLHttpRequest();
	}
	catch(trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed)
			{
				request = null;
			}
		}
	}
	
	if(request == null)
	{
		alert('Error creating request object !!!');
	}
}
//----------------------------------------------------------------------------
function getBoardsSold()
{


	createRequest();
	if(request.readyState==4 || request.readyState==0){
		name = encodeURIComponent(document.getElementById("kapcha_id").value);
		var url = "getAjax.php?data=";
		url = url + name;
		request.open("GET", url, true);
		request.onreadystatechange = updatePage;
		request.send(null);
	} else {
		setTimeout("getBoardsSold()",200);
	}
}
//----------------------------------------------------------------------------
function updatePage()
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			xmlResponse=request.responseXML;	
			xmlDocumentElement=xmlResponse.documentElement;
			helloMassege=xmlDocumentElement.firstChild.data;
			if(helloMassege == "no")
			{
				background_ok=background_ok1;
				change_obj("kapcha_id",false);
				
			}
			else 
				change_obj("kapcha_id",true);
			setTimeout("getBoardsSold()",200);
		}
	}
}
/*--------------------------------------------------------------------------*/
function getMail()
{
	createRequest();
	if(request.readyState==4 || request.readyState==0){
		name = encodeURIComponent(document.getElementById("email").value);
		var url = "mailAjax.php?mail=";
		url = url + name;
		request.open("GET", url, true);
		request.onreadystatechange = updateMail;
		request.send(null);
	} else {
		setTimeout("getMail()",200);
	}
}
//----------------------------------------------------------------------------
function updateMail()
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			xmlResponse=request.responseXML;	
			xmlDocumentElement=xmlResponse.documentElement;
			helloMassege=xmlDocumentElement.firstChild.data;
            var re = /^[a-z0-9_\.-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i;
 			 if(!re.test(document.getElementById("email").value)){change_obj("email",false); return ;}
			if(helloMassege=='no')
				change_obj("email",false);
			else 
				change_obj("email",true);
			setTimeout("getMail()",200);
		}
	}
}

/*--------------------------------------------------------------------------*/
function getMailCompany()
{
	createRequest();
	if(request.readyState==4 || request.readyState==0){
		name = encodeURIComponent(document.getElementById("email").value);
		var url = "mailAjaxCompany.php?mail=";
		url = url + name;
		request.open("GET", url, true);
		request.onreadystatechange = updateMailCompany;
		request.send(null);
	} else {
		setTimeout("getMailCompany()",500);
	}
}
//----------------------------------------------------------------------------
function updateMailCompany()
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			xmlResponse=request.responseXML;	
			xmlDocumentElement=xmlResponse.documentElement;
			helloMassege=xmlDocumentElement.firstChild.data;
			if(helloMassege=='no')
				change_obj("email",false);
			else 
				change_obj("email",true);
			setTimeout("getMailCompany()",500);
		}
	}
}

/*--------------------------------------------------------------------------*/
