

function $(element_id)
{
    return document.getElementById(element_id);
}


Array.prototype.in_array = function ( obj ) 
{
    var len = this.length;
    for ( var x = 0 ; x <= len ; x++ ) 
    {
        if ( this[x] == obj ) return true;
    }
    return false;
}

function openPopup(url, win_width, win_height)
{
    if (!win_width)
        win_width = 800;
    if (!win_height)
        win_height = 700;
        
    var win_left = window.screen.width/2 - win_width/2;
    var win_top  = window.screen.height/2 - win_height/2;

    win = window.open(url, 'win', "height="+win_height+",width="+win_width+", left="+win_left+", top="+win_top+", status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
    win.focus();     
}


function reload_image(img_id)
{
    var img = $(img_id);
    img.src = img.src + '&' +  (new Date()).getTime();
}

function second_counter(start,el_id,redirect_url)
{
    if (start <= 0)
    {
       if (redirect_url)
       {
            document.location = redirect_url;
       }
       else
       {
            var edit = $("edit_"+el_id);
            edit.style.display = "none";           
            return 0;
       }
    }
    
    var el = $("edit_timer_"+el_id);
    el.innerHTML = start;
    start --;
    down=setTimeout("second_counter("+start+",'"+el_id+"', '"+redirect_url+"')",1000); 
    
}   



function check_all()
{
    var e = document.forms.jobsite_work_form.elements;
    var check = document.forms.jobsite_work_form.elements.select_all.checked;
    
    for (i in e)
    {
        if (e[i].type == 'checkbox' && e[i].name == 'check_id[]')
            if (check == true)
                e[i].checked = true;
            else    
                e[i].checked = false;
    }
}


function footer_reposition()
{
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
    {
        //alert('here');
        var footer = $('footer');
        var body = $('body');
        if (footer != null)
        {
            footer.style.top = body.offsetHeight - footer.offsetHeight -1 +"px";
        }
    }
}


function save_next(nextURL,formName)
{
    document.getElementById("save_and_nextEd").value = true;
    formRef = eval("document." + formName);
    formRef.action = nextURL;
    formRef.submit();
}

NextEdNum = 1;
NextWHNum = 1;
NextSNum = 1;
NextLNum = 1;
NextRNum = 1;
NextJSNum = 1;
function AddAnothera(AnotherType)   {
    if (AnotherType == "Education") {
        //NextEdNum = NextEdNum + 1;
        NextEdNum = getNextNum(NextEdNum,'Education','')
        $("numberEducationLines").value = parseFloat($("numberEducationLines").value) + 1;
        var educationAddAnother = new Element('table', {'id':'Education' + NextEdNum,'class':'table_form2', 'cellpadding':'0', 'cellspacing':'0', 'style':'margin-top: 10px;'} ).update('<tr><td><input type="text" class="field" name="ED' + NextEdNum + 'college_name" /></td><th>University / School *</th></tr><tr><td><span id="sYED' + NextEdNum + '"></span> to <span id="eYED' + NextEdNum + '"></span></td><th>Period of Education *</th></tr><tr><td><input type="text" name="ED' + NextEdNum + 'profession" class="field" /></td><th>Details *</th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'Education\', ' + NextEdNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>');
        Element.insert('EducationFields', { bottom:$(educationAddAnother.cloneNode(true)) });
        
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_to_year', 'aWHI': NextEdNum, 'soe':'ED' + NextEdNum + 'start', 'showmonths':'false' },
                         method: 'get',
                         onSuccess: function(transport) {  
                            Element.insert('sYED' + NextEdNum, { bottom:transport.responseText });
                         }
        });
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_to_year', 'aWHI': NextEdNum, 'soe':'ED' + NextEdNum + 'end', 'showmonths':'false' },
                         method: 'get',
                         onSuccess: function(transport) {
                            Element.insert('eYED' + NextEdNum, { bottom:transport.responseText });
                        }
        });
    } else if (AnotherType == "WorkHistory")    {
        //NextWHNum = NextWHNum + 1;
        NextWHNum = getNextNum(NextWHNum,'WorkHistory','')
        $("numberWorkHistoryLines").value = parseFloat($("numberWorkHistoryLines").value) + 1;
        var workHistoryAddAnother = new Element('table', {'id':'WorkHistory' + NextWHNum,'class':'table_form2', 'cellpadding':'0', 'cellspacing':'0', 'style':'margin-top: 10px;'} ).update('<tr><td><input type="text" class="field" name="WH' + NextWHNum + 'employer" /></td><th>Employer*</th></tr><tr><td><input type="text" class="field" name="WH' + NextWHNum + 'position" /></td><th>Position *</th></tr><tr><td><textarea class="field" name="WH' + NextWHNum + 'description"></textarea></td><th>Description</th></tr><tr><td><div id="sYWH' + NextWHNum + '"></div>to<br/><div id="eYWH' + NextWHNum + '"></div></td><th>Period * (if current, leave second row blank)</th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'WorkHistory\', ' + NextWHNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>'); 
        Element.insert('WorkHistoryFields', { bottom:$(workHistoryAddAnother.cloneNode(true)) });
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_to_year', 'aWHI': NextWHNum, 'soe':'WH' + NextWHNum + 'start', 'showmonths':'true', 'year_extra':'100px' },
                         method: 'get',
                         onSuccess: function(transport) {
                            Element.insert('sYWH' + NextWHNum, { bottom:transport.responseText });
                         }
        });
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_to_year', 'aWHI': NextWHNum, 'soe':'WH' + NextWHNum + 'end', 'showmonths':'true', 'year_extra':'100px' },
                         method: 'get',
                         onSuccess: function(transport) {
                            Element.insert('eYWH' + NextWHNum, { bottom:transport.responseText });
                        }
        });
    } else if (AnotherType == "Skills") {
        //NextSNum = NextSNum + 1;
        NextSNum = getNextNum(NextSNum,'Skills','')
        $("numberSkillsLines").value = parseFloat($("numberSkillsLines").value) + 1;
        var skillsAddAnother = new Element('table', {'id':'Skills' + NextSNum,'class':'table_form2', 'cellpadding':'0', 'cellspacing':'0', 'style':'margin-top: 10px;'} ).update('<tr><td><input type="text" class="field" name="S' + NextSNum + 'job_skills" value="" ></td><th>Skill *</th></tr><tr><td><textarea class="field" name="S' + NextSNum + 'description" style="height: 100px;"></textarea></td><th>Description *</th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'Skills\', ' + NextSNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>'); 
        Element.insert('SkillsFields', { bottom:$(skillsAddAnother.cloneNode(true)) });
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_job_categories', 'soe':'S' + NextSNum },
                         method: 'get',
                         onSuccess: function(transport) {
                            Element.insert('sYS' + NextSNum, { bottom:transport.responseText });
                         }
        });
    } else if (AnotherType == "JobSector") {        
        NextJSNum = getNextNum(NextJSNum,'JobSector','')
        $("numberJobSectorLines").value = parseFloat($("numberJobSectorLines").value) + 1;
        var skillsAddAnother = new Element('table', {'id':'JobSector' + NextJSNum,'class':'table_form2', 'cellpadding':'0', 'cellspacing':'0', 'style':'margin-top: 10px;'} ).update('<tr><td><div id="sYJS' + NextJSNum + '"></div></td><th>Job Sector*</th></tr><tr><td><span id="JS' + NextJSNum + 'prepare_job_subcategory_id" style="display: none;"></span><select name="JS' + NextJSNum + 'job_subcategory_id" id="JS' + NextJSNum + 'job_subcategory_id" class="field"><option value="0">Select Sub-Sector...</option></select></td><th>Job Sub-Sector*</th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'JobSector\', ' + NextJSNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>'); 
        Element.insert('JobSectorFields', { bottom:$(skillsAddAnother.cloneNode(true)) });
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_job_categories', 'soe':'JS' + NextJSNum },
                         method: 'get',
                         onSuccess: function(transport) {
                            Element.insert('sYJS' + NextJSNum, { bottom:transport.responseText });
                            correctSizeOverlay();
                         }
        });
        //alert('<tr><td><div id="sYS' + NextJSNum + '"></div></td><th>Job Sector*</th></tr><tr><td><span id="JS' + NextJSNum + 'prepare_job_subcategory_id" style="display: none;"></span><select name="S' + NextJSNum + 'job_subcategory_id" id="S' + NextJSNum + 'job_subcategory_id" class="field"><option value="0">Select Sub-Sector...</option></select></td><th>Job Sub-Sector*</th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'Skills\', ' + NextJSNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>');
    } 
    else if (AnotherType == "Locations")  {
        //NextLNum = NextLNum + 1;
        NextLNum = getNextNum(NextLNum,'L','div_region_id')
        $("numberLocationsLines").value = parseFloat($("numberLocationsLines").value) + 1;
        var locationsAddAnother = new Element('table', {'id':'Locations' + NextLNum,'class':'table_form2', 'cellpadding':'0', 'cellspacing':'0', 'style':'margin-top: 10px;'} ).update('<tr><td><div id="sYL' + NextLNum + '"></div></td><th>Country *</th></tr><tr><td><span id="L' + NextLNum + 'prepare_region_id" style="display: none;"></span><div class="regions_container" id="L' + NextLNum + 'div_region_id">&nbsp;<br/>&nbsp;<br>&nbsp;</div></td><th >State / Region *</th></tr><tr id="L' + NextLNum + 'city_tr" style="display:none;" ><td id="L' + NextLNum + 'prepare_city" >&nbsp;</td><th> City </th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'Locations\', ' + NextLNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>'); 
        Element.insert('LocationsFields', { bottom:$(locationsAddAnother.cloneNode(true)) });
        new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_prepare_country_list', 'soe':'L' + NextLNum },
                         method: 'get',
                         onSuccess: function(transport) {
                            Element.insert('sYL' + NextLNum, { bottom:transport.responseText });
                            correctSizeOverlay();
                         }
        });
    } else if (AnotherType == "References") {
        //NextRNum = NextRNum + 1;
        NextRNum = getNextNum(NextRNum,'References','')
        $("numberReferencesLines").value = parseFloat($("numberReferencesLines").value) + 1;
        var locationsAddAnother = new Element('table', {'id':'References' + NextRNum,'class':'table_form2', 'cellpadding':'0', 'cellspacing':'0', 'style':'margin-top: 10px;'} ).update('<tr><td><input type="text" name="R' + NextRNum + 'emp_firstname" value="" class="field" /></td><th>Referee&#39;s Name *</th></tr><tr><td><input type="text" name="R' + NextRNum + 'emp_phone" value="" class="field" /></td><th>Phone *</th></tr><tr><td><input type="text" name="R' + NextRNum + 'emp_email" value="" class="field" /></td><th>Email </th></tr><tr><td><input type="text" name="R' + NextRNum + 'emp_company_name" value="" class="field" /></td><th>Company </th></tr><tr><td colspan="2"><a href="#" onclick="RemoveAnothera(\'References\', ' + NextRNum + ');return false;">Remove</a></td></tr><tr><td colspan="2"><hr class="hrs"></td></tr>'); 
        Element.insert('ReferencesFields', { bottom:$(locationsAddAnother.cloneNode(true)) });
    }

    
}

