﻿// JScript File
var root_folder = '/cvdb/';
function removejscssfile(filename, filetype)
{
   var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
   var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
   var allsuspects=document.getElementsByTagName(targetelement)
   //alert(allsuspects[0].getAttribute(targetattr));
   for (var i=allsuspects.length; i>=0; i--)
   { //search backwards within nodelist for matching elements to remove
       //alert(allsuspects.getAttribute(targetattr));
       if (allsuspects && allsuspects.getAttribute(targetattr)!=null && allsuspects.getAttribute(targetattr).indexOf(filename)!=-1)
       allsuspects.parentNode.removeChild(allsuspects) //remove element by calling parentNode.removeChild()
   }
}
function isScriptAvaiable(filename, filetype)
{
   var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
   var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
   var allsuspects=document.getElementsByTagName(targetelement)
  
   //alert(allsuspects[0].getAttribute(targetattr));
   for (var i=allsuspects.length; i>=0; i--)
   { //search backwards within nodelist for matching elements to remove
       //alert(allsuspects.getAttribute(targetattr));
       if (allsuspects && allsuspects.getAttribute(targetattr)!=null && allsuspects.getAttribute(targetattr).indexOf(filename)!=-1)
       //allsuspects.parentNode.removeChild(allsuspects) //remove element by calling parentNode.removeChild()
       return true;
   }
   return false;
}

function createjscssfile(filename, filetype){
   if (filetype=="js")
   { //if filename is a external JavaScript file
       var fileref=document.createElement('script')
       fileref.setAttribute("type","text/javascript")
       fileref.setAttribute("src", root_folder.concat('lib/static/js/').concat(filename))
   }
   else if (filetype=="css"){ //if filename is an external CSS file
       var fileref=document.createElement("link")
       fileref.setAttribute("rel", "stylesheet")
       fileref.setAttribute("type", "text/css")
       fileref.setAttribute("href", root_folder.concat('lib/static/css/').concat(filename))
   }
   return fileref;
}
//replacejscssfile("oldscript.js", "newscript.js", "js") //Replace all occurences of "oldscript.js" with "newscript.js"
//replacejscssfile("oldstyle.css", "newstyle", "css") //Replace all occurences "oldstyle.css" with "newstyle.css"
function replacejscssfile(oldfilename, newfilename, filetype)
{
   var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
   var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
   var allsuspects=document.getElementsByTagName(targetelement)
   for (var i=allsuspects.length; i>=0; i--)
   { //search backwards within nodelist for matching elements to remove
       if (allsuspects && allsuspects.getAttribute(targetattr)!=null && allsuspects.getAttribute(targetattr).indexOf(oldfilename)!=-1)
       {
           var newelement=createjscssfile(newfilename, filetype)
           allsuspects.parentNode.replaceChild(newelement, allsuspects)
       }
   }
}
function loadjscssfile(filename, filetype){ //load or replace (if already exists) external .js and .css files
       var newelement=this.createjscssfile(filename, filetype)
       document.getElementsByTagName("head")[0].appendChild(newelement);
} 

function init()
{
    loadjscssfile('qs_global.css','css');
    loadjscssfile('qs_tab.css','css');
    loadjscssfile('default.css','css');
    loadjscssfile('SelectOptionForm.css','css');
    loadjscssfile('GridStyle.css','css');
    loadjscssfile('qs_tab.css','css');
    loadjscssfile('kscv_setting.js','js');
    loadjscssfile('qs_global.js','js');
    loadjscssfile('kscv_function.js','js');
}

if (/msie/i.test (navigator.userAgent)) //only override IE
{
    document.nativeGetElementById = document.getElementById;
    document.getElementById = function(id)
    {
        var elem = document.nativeGetElementById(id);
        if(elem)
        {
	        //make sure that it is a valid match on id
	        if(elem.id == id)
	        {
		        return elem;
	        }
	        else
	        {
		        //otherwise find the correct element
		        for(var i=1;i<document.all[id].length;i++)
		        {
			        if(document.all[id][i].id == id)
			        {
				        return document.all[id][i];
			        }
		        }
	        }
        }
        return null;
    };
}


function initcommon()
{
    var isactive = readCookie('isactive');
    if(isactive!=null)
    {
        $('#basic-modal-content').modal();
        var email= document.getElementById('txt_email');
        email.value = isactive;
        //document.getElementById('txt_password').focus();
        //alert(email.value);
        eraseCookie('isactive');
    }
    var userId = readCookie('user_id');
    if(userId!=null)
    {
        //save job to my list
        var jobad = readCookie('job_save');
         if(jobad!=null && userId!='0')
        {
            load_list_ad();
            eraseCookie('job_save');
        }
        //SubscribeJob
        var subcr_job = readCookie('job_sub');
        if(subcr_job!=null)
        {
            SubscribeJob(userId,get_query_string_sid()); 
            eraseCookie('job_sub');
            eraseCookie('user_id');
        }
        //subr job ad right box
        var right_subscribe_ads = readCookie('right_subscribe_ads');
        if(right_subscribe_ads!=null)
        {
            jobad_subscribe_to(userId,get_query_string_sid()); 
            eraseCookie('right_subscribe_ads');
            eraseCookie('user_id');
        }
        //add company interest job ad right box
        var bid = readCookie('right_add_com_interest');
        if(bid!=null)
        {
            jobad_add_companies_Of_interest(userId,bid); 
            eraseCookie('right_add_com_interest');
            eraseCookie('user_id');
        }
        //Add to companies of interested
        var url_bedrift = readCookie('CompaniesOfInterest');
        if(url_bedrift!=null)
        {
            AddToCompaniesOfInterest(userId,get_query_string_bid(),''); 
            eraseCookie('CompaniesOfInterest');
            eraseCookie('user_id');
        }
        //Subcr this company
        var isSubscribeCompany = readCookie('is_Subscribe_Company');
        if(isSubscribeCompany!=null)
        {
            SubscribeCompany(userId,get_query_string_bid()); 
            eraseCookie('is_Subscribe_Company');
            eraseCookie('user_id');
        }
        var user_com_id=readCookie('company_user_id');
        if(user_com_id!=null)
        {
            Send_melding_check_login(userId,'',user_com_id);
            eraseCookie('company_user_id');
            eraseCookie('user_id');
            eraseCookie('send_melding');
        }
       
    }
}