/* -------------------------- */
/*   XMLHTTPRequest Enable    */
/* -------------------------- */
function createObject() {
  var request=null;

  // пытаемся создать объект для MSXML 2 и старше
  if(!request) try {
    request=new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e){}

  // не вышло... попробуем для MSXML 1
  if(!request) try {
    request=new ActiveXObject('Microsoft.XMLHTTP');
  } catch (e){}

  // не вышло... попробуем для Mozilla
  if(!request) try {
    request=new XMLHttpRequest();
  } catch (e){}

  if(!request)
    // ничего не получилось...
    return "";
 
  return request
}

var http = createObject();

/* -------------------------- */
/*        FUNCTIONS           */
/* -------------------------- */


function trim(str){

return str.replace(/(^\s+)|(\s+$)/g, "");

}
function SelectChange(blockName, currentBlock, blockCount){
return;
    switch (blockName){
        case "spec":

            if (document.getElementById('specialization'.concat(currentBlock)).value!=""){
                document.getElementById(blockName.concat(currentBlock+1)).style.display = "block";
            }
            else{

                for(ii=blockCount; ii>currentBlock; ii--){

                    document.getElementById('professional_field'.concat(ii)).value=" ";
                    ClearSelect('specialization'.concat(ii));
                    var newElem = document.createElement("OPTION");
                    newElem.text = " ";
                    newElem.value = " ";
                    document.getElementById('specialization'.concat(ii)).options.add(newElem);
                    document.getElementById('specialization'.concat(ii)).disabled = true;
                    document.getElementById(blockName.concat(ii)).style.display = "none";
                }
            }   
            break;
    }
}

function ClearSelect(selectID){
count = document.getElementById(selectID).options.length;
for(i=0;i<count;i++)
         document.getElementById(selectID).options[0]=null;
}

function FillSelect(selectID,arr){
ClearSelect(selectID);
for(i=0;i<arr.length;i++){
    var pair=arr[i].split(',');
    var newElem = document.createElement("OPTION");
    newElem.text = pair[1];
    newElem.value = pair[0];
    document.getElementById(selectID).options.add(newElem)
     }
document.getElementById(selectID).disabled=(document.getElementById(selectID).options.length==1);
    http.abort();
}

function ShowNextBlock(currentBlockId){
i=currentBlockId.substring(currentBlockId.length-1,currentBlockId.length);
i++;
nextblockId=currentBlockId.substring(0,currentBlockId.length-1)+i;
document.getElementById(nextblockId).style.display="block";
}

function switchFilter(){

if (document.getElementById("filter").style.display=="block"){

      document.getElementById("filter").style.display="none";
      document.getElementById("switch_filter").innerHTML="Показать фильтр";
}else{
      document.getElementById("filter").style.display="block";
      document.getElementById("switch_filter").innerHTML="Скрыть фильтр";
}
}
function switchSearch(){

if (document.getElementById("search").style.display=="block"){

      document.getElementById("search").style.display="none";
      document.getElementById("switch_search").innerHTML="Показать фильтр";
}else{
      document.getElementById("search").style.display="block";
      document.getElementById("switch_search").innerHTML="Скрыть фильтр";
}
}

function getChildsByParent(parentID,childID,filter){

filter=filter||false;

id= encodeURI(document.getElementById(parentID).value);

// Set te random number to add to URL request
nocache = Math.random();
switch(childID){
      case 'city':
             
           dict='city';
           http.onreadystatechange =  updatecity;
           break;
      case 'metro':
             
           dict='metro';
           http.onreadystatechange =  updatemetro;
           break;
      case 'specialization':
           dict='specialization';
           http.onreadystatechange =  updatespecialization;
           break;
      case 'specialization1':
           dict='specialization';
           http.onreadystatechange =  updatespecialization1;
           break;
      case 'specialization2':
           dict='specialization';
           http.onreadystatechange =  updatespecialization2;
           break;
      case 'specialization3':
           dict='specialization';
           http.onreadystatechange =  updatespecialization3;
           break;
      case 'specialization4':
           dict='specialization';
           http.onreadystatechange =  updatespecialization4;
           break;
      case 'specialization5':
           dict='specialization';
           http.onreadystatechange =  updatespecialization5;
           break;
}
queryurl='/ajax/getdictonary.php?dictonary='+dict+'&parentID='+id+'&nocache ='+nocache;
if(filter){
queryurl = queryurl + "&filter=true";
}
http.open('get', queryurl);
http.send(null);

}

function updatecity() {

if(http.readyState == 4)
    FillSelect('city', http.responseText.split('|'));

}
function updatemetro() {

if(http.readyState == 4)
    FillSelect('metro', http.responseText.split('|'));

}
function updatespecialization() {
if(http.readyState == 4)
    FillSelect('specialization', http.responseText.split('|'));

}
function updatespecialization1() {
if(http.readyState == 4)
    FillSelect('specialization1', http.responseText.split('|'));

}
function updatespecialization2() {
if(http.readyState == 4)
    FillSelect('specialization2', http.responseText.split('|'));

}
function updatespecialization3() {
if(http.readyState == 4)
    FillSelect('specialization3', http.responseText.split('|'));

}
function updatespecialization4() {
if(http.readyState == 4)
    FillSelect('specialization4', http.responseText.split('|'));

}
function updatespecialization5() {
if(http.readyState == 4)
    FillSelect('specialization5', http.responseText.split('|'));

}

