var _CurrentMenu = '';
var _CurrentSubmenu = '';

function setCurrent(id) {
	_CurrentMenu = id;
        document.getElementById(id).className = 'select';
}

function setSubMenu(id) {
	_CurrentSubmenu = id;
        document.getElementById(id).className = 'subactive';
}

function openWin(strLink){
         window.open(strLink,'_blank','toolbar=no,width=400,height=218,scrollbars=yes,status=no')
}

function closeWin() {
        window.open('','_parent','');
        window.close();
}

function appendOption(id,string,val) {

        var elOptNew = document.createElement('option');
        elOptNew.text = string;
        elOptNew.value = val;
        var elSel = document.getElementById(id);

        try {
                elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
                elSel.add(elOptNew); // IE only
        }

}

function removeOptions(id) {

        var elSel = document.getElementById(id);
        while (elSel.length > 0) {
                elSel.remove(elSel.length - 1);
        }

}

function comboItem(ID, ParentID, Value, Sel) {

        this.ID = ID
        this.Value = Value
        this.ParentID = ParentID
        this.Sel = Sel
}

function fillCombo(cmbArray, cmbObj, cmbKey, ind) {

        if (ind) { document.getElementById(ind).style.display = ''; }
        cmbObj.options.length = 0;
        q = 1;
        for (var i = 0; i < cmbArray.length; i++) {
                if (cmbArray[i].ParentID == cmbKey) {
                        cmbObj.options[q] = new Option(cmbArray[i].Value, cmbArray[i].ID, cmbArray[i].Sel);
                        q++;
                }
        }
        if (ind) { document.getElementById(ind).style.display = 'none'; }
}

function comboProv(strarray,prov_obj) {

        removeOptions(prov_obj);
        var num = 0;
        var prov = strarray.split(",");
        appendOption(prov_obj,'-- Selecciona --','');
        while (num < prov.length) {
                var elem = prov[num].split("=");
		if (elem[0] != '') {
                	appendOption(prov_obj,elem[0],elem[1]);
		}
                num += 1;
        }

}

function changeProv(region,prov_obj,ind) {

        document.getElementById(ind).style.display = '';
        var url = '/do/prov.php';
        var pars = 'id_region='+region;

        $.ajax({
	type: "POST",
	url: url,
	data: pars,
        success: function (o) {
             //alert(o);
                comboProv(o,prov_obj);
                document.getElementById(ind).style.display = 'none';
	}
        });

}

function comboCity(strarray,city_obj) {

        removeOptions(city_obj);
	appendOption(city_obj,'-- Selecciona --','');
        var num = 0;
        var cities = strarray.split(";");
        while (num < cities.length) {
                var elem = cities[num].split("=");
		if (elem[0] != '') {
                	appendOption(city_obj,elem[0],elem[1]);
		}
                num += 1;
        }

}

function changeCity(prov,city_obj,ind) {

        document.getElementById(ind).style.display = '';
        var url = '/do/city.php';
        var pars = 'id_prov='+prov;

        $.ajax({
	type: "POST",
	url: url,
	data: pars,
        success: function (o) {
             //alert(o);
                comboCity(o,city_obj);
                document.getElementById(ind).style.display = 'none';
        }
	});

}

function comboJobtitle(strarray,jt_obj) {

        removeOptions(jt_obj);
	appendOption(jt_obj,'-- Selecciona --','');
        var num = 0;
        var jobtitles = strarray.split(",");
        while (num < jobtitles.length) {
                var elem = jobtitles[num].split("=");
		if (elem[0] != '') {
                	appendOption(jt_obj,elem[0],elem[1]);
		}
                num += 1;
        }

}

function changeJobtitle(area,jt_obj,ind) {

        document.getElementById(ind).style.display = '';
        var url = '/do/jobtitle.php';
        var pars = 'id_area='+area;

        $.ajax({
	type: "POST",
	url: url,
	data: pars,
        success: function (o) {
             //alert(o);
                comboJobtitle(o,jt_obj);
                document.getElementById(ind).style.display = 'none';
	}
        });

}

function verifyEmail(s) {
        var chrs = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-@';
        var sLen = s.length; var i=0, c=0, cCnt=0, step=0;
        if (sLen < 6) return false;
        if (s.indexOf('@.')>=0) return false;
        if (s.indexOf('.@')>=0) return false;
        while (i < sLen){
                c=s.charAt(i);
                if (!(chrs.indexOf(c)>=0 || (c=='_' && step<1))) return false;
                if (c=='.') { if (cCnt<1) return false; cCnt=0; }
                if (c=='@') { if (step>0) return false; if (cCnt<1) return false; step++; cCnt=0; }
                cCnt=cCnt+1; i++;
        }
        if (cCnt < 3 || cCnt > 5 || step==0 || (s.indexOf(".")<0) ) return false;
        return true;
}

function findPosX(obj) {

    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;

}

function findPosY(obj) {

    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;

}

function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;
    }
    return new Array(xScroll,yScroll)
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function fadeObj(objid) {

	$('#'+objid).fadeOut(4000);

}

function checkSearch(form) {

	if (form.keywords.value == 'Palabras clave') {
		form.keywords.value = '';
	}
	if (form.place.value == 'Provincia') {
		form.place.value = '';
	}
	return true;

}

function checkCourseSearch(form) {

	if (form.keywords.value == 'Titulo del Curso') {
		form.keywords.value = '';
	}
	if (form.place.value == 'Location') {
		form.place.value = '';
	}
	return true;

}

function quickLogin() {

        email = document.getElementById('ql_email').value;
        pwd = document.getElementById('ql_password').value;
        var url = '/jobseekers/login.php';
        var pars = 'email='+email+'&password='+pwd;

        if (email.length < 1 || pwd.length < 1) {
                alert("Rellena los campos Email/Contraseña!");
        } else {

        $.ajax({
        type: "POST",
        url: url,
        data: pars,
        success: function (o) {
             //alert(o);
                if (o) {
                       //window.location.reload();
                       window.location.href = '/jobseekers/';
                } else {
			alert('Email/Contraseña incorrectos!');
                }
        }
        });

        }

}

function quickPassword(el) {

        var url = '/jobseekers/password.php';

        scrollXY = getPageScroll();
        x = findPosX(el) - scrollXY[0];
        y = findPosY(el) - scrollXY[1];
        $("#dialog").dialog({ zIndex: 3999 });
        //$('#dialog').dialog('option','position',[findPosX(el),findPosY(el)]);
        $('#dialog').dialog('option','position',[x - 40,y + 20]);
        $("#dialog").dialog({ width: 340, show: 'slide' });
        $("#dialog").dialog({ title: '¿Olvidaste la contraseña?' });
        $('#dialog').dialog('open');
        document.getElementById('dialog').innerHTML = '<iframe frameborder="0" name="quickpassword_dialog" src="'+url+'"></iframe>';

}

$(document).ready(function(){
        dialog_div = document.createElement('div');
        dialog_div.id = 'dialog';
        document.body.appendChild(dialog_div);
});