function RemoveAnothera(AnotherType, TypeID)    {
    if (AnotherType == "Education") {
        if (confirm("Are you sure you wish to remove this Education entry?"))   {
            $("Education" + TypeID).remove();
        }
    } else if (AnotherType == "WorkHistory")    {
        if (confirm("Are you sure you wish to remove this Work History entry?"))    {
            $("WorkHistory" + TypeID).remove();
        }
    } else if (AnotherType == "Skills") {
        if (confirm("Are you sure you wish to remove this Skill?")) {
            $("Skills" + TypeID).remove();
        }
    } else if (AnotherType == "JobSector") {
        if (confirm("Are you sure you wish to remove this Job Sector?")) {
            $("JobSector" + TypeID).remove();
        }
    } else if (AnotherType == "Locations")  {
        if (confirm("Are you sure you wish to remove this Location?"))  {
            $("Locations" + TypeID).remove();
        }
    } else if (AnotherType == "References") {
        if (confirm("Are you sure you wish to remove this Reference?")) {
            $("References" + TypeID).remove();
        }
    }
    correctSizeOverlay();
}

InviteFields = 5;
function InviteMoreFriends()    {
    if (InviteFields < 50)  {
        for (i = 1; i <= 5; i++)    {
            //InviteFields = InviteFields + 1;
            InviteFields = getNextNum(InviteFields,'friend_email[',']');
            var row = new Template('<tr><td><input type="text" name="friend_email[#{FieldIdent}]" id="friend_email[#{FieldIdent}]" class="field" value="" /></td><th>Friend&#39;s Email #{FieldIdent}</th></tr>');
            var newfields = [{FieldIdent: InviteFields}];
            newfields.each(function(newfielddo){
              new Insertion.Bottom('InviteFriendsFields', row.evaluate(newfielddo));
            });
        }
        if (InviteFields == 50) {
            $("inviteLink").style.display = "none";
        }
    } else  {
        $("inviteLink").style.display = "none";
    }
}