function CheckDatePart(day,month,year){

switch (month){
    case "1": case "3": case "5": case "7": case "8": case "10": case "12": 
        maxday=31;
        break;
    default:
        maxday=30;
        break;
}

if (month==2){
    maxday=(((year%100)%4)==0)?29:28;

}
return day<=maxday;
}
function CheckDate(date,format){
var re = /^[0-9]*$/;

if (date.length==0) return false;
if (date.length!=format.length) return false;
var arrDate=date.split(".");
var arrFormat=format.split(".");
if (arrDate.length!=arrFormat.length) return false;
switch (format){
    case "YYYY":
       if (!re.test(arrDate[0])) return false;
       if ((arrDate[0]<1900)||(arrDate[0]>2100)) return false;
       break;
    case "MM.YYYY":
       if (!re.test(arrDate[1])) return false;
       if ((arrDate[1]<1900)||(arrDate[1]>2100)) return false;
       if (!re.test(arrDate[0])) return false;
       if ((arrDate[0]<1)||(arrDate[0]>12)) return false;
       break;
    default:
       return false;
}
return true;
}

function checkCommonPart(){
var OK = true;

if(document.getElementById('d_birthday').value==" "||
   document.getElementById('m_birthday').value==" "||
   document.getElementById('y_birthday').value==" "){
     document.getElementById('mes_birthday').innerHTML="Не указана дата рождения.";
     OK=false;
}else{
     if (!CheckDatePart(document.getElementById('d_birthday').value,document.getElementById('m_birthday').value,document.getElementById('y_birthday').value)){
          document.getElementById('mes_birthday').innerHTML="Несуществующая дата.";
          OK = false;
     }else{
          document.getElementById('mes_birthday').innerHTML="";
     }
}

if(!(document.common_part.gender[0].checked||document.common_part.gender[1].checked)){
     document.getElementById('mes_gender').innerHTML="Не выбран пол.";
     OK=false;
}else{
     document.getElementById('mes_gender').innerHTML="";
}


if(document.getElementById('citizenship').value==" "){
     document.getElementById('mes_citizenship').innerHTML="Не выбрано гражданство.";
     OK=false;
}else{
     if ((document.getElementById('citizenship').value=="other")&&(trim(document.getElementById('other_citizenship').value)=="")){
           document.getElementById('mes_citizenship').innerHTML="Не указано \"Другое\" гражданство";
           OK=false;
     }else{
           document.getElementById('mes_citizenship').innerHTML="";
     }
}

if(document.getElementById('city').value==" "){
     document.getElementById('mes_city').innerHTML="Не выбран город.";
     OK=false;
}else{
     document.getElementById('mes_city').innerHTML="";
}

if((trim(document.getElementById('phone').value)=="")&&(trim(document.getElementById('phone2').value)=="")){
     document.getElementById('mes_phone').innerHTML="Нужно указать хотя бы один контактный телефон.";
     OK=false;
}else{
     document.getElementById('mes_phone').innerHTML="";
}

if(document.getElementById('professional_field1').value==" "){
     document.getElementById('mes_professional_field1').innerHTML="Не выбрана профессиональная область.";
     OK=false;
}else{
     document.getElementById('mes_professional_field1').innerHTML="";
}

if(document.getElementById('specialization1').value==" "){
     document.getElementById('mes_specialization1').innerHTML="Не выбрана специализация.";
     OK=false;
}else{
     document.getElementById('mes_specialization1').innerHTML="";
}

if(document.getElementById('experience').value==" "){
     document.getElementById('mes_experience').innerHTML="Не выбран опыт работы.";
     OK=false;
}else{
     document.getElementById('mes_experience').innerHTML="";
}

if(trim(document.getElementById('profit').value)==""){
     document.getElementById('mes_profit').innerHTML="Не указан ожидаемый доход.";
     OK=false;
}else{
     if (isNaN(document.getElementById('profit').value)){
           document.getElementById('mes_profit').innerHTML="Не числовое значение.";
           OK=false;
     }else{
           document.getElementById('mes_profit').innerHTML="";
     }
}

if(document.getElementById('employment').value==" "){
     document.getElementById('mes_employment').innerHTML="Не выбран тип занятости.";
     OK=false;
}else{
     document.getElementById('mes_employment').innerHTML="";
}
return OK;
}


