/**
 * 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="0">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://ca.livejobs.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://ca.livejobs.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": 3446, "txt": "Accounting"}, 
    {"val": 3447, "txt": "Administrative"}, 
    {"val": 3448, "txt": "Agricultural"}, 
    {"val": 3449, "txt": "Automotive"}, 
    {"val": 3450, "txt": "Banking/Financial Services"}, 
    {"val": 3451, "txt": "Biotechnology & Pharmaceutical"}, 
    {"val": 3452, "txt": "Call Centre"}, 
    {"val": 3453, "txt": "Catering/Food and Beverage"}, 
    {"val": 3454, "txt": "Chief Engineer"}, 
    {"val": 3455, "txt": "Clinical"}, 
    {"val": 3456, "txt": "Computers - Hardware"}, 
    {"val": 3457, "txt": "Computers - Software"}, 
    {"val": 3458, "txt": "Consulting"}, 
    {"val": 3459, "txt": "Creative Services"}, 
    {"val": 3460, "txt": "Customer Services"}, 
    {"val": 3461, "txt": "Education"}, 
    {"val": 3462, "txt": "Electronic Assembly"}, 
    {"val": 3463, "txt": "Employment Placement Agencies"}, 
    {"val": 3464, "txt": "Engineering"}, 
    {"val": 3465, "txt": "Finance"}, 
    {"val": 3466, "txt": "Food and Drink"}, 
    {"val": 3467, "txt": "General Management"}, 
    {"val": 3468, "txt": "Government"}, 
    {"val": 3469, "txt": "Healthcare"}, 
    {"val": 3470, "txt": "Healthcare - Laboratory/Pathology"}, 
    {"val": 3471, "txt": "Heavy Industrial"}, 
    {"val": 3472, "txt": "Home Care"}, 
    {"val": 3473, "txt": "Hospitality"}, 
    {"val": 3474, "txt": "Human Resources"}, 
    {"val": 3475, "txt": "Industrial/Driving"}, 
    {"val": 3476, "txt": "Information Technology"}, 
    {"val": 3477, "txt": "Insurance"}, 
    {"val": 3478, "txt": "Legal"}, 
    {"val": 3479, "txt": "Light Industrial"}, 
    {"val": 3480, "txt": "Logistics"}, 
    {"val": 3481, "txt": "Manufacturing and production"}, 
    {"val": 3482, "txt": "Marketing"}, 
    {"val": 3483, "txt": "Material Handling"}, 
    {"val": 3484, "txt": "Media"}, 
    {"val": 3485, "txt": "Nursing"}, 
    {"val": 3486, "txt": "Office"}, 
    {"val": 3487, "txt": "Oil/Gas/Utilities"}, 
    {"val": 3488, "txt": "Other"}, 
    {"val": 3489, "txt": "Professional (Other)"}, 
    {"val": 3490, "txt": "Project Engineer"}, 
    {"val": 3491, "txt": "Purchasing"}, 
    {"val": 3492, "txt": "Quality control"}, 
    {"val": 3493, "txt": "Recruiter"}, 
    {"val": 3494, "txt": "Sales Promotion"}, 
    {"val": 3495, "txt": "Sales/Retail"}, 
    {"val": 3496, "txt": "Science/Environmental"}, 
    {"val": 3497, "txt": "Scientific"}, 
    {"val": 3498, "txt": "Scientist"}, 
    {"val": 3499, "txt": "Shop"}, 
    {"val": 3500, "txt": "Skilled Trades"}, 
    {"val": 3501, "txt": "Technician-Engineering"}, 
    {"val": 3502, "txt": "Trades - Other"}, 
    {"val": 3503, "txt": "Transport"}
  ],
  LocItems:[
    {"val": 0,    "txt": "Any"}, 
    {"val": 1303, "txt": "Alberta"}, 
    {"val": 1304, "txt": "British Columbia"}, 
    {"val": 1305, "txt": "Manitoba"}, 
    {"val": 1306, "txt": "New Brunswick"}, 
    {"val": 1307, "txt": "Newfoundland"}, 
    {"val": 1308, "txt": "Northwest Territory"}, 
    {"val": 1309, "txt": "Nova Scotia"}, 
    {"val": 1310, "txt": "Nunavut"}, 
    {"val": 1311, "txt": "Ontario"}, 
    {"val": 1312, "txt": "Prince Edward Island"}, 
    {"val": 1313, "txt": "Quebec"}, 
    {"val": 1314, "txt": "Saskatchewan"}, 
    {"val": 1315, "txt": "Yukon Territory"}, 
    {"val": -1,   "txt": "International"}
  ],
  AreaItems:{
    "area1303":[
      {"val":481,"txt":"Calgary and area"},
      {"val":482,"txt":"Edmonton and area"},
      {"val":483,"txt":"Fort McMurray and area"},
      {"val":484,"txt":"Grande Prairie and area"},
      {"val":485,"txt":"Lethbridge and area"},
      {"val":486,"txt":"Medicine Hat and area"},
      {"val":487,"txt":"Red Deer and area"},
      {"val":537,"txt":"Other"}
    ],
    "area1304":[
      {"val":489,"txt":"Abbotsford and area"},
      {"val":490,"txt":"Kamloops and area"},
      {"val":491,"txt":"Kelowna and area"},
      {"val":492,"txt":"Nanaimo and area"},
      {"val":488,"txt":"Nelson and area"},
      {"val":493,"txt":"Prince George and area"},
      {"val":494,"txt":"Vancouver and area"},
      {"val":495,"txt":"Victoria and area"},
      {"val":538,"txt":"Other"}
    ],
    "area1305":[
      {"val":496,"txt":"Brandon and area"},
      {"val":497,"txt":"Winnipeg and area "},
      {"val":539,"txt":"Other"}
    ],
    "area1306":[
      {"val":498,"txt":"Fredericton and area"},
      {"val":499,"txt":"Moncton and area"},
      {"val":500,"txt":"Saint John and area"},
      {"val":540,"txt":"Other"}
    ],
    "area1307":[
      {"val":501,"txt":"St. John's and area"},
      {"val":541,"txt":"Other"}
    ],
    "area1308":[
      {"val":548,"txt":"Yellowknife and area"},
      {"val":549,"txt":"Other"}
    ],
    "area1309":[
      {"val":547,"txt":"Cape Breton and Area"},
      {"val":502,"txt":"Halifax and area"},
      {"val":542,"txt":"Other"}
    ],
    "area1310":[
      {"val":552,"txt":"Iqualit and area"},
      {"val":553,"txt":"Other"}
    ],
    "area1311":[
      {"val":503,"txt":"Barrie and area"},
      {"val":504,"txt":"Belleville and area"},
      {"val":554,"txt":"Bolton and Area"},
      {"val":555,"txt":"Brampton and Area"},
      {"val":505,"txt":"Brantford and area"},
      {"val":556,"txt":"Cambridge and Area"},
      {"val":557,"txt":"Chatham and Area"},
      {"val":506,"txt":"Guelph and area"},
      {"val":507,"txt":"Hamilton and area"},
      {"val":508,"txt":"Kingston and area"},
      {"val":509,"txt":"Kitchener and area"},
      {"val":510,"txt":"London and area"},
      {"val":511,"txt":"Markham and area"},
      {"val":512,"txt":"Mississauga and area"},
      {"val":558,"txt":"Newmarket and Area"},
      {"val":513,"txt":"North Bay and area"},
      {"val":559,"txt":"North York and Area"},
      {"val":514,"txt":"Oakville and area"},
      {"val":515,"txt":"Oshawa and area"},
      {"val":516,"txt":"Ottawa and area"},
      {"val":517,"txt":"Peterborough and area"},
      {"val":560,"txt":"Sarnia and Area"},
      {"val":518,"txt":"Sault Ste. Marie and area"},
      {"val":561,"txt":"Scarborough and Area"},
      {"val":519,"txt":"Simcoe and area"},
      {"val":520,"txt":"St. Catharines and area"},
      {"val":521,"txt":"Sudbury and area"},
      {"val":522,"txt":"Thunder Bay and area"},
      {"val":523,"txt":"Toronto and area"},
      {"val":524,"txt":"Windsor and area"},
      {"val":562,"txt":"WoodStock and Area"},
      {"val":543,"txt":"Other"}
    ],
    "area1312":[
      {"val":525,"txt":"Charlottetown and area"},
      {"val":544,"txt":"Other"}
    ],
    "area1313":[
      {"val":526,"txt":"Drummondville and area"},
      {"val":527,"txt":"Gatineau and area"},
      {"val":563,"txt":"Granby and Area"},
      {"val":528,"txt":"Laval and area"},
      {"val":529,"txt":"Longueuil and area"},
      {"val":530,"txt":"Montréal and area"},
      {"val":531,"txt":"Québec and area"},
      {"val":532,"txt":"Saguenay and area"},
      {"val":533,"txt":"Sherbrooke and area"},
      {"val":534,"txt":"Trois-Rivières and area"},
      {"val":545,"txt":"Other"}
    ],
    "area1314":[
      {"val":535,"txt":"Regina and area"},
      {"val":536,"txt":"Saskatoon and area"},
      {"val":546,"txt":"Other"}
    ],
    "area1315":[
      {"val":550,"txt":"Whitehorse and area"},
      {"val":551,"txt":"Other"}
    ]
  },
  DivisionItems:[
    {"val":  0, "txt": "Any"}, 
    {"val":  5, "txt": "Casual"}, 
    {"val":  3, "txt": "Contract"}, 
    {"val": 13, "txt": "Ongoing"}, 
    {"val":  4, "txt": "Part-Time"}, 
    {"val":  2, "txt": "Permanent"}, 
    {"val": 12, "txt": "Seasonal"}, 
    {"val": 15, "txt": "Temporary"},
    {"val": 16, "txt": "Kelly Internal Perm Positions"}
  ]
};