function getNextNum(num,prefix,postfix)
{    
    while($(prefix + num + postfix))
    {
        num = num + 1;
    }
    return num;
}

movingMouseMove = function (event)
{       
    var event = event || window.event;
    imgWidth=$('thumbnail').width;
    imgHeight=$('thumbnail').height;
        
    pageX = getPageX(event);
    pageY = getPageY(event);

    var x=Math.max(0,Math.min(pageX-moved.mx+selected.x,imgWidth-selected.w));
    var y=Math.max(0,Math.min(pageY-moved.my+selected.y,imgHeight-selected.h));
            
           
    $('thumbnail').style.marginLeft = '-' + Math.round(x) + 'px';
    $('thumbnail').style.marginTop= '-' + Math.round(y) + 'px'; 
    //alert(x);
    selected.x = x;
    selected.y = y;
    $('selX').value = selected.x; 
    $('selY').value = selected.y; 

    return false;
}
    
function mouseDownImage(event)
{       
   
    moved ={mx:getPageX(event),my:getPageY(event),f:1};
    $('uploaded_image').onmousemove = movingMouseMove;
    $('uploaded_image').style.cursor ='move';            
    //for(e in event)alert(e+" = "+event[e]);
    document.onmouseup = mouseUpImage;
    return false;
}