function checkEducationPart(){
var OK = true;

if(document.getElementById('native_lang').value==" "){
     document.getElementById('mes_native_lang').innerHTML="Не выбран родной язык.";
     OK=false;
}else{
     document.getElementById('mes_native_lang').innerHTML="";
}

if(document.getElementById('education').value==" "){
     document.getElementById('mes_education').innerHTML="Не выбрано образование.";
     OK=false;
}else{
     document.getElementById('mes_education').innerHTML="";
}

if(trim(document.getElementById('education_start1').value)==""){
     document.getElementById('mes_education_start1').innerHTML="Не указана дата начала обучения.";
     OK=false;
}else{
     if(!CheckDate(document.getElementById('education_start1').value,"YYYY")){
          document.getElementById('mes_education_start1').innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          document.getElementById('mes_education_start1').innerHTML="";
     }
}
if(trim(document.getElementById('school1').value)==""){
     document.getElementById('mes_school1').innerHTML="Не указано учебное заведение.";
     OK=false;
}else{
     document.getElementById('mes_school1').innerHTML="";
}

for(i=1;i<=3;i++){
     document.getElementById('education_start'+i).value=trim(document.getElementById('education_start'+i).value);
     document.getElementById('education_end'+i).value=trim(document.getElementById('education_end'+i).value);
     document.getElementById('courses_start'+i).value=trim(document.getElementById('courses_start'+i).value);
     document.getElementById('courses_end'+i).value=trim(document.getElementById('courses_end'+i).value);
     if((i!=1)&&(!CheckDate(document.getElementById('education_start'+i).value,"YYYY"))&&(document.getElementById('education_start'+i).value!="")){
          document.getElementById('mes_education_start'+i).innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          if(i!=1) document.getElementById('mes_education_start'+i).innerHTML="";
     }
     if((!CheckDate(document.getElementById('education_end'+i).value,"YYYY"))&&(document.getElementById('education_end'+i).value!="")){
          document.getElementById('mes_education_end'+i).innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          document.getElementById('mes_education_end'+i).innerHTML="";
     }
     if((!CheckDate(document.getElementById('courses_start'+i).value,"MM.YYYY"))&&(document.getElementById('courses_start'+i).value!="")){
          document.getElementById('mes_courses_start'+i).innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          document.getElementById('mes_courses_start'+i).innerHTML="";
     }
     if((!CheckDate(document.getElementById('courses_end'+i).value,"MM.YYYY"))&&(document.getElementById('courses_end'+i).value!="")){
          document.getElementById('mes_courses_end'+i).innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          document.getElementById('mes_courses_end'+i).innerHTML="";
     }
}
for(i=1;i<=5;i++){
     document.getElementById('work_start'+i).value=trim(document.getElementById('work_start'+i).value);
     document.getElementById('work_end'+i).value=trim(document.getElementById('work_end'+i).value);
     if((!CheckDate(document.getElementById('work_start'+i).value,"MM.YYYY"))&&(document.getElementById('work_start'+i).value!="")){
          document.getElementById('mes_work_start'+i).innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          document.getElementById('mes_work_start'+i).innerHTML="";
     }
     if((!CheckDate(document.getElementById('work_end'+i).value,"MM.YYYY"))&&(document.getElementById('work_end'+i).value!="")){
          document.getElementById('mes_work_end'+i).innerHTML="Неверный формат даты.";
          OK=false;
     }else{
          document.getElementById('mes_work_end'+i).innerHTML="";
     }
}
return OK;
}
function checkAdditionalPart(){
var OK = true;

return OK;
}

function checkVacancy(){
var OK = true;

if(trim(document.getElementById('name').value)==""){
     document.getElementById('mes_name').innerHTML="Не указано название вакансии.";
     OK=false;
}else{
     document.getElementById('mes_name').innerHTML="";
}

if(document.getElementById('vacancy_type').value==" "){
     document.getElementById('mes_vacancy_type').innerHTML="Не выбран тип вакансии.";
     OK=false;
}else{
     document.getElementById('mes_vacancy_type').innerHTML="";
}

if(document.getElementById('client_type').value==" "){
     document.getElementById('mes_client_type').innerHTML="Не выбран тип клиента.";
     OK=false;
}else{
     document.getElementById('mes_client_type').innerHTML="";
}

if(document.getElementById('sector').value==" "){
     document.getElementById('mes_sector').innerHTML="Не выбран сектор.";
     OK=false;
}else{
     document.getElementById('mes_sector').innerHTML="";
}

if(document.getElementById('education').value==" "){
     document.getElementById('mes_education').innerHTML="Не выбрано образование.";
     OK=false;
}else{
     document.getElementById('mes_education').innerHTML="";
}
return OK;
}

function checkCandidate_search(){
var OK = true;
var err_str="";
     if (isNaN(document.getElementById('age_from').value)){
           err_str+="В поле 'Возраст от' указано не числовое значение.<br/>";
           OK=false;
     }
     if (isNaN(document.getElementById('age_to').value)){
           err_str+="В поле 'Возраст до' указано не числовое значение.<br/>";
           OK=false;
     }
     if (isNaN(document.getElementById('profit_from').value)){
           err_str+="В поле 'Зарплата от' указано не числовое значение.<br/>";
           OK=false;
     }
     if (isNaN(document.getElementById('profit_to').value)){
           err_str+="В поле 'Зарплата до' указано не числовое значение.<br/>";
           OK=false;
     }

document.getElementById('error').innerHTML=err_str;
return OK;
}