/**
 * Job Search Block for Canada - English ca_services_en
 *
 * This is currently initialized in the page as follows:
 <script type="text/javascript">
 <!--
  var sJobSearch = { btn_type:"search" , btn_lbl:"Search" , kw_lbl:"Keywords" , cat_lbl:"Job Category" , loc_lbl:"Location" , div_lbl:"Employment Type" };
  JSEARCH.create( sJobSearch , "jsearch" , "lb01" );        
 //-->
 </script>
 */

var JSEARCH = {
  requiredAttrParams: ["btn_type" , "btn_lbl" , "kw_lbl" , "cat_lbl" , "loc_lbl" , "div_lbl" ],
  //declare some params - easier to reference later
  SO:null,
  objTarget:null,
  objParent:null,
  /**
   * Start the the Job Search dropdown(s) creation process
   *
   * @param object SO - an object containing the form parameters
   * @param string id - the document element ID to put the search into
   * @param string parentid - The top level 'containing element'
   */
  create: function( SO , id , parentid ) {
    //stop if the browser is too old
    if( !document.getElementById || !document.getElementsByTagName ) return;
    
    //initialize the object params
    this.SO = SO;
    this.objTarget = document.getElementById(id);
    this.objParent = document.getElementById(parentid);
    
    //hides the parent element
    DROP.setElementDisplay(parentid, "none");
    //adds this load function to the window.onload event
    var me = this; //closure
    var loadfn = function() {
      /*
      checks the params passed in against the DROP required params
      which is actually a smaller set than the 'required' params for
      this object
      */
      if (DROP.hasRequiredAttrParams(SO)) {
        //use the closure to reduce memory usage
        me.init();
      }
      //show the parent block again
      DROP.setElementDisplay(parentid, "block");
    };
    DROP.addLoadEvent(loadfn);
  },
  /**
   * Creates the dropdown list(s)
   */
  init: function(){
    //not sure why this check is here?
    if (typeof this.objTarget.innerHTML == "undefined" || typeof this.objParent.innerHTML == "undefined") { return; }
    
    var id = this.objTarget.id;
    //use an array - string concatenation is very slow for large strings of data
    var strOutPut = [];
    strOutPut = [
      '<form action="" method="post">',
      ' <span class="wrapper">',
      '   <label for="' + id + '_keywords">' + this.SO.kw_lbl + '</label>',
      '   <input type="text" id="' + id + '_keywords" class="txt" maxlength="30" />',
      ' </span>',
      ' <span class="wrapper">',
      '   <label for="' + id + '_category" class="hide">' + this.SO.cat_lbl + '</label>',
      '   <select id="' + id + '_category">',
      '     <option value="">' + this.SO.cat_lbl + '</option>'
    ];
    //categories
    var strOptions = [];
    //optimize the loop by declaring a length var in the initial conditions
    for(var x = 0, len = this.CatItems.length; x < len ; ++x ) {
      var dat = this.CatItems[x]; //var used for convenience
      strOptions.push('     <option value="' + dat.val + '">' + dat.txt + '</option>');
    } 
    strOutPut = strOutPut.concat([
      strOptions.join("\n"),
      '   </select>',
      ' </span>',
      ' <span class="wrapper">',
      '   <label for="' + id + '_location" class="hide">' + this.SO.loc_lbl + '</label>',
      '   <select id="' + id + '_location" onchange="JSEARCH.changeArea(this);">',
      '     <option value="">' + this.SO.loc_lbl + '</option>'
    ]);
    //location
    strOptions = [];
    for( x = 0, len = this.LocItems.length; x < len; ++x ) {
      var dat = this.LocItems[x];
      strOptions.push('   <option value="' + dat.val + '">' + dat.txt + '</option>');
    }
    strOutPut = strOutPut.concat([
      strOptions.join("\n"),
      '   </select>',
      ' </span>',
      //add the areas dropdown
      ' <span class="wrapper">',
      '   <label for="' + id + '_areas" class="hide">Area</label>',
      '   <select id="' + id + '_areas">',
      '     <option value="">Any Area</option>',
      '   </select>',
      ' </span>',
      //done with areas dropdown
      ' <span class="wrapper">',
      '   <label for="' + id + '_division" class="hide">' + this.SO.loc_lbl + '</label>',
      '   <select id="' + id + '_division">',
      '     <option value="">' + this.SO.div_lbl + '</option>'
    ]);
    //division
    strOptions = [];
    for( x = 0, len = this.DivisionItems.length; x < len; ++x ) {
      var dat = this.DivisionItems[x];
      strOptions.push('   <option value="' + dat.val + '">' + dat.txt + '</option>');
    } 
    strOutPut = strOutPut.concat([
      strOptions.join("\n"),
      '   </select>',
      ' </span>',
      ' <a href="#" title="' + this.SO.btn_lbl + '" class="btn btn' + this.SO.btn_type + '" id="' + id + '_btn" target="_blank"><span>' + this.SO.btn_lbl + '</span></a>',
      '</form>'
    ]);

    this.objTarget.innerHTML = strOutPut.join("\n");

    objButton = document.getElementById( id + '_btn' );
    if( objButton ){
      addEvent( objButton , 'click' , JSEARCH.processClick , false );
    }

    addclass( this.objTarget , 'selector' );
    this.objParent.style.overflow = 'visible';
    this.objParent.style.overflow = '';
  },

  addOption: function(obj){
    var dat = obj.split("|");
    //strIdAttrib = id != '' ? ' id="' + id + '"' : '';
    return '<option value="' + dat[1] + '">' + dat[0] + '</option>';
  },
  
  changeArea:function(elem) {
    var id = elem.id.split("_")[0];
    var val = elem[elem.selectedIndex].value
    
    //get the area select element and trunc it
    var sel = document.getElementById(id + '_areas');
    sel.options.length = 0;;
    sel.options[0] = new Option('Any Area',0);
    if (val == "" || val == 0) { 
      return;
    } else {
      //get the object data based on the value of the dropdown
      var areas = this.AreaItems["area" + val];
      
      //fill the select box
      for (var i = 0, len = areas.length; i < len; ++i) {
        sel.options[sel.options.length] = new Option(areas[i].txt,areas[i].val);
      }
    }
  },

  processClick: function(e){
    var curNode = window.event ? window.event.srcElement: e ? e.target : null;
    if( curNode == null ) return;

    divBase = ascendDOM( curNode , 'div' );
    if( divBase.nodeName.toLowerCase() != 'div' || !divBase.id ) return;

    strSelected = 'http://kcnua.jobseu.recruitadvantage.com/job/job_search_result.cfm?cLang=English';

    objKw = document.getElementById( divBase.id + '_keywords' );
    objCat = document.getElementById( divBase.id + '_category' );
    objLoc = document.getElementById( divBase.id + '_location' );
    objArea = document.getElementById( divBase.id + '_areas' );
    objDivision = document.getElementById( divBase.id + '_division' );

    strKw = objKw ? escape( objKw.value ) : '';
    strCat = objCat ? objCat.options[objCat.selectedIndex].value : '';
    strLoc = objLoc ? objLoc.options[objLoc.selectedIndex].value : '';
    strArea = objArea ? objArea.options[objArea.selectedIndex].value : '';
    strDivision = objDivision ? objDivision.options[objDivision.selectedIndex].value : '';
    

    if( strKw == '' && strCat == '' && strLoc == '' && strDivision == '' && strArea == '' ){
      curNode.href = 'http://kcnua.jobseu.recruitadvantage.com/job/job_search.cfm?cLang=English';
    }
    else {

             { 
              if (strCat == '' ){ (strCat = '0' );}
              if (strLoc == '') { (strLoc = '0');}
              if (strDivision == '') {(strDivision = '0' );}
              if (strArea == '') { (strArea = '0');}
             }
           curNode.href = strSelected + '&frm_loc_id=' + strLoc + '&frm_ind_id=' + strCat + '&frm_job_type_id=' + strDivision + '&frm_keyword=' + strKw + '&frm_area_id=' + strArea;

         }

    return true;
  },
  CatItems:[
    {"val": 0,    "txt": "Any"},
{"val": 7666,    "txt": "Accounting"},
{"val": 7667,    "txt": "Administration/Support Services"},
{"val": 7668,    "txt": "Automotive"},
{"val": 7669,    "txt": "Banking - Consumer - Retail"},
{"val": 7675,    "txt": "Banking/ Financial Services"},
{"val": 7672,    "txt": "Banking/ Fund/ Asset Management / Securities"},
{"val": 7674,    "txt": "Banking/ Wealth Management"},
{"val": 7670,    "txt": "Banking/Corporate/Institutional"},
{"val": 7671,    "txt": "Banking/Finance"},
{"val": 7673,    "txt": "Banking/Investment"},
{"val": 7676,    "txt": "Biotechnology/Pharmaceutical"},
{"val": 7677,    "txt": "Computers/Software "},
{"val": 7678,    "txt": "Consulting"},
{"val": 7679,    "txt": "Customer Services "},
{"val": 7680,    "txt": "Education"},
{"val": 7681,    "txt": "Electronic Assembly"},
{"val": 7682,    "txt": "Employment Placement Agencies"},
{"val": 7683,    "txt": "Engineering"},
{"val": 7684,    "txt": "Finance"},
{"val": 7685,    "txt": "FMCG"},
{"val": 7686,    "txt": "Food/ Drink"},
{"val": 7687,    "txt": "General Management"},
{"val": 7688,    "txt": "Healthcare - Laboratory/ Pathology"},
{"val": 7689,    "txt": "Heavy Industrial"},
{"val": 7690,    "txt": "Human Resources"},
{"val": 7691,    "txt": "Information Technology"},
{"val": 7692,    "txt": "Light Industrial"},
{"val": 7693,    "txt": "Logistics"},
{"val": 7694,    "txt": "Manufacturing/ Production"},
{"val": 7695,    "txt": "Marketing"},
{"val": 7696,    "txt": "Material Handling"},
{"val": 7697,    "txt": "Other"}
  ],
  LocItems:[
    {"val": 0,    "txt": "Any"},
{"val": 1,    "txt": "Kyiv"},
{"val": 2,    "txt": "Vinnytsya"},
{"val": 3,    "txt": "Dnipropetrovsk"},
{"val": 4,    "txt": "Donetsk"},
{"val": 5,    "txt": "Zhytomyr"},
{"val": 6,    "txt": "Zaporizhzhia"},
{"val": 7,    "txt": "Ivano-Frankivsk"},
{"val": 8,    "txt": "Kirovohrad"},
{"val": 9,    "txt": "Luhansk"},
{"val": 10,    "txt": "Lutsk"},
{"val": 11,    "txt": "Lviv"},
{"val": 12,    "txt": "Mykolayiv"},
{"val": 13,    "txt": "Odesa"},
{"val": 14,    "txt": "Poltava"},
{"val": 15,    "txt": "Rivne"},
{"val": 16,    "txt": "Simferopol"},
{"val": 17,    "txt": "Crimea"},
{"val": 18,    "txt": "Sumy"},
{"val": 19,    "txt": "Ternopil"},
{"val": 20,    "txt": "Uzhgorod"},
{"val": 21,    "txt": "Kharkiv"},
{"val": 22,    "txt": "Kherson"},
{"val": 23,    "txt": "Khmelnytskyi"},
{"val": 24,    "txt": "Cherkasy"},
{"val": 25,    "txt": "Chernivtsi"},
{"val": 26,    "txt": "Chernihiv"},
{"val": -1,    "txt": "International"}
],
  AreaItems:{
    "area1":[
      {"val":564,"txt":"Kyivska oblast"}
    ],
    "area2":[
      {"val":565,"txt":"Vinnytska oblast"}
    ],
  "area3":[
      {"val":566,"txt":"Dnipropetrovska oblast"}
    ],
    "area4":[
      {"val":567,"txt":"Donetska oblast"}
    ],
      "area5":[
      {"val":568,"txt":"Zhytomyrska oblast"}
    ],
    "area6":[
      {"val":569,"txt":"Zaporizka oblast"}
    ],
      "area7":[
      {"val":570,"txt":"Ivano-Frankivska oblast"}
    ],
    "area8":[
      {"val":571,"txt":"Kirovohradska oblast"}
    ],
    "area9":[
      {"val":572,"txt":"Luhanska oblast"}
    ],
        "area10":[
      {"val":573,"txt":"Volynska oblast"}
    ],
        "area11":[
      {"val":574,"txt":"Lvivska oblast"}
    ],
        "area12":[
      {"val":575,"txt":"Mykolayivska oblast"}
    ],
        "area13":[
      {"val":576,"txt":"Odes'ka oblast"}
    ],
        "area14":[
      {"val":577,"txt":"Poltavska  oblast"}
    ],
        "area15":[
      {"val":578,"txt":"Rivnenska oblast"}
    ],
        "area16":[
      {"val":579,"txt":"Sumska oblast"}
    ],
        "area17":[
      {"val":580,"txt":"Sumska oblast"}
    ],		
        "area18":[
      {"val":581,"txt":"Sumska oblast"}
    ],
            "area19":[
      {"val":582,"txt":"Ternopilska oblast"}
    ],
            "area20":[
      {"val":583,"txt":"Zakarpatska oblast"}
    ],
            "area21":[
      {"val":584,"txt":"Kharkivska oblast"}
    ],
            "area22":[
      {"val":585,"txt":"Khersonska oblast"}
    ],
                "area23":[
      {"val":586,"txt":"Khmelnytska oblast"}
    ],
                "area24":[
      {"val":587,"txt":"Cherkaska oblast"}
    ],
                "area25":[
      {"val":588,"txt":"Chernivetska oblast"}
    ],
                "area26":[
      {"val":589,"txt":"Chernihivska oblast"}
    ]
  },
  DivisionItems:[
   {"val": 0,    "txt": "Any"},
{"val": 2,    "txt": "Contract"},
{"val": 3,    "txt": "Part-Time"},
{"val": 4,    "txt": "Permanent"},
{"val": 5,    "txt": "Seasonal"},
{"val": 6,    "txt": "Temporary"},
{"val": 7,    "txt": "Internal Opportunity"}  ]
};