mouseUpImage = function (event)
{
    
    $('uploaded_image').onmousemove=null;
    $('uploaded_image').style.cursor='';
    moved ={mx:0,my:0,f:0}
    return false; 
}

opera_nav = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
ie_nav = (navigator.userAgent.indexOf('MSIE') != -1 && !opera_nav) ? true : false;
mozilla_nav = (navigator.userAgent.indexOf('Gecko') != -1) ? true : false;

function getPageX(event)
{
    
    if(ie_nav) return event.offsetX;
    else return event.layerX;
}

function getPageY(event)
{
    
    if(ie_nav) return event.offsetY;
    else return event.layerY;
}

function show_resume_style(url,obj,id)
{
   
   var j = obj.options[obj.selectedIndex].value;  
   $(id).src = url + "/"+"resume_s"+j+".jpg"; 
}

function showPopup(id)
{   
    $("overlay").style.display = 'block';
    $("popups").style.display = 'block';                

    var top = document.documentElement.scrollTop || document.body.scrollTop;
    $("popups").style.top = 250 + top + "px";
    
    updatePopupPosition();
    correctSizeOverlay();        
    

}

function updatePopupPosition() 
{   
    var dw = document.body.clientWidth || document.documentElement.clientWidth;
    var pw = $("popups").offsetWidth;
    $("popups").style.left = Math.ceil(0.5 * (dw - pw)) + "px";
}

function correctSizeOverlay()
{
    if($("overlay"))
    {   
        var height = document.body.clientHeight || document.documentElement.clientHeight;        
        var width = document.body.clientWidth || document.documentElement.clientWidth;
        if(height<$("popups").offsetHeight+$("popups").offsetTop) height = height + ($("popups").offsetHeight+$("popups").offsetTop-height);        
        $("overlay").style.height = height + "px";
        //$("overlay").style.width = width + "px";
    }
}

function chekJobSectorLocation()
{
  showPopup();
  new Ajax.Request(site_root_sc, {
                         parameters: { 'rm': 'ajax_sector_location', 'soe':'CJSL' },
                         method: 'get',
                         onSuccess: function(transport) {                            
                            var error = transport.responseText.substr(0,1);
                            transport.responseText =transport.responseText.substr(1,transport.responseText.length);                            
                            Element.insert('sYCJSL', { bottom:transport.responseText });
                            correctSizeOverlay();                            
                         }
                 });

}

function submitJobSectorLocation()
{   
    fields = new Object();
    tags=new Array();
    var col =  scan_tags($("sYCJSL"));    
    col['rm']= 'ajax_sector_location';
    col['soe']='CJSL';
    new Ajax.Request(site_root_sc, {
                         parameters: col,
                         method: 'post',
                         onSuccess: function(transport) {
                            
                            var error = transport.responseText.substr(0,1)*1;
                            transport.responseText =transport.responseText.substr(1,transport.responseText.length);
                            Element.update('sYCJSL', transport.responseText );
                            correctSizeOverlay();
                            if(!error)
                            {
                                $("overlay").style.display = 'none';
                                $("popups").style.display = 'none';    
                            }  
                         }
                 });

}

var tags=new Array();
var fields = new Object();
function scan_tags(tag)
{   
    if(tag.childNodes) 
    {
        for(var t in tag.childNodes) 
        if(tag.childNodes[t].nodeName)
        {   
            var child=tag.childNodes[t].nodeName.toLowerCase();
            if(child=="input" || child=="textarea" || child=="select")
            {
                var exst=false;

                for(var v in tags)
                if(tags[v]==tag.childNodes[t])
                {
                    exst=true;break;
                }
                if(!exst) 
                {
                    tags[tags.length]=tag.childNodes[t];
                    
                    if(child=="input" && tag.childNodes[t].type=='checkbox')
                    {
                        if(tag.childNodes[t].checked)
                        {   
                            if(tag.childNodes[t].name.substr(tag.childNodes[t].name.length-2,tag.childNodes[t].name.length)=="[]")
                            {   
                                if(!fields[tag.childNodes[t].name]) fields[tag.childNodes[t].name]=new Array();
                                ck_length = fields[tag.childNodes[t].name].length;
                                fields[tag.childNodes[t].name][ck_length]=tag.childNodes[t].value;    
                            }
                            else fields[tag.childNodes[t].name]=tag.childNodes[t].value;
                        }  
                    }
                    else fields[tag.childNodes[t].name]=tag.childNodes[t].value;
                    
                }
            }
            scan_tags(tag.childNodes[t]);
        }
    }
    return fields;
}

function users_search(value)
{   
    params= {'rm': 'ajax_users_search', 'search': value }; 
    new Ajax.Request(site_root_sc, {
                         parameters: params,
                         method: 'get',
                         onSuccess: function(transport) 
                                    {                            
                                        Element.update('users_search_result', transport.responseText );
                                    }
                 });
  
}
