var fac 	= new function()
{
	this.populate		=	function(o)
	{
		params	=	"uID=" + o.value;	
		this.sendRequest(params);
	}
    
    this.reset = function() {
        if ( $("facultati").hasChildNodes() )
        {
            while ( $("facultati").childNodes.length >= 1 )
            {
                $("facultati").removeChild( $("facultati").firstChild );
            }
        }
      /*  var select = $('facultati');
        for(i = 1; i < select.length; i++) {
            try {
                select.remove(select[i]);
            } catch(e) {
            };
        }*/
    }
	
	this.sendRequest	=	function(params)
	{				
        this.reset();                      
		hdr ="Content-type", "application/x-www-form-urlencoded";
        hdr +="Content-length", params.length;
      	hdr +="Connection", "close";
      	hdr +="Accept",	"text/javascript, text/html, application/xml, text/xml";
			
		url	=	'cv_request.php?action=faculty';
			  new Ajax.Request(url,{
			  method: 'post',
			  headers: hdr,
			  parameters: params,
			  onSuccess: function(transport) {	
			  var string	=	transport.responseText;
			  var opts		=	string.split("^");
			  
		  	  for(i = 0; i <= opts.length - 2; i++)
				{
					option	=	opts[i];
					
					s		=	option.split("*");
					id 		=	s[0];
					name	=	s[1];
					if(name !== "undefined"){
					opt		=	document.createElement('option');
					opt.setAttribute('name', id);
					opt.setAttribute('id', "fac" + id);
					opt.setAttribute('value', id);
                    opt.text = name
                    opt.selected = true;
                    try {
					    $('facultati').add(opt, null);
					} catch(e) {
                        $('facultati').add(opt);
                    }
                    }
                    
				}
			  }
			});

	}
	this.addstudies		=	function()
	{
		var catcher		=	this.validateStudies();
		if(catcher == true)
		{
			var uval	=	$('universitati').value;
			var utext	=	$('univ'+uval).innerHTML;
		
			var fval	=	$('facultati').value;
			var ftext	=	$('fac'+fval).innerHTML;
			
			var spc		=	$('specialised').value;
			
			var sval	=	$('syear').value;
			var stext	=	$('syear'+sval).innerHTML;
			
			var id		=	uval+"|"+fval+"|"+spc+"|"+sval;
			var val		=	utext + " / " + ftext + " / " + spc + " / " + stext; 
			
			opt		=	document.createElement('option');
			opt.setAttribute('name', id);
			opt.setAttribute('id', "fac" + id);
            opt.setAttribute('value', id);
			optval	=	document.createTextNode(val);					
			opt.appendChild(optval);
            opt.selected = true;
			$('studies').appendChild(opt);
			$('universitati').value = '';
            $('facultati').innerHTML = '';
            $('specialised').value = '';
            $('syear').value = '';
		}	else {
			alert(catcher);
		}
		
		
	}
	

    
	this.remstudies		=	function()
	{
		var optvar	=	$('studies').value;
		if(optvar == "") alert("Selectati intrarea ce doriti a fi stearsa");
		else{
			var el	=	$('studies').options[$('studies').selectedIndex].id;
			$('studies').removeChild(document.getElementById(el));
		}
	}
	
	this.validateStudies	=	function()
	{
		var	error	=	"";
		
		if($('universitati').value == "")	error	+=  "Campul universitati nu este completat \n";
		if($('specialised').value == "") 	error	+=  "Campul specializare nu este completat \n";
		if($('facultati').value == "")		error	+=  "Campul facultati nu este completat \n";
		if($('syear').value	==	 "")		error	+=  "Campul an de studii nu este completat \n";
		
		if(error.length == 0)	return true;
		else return error;	
	}
}

var lb = new function() {
    this.adauga = function() {
        limbi = $('limbi');
        limba = $('limba');
        vorbit = $('vorbit');
        citit = $('citit');
        scris = $('scris');
        if(!limba.value) {
            alert('Selectati limba');
            return false;
        }
        l_id = limba.options[limba.selectedIndex].value;
        l_name = limba.options[limba.selectedIndex].text;
        v = vorbit.value;
        c = citit.value;
        s = scris.value;
        val = l_id+'|'+v+'|'+c+'|'+s;
        txt = l_name+' / '+v+' / '+c+' / '+s;
        if(!$(val)) {
            option = new Element(
                'option', {
                    id: val,
                    value: val
                }
            );
            option.text = txt;
            option.selected = true;
            try {
                        limbi.add(option, null);
            } catch(e) {
                        limbi.add(option);
            }
            //limbi.appendChild(option);
        }
    }
    
    this.sterge = function() {
        limbi = $('limbi');
        limba = $('limba');
        vorbit = $('vorbit');
        citit = $('citit');
        scris = $('scris');
        try {
            limbi.remove(limbi.selectedIndex);
        } catch(e) {
        }
    }
}

function mark() {
    try {
        studies = $('studies');
        for(i = 0; i < studies.length; i++) {
            studies[i].selected = true;
        }
    } catch(e) {
    }
    
    try {
        limbi = $('limbi');
        for(i = 0; i < limbi.length; i++) {
            limbi[i].selected = true;
        }
    } catch(e) {
    }
    
    try {
        exp = $('exp'); 
        for(i = 0; i < exp.length; i++) {
            exp[i].selected = true;
        }
    } catch(e) {
    }
    
    return true;
}


var xp = new function() {
    this.adauga = function() {
        post = $('ex_post');
        firma = $('ex_firma');
        dom = $('ex_dom');
        an_d = $('ex_an_d');
        luna_d = $('ex_luna_d');
        zi_d = $('ex_zi_d');
        de_la = an_d.value+'-'+luna_d.value+'-'+zi_d.value;
        an_p = $('ex_an_p');
        luna_p = $('ex_luna_p');
        zi_p = $('ex_zi_p');
        pana_la = an_p.value+'-'+luna_p.value+'-'+zi_p.value;
        arii = $('ex_arii');
		o_lucru = $('o_lucru');
        resp = $('ex_resp');
		ariies = '';
		for(i = 0; i < arii.length; i++) {
			if(arii[i].selected == true) {
				ariies += arii[i].value+';';
			};
		}
        if(post.value.length == 0) {
            alert('Introduceti postul');
            return false;
        }
        if(firma.value.length == 0) {
            alert('Introduceti firma');
            return false;
        }
        if(an_d.value.length == 0) {
            alert('Introduceti anul');
            return false;
        }
        if(luna_d.value.length == 0) {
            alert('Introduceti luna');
            return false;
        }
        if(zi_d.value.length == 0) {
            alert('Introduceti ziua');
            return false;
        }
        if(dom.value.length == 0) {
            alert('Introduceti domeniu de activitate');
            return false;
        }
		if(o_lucru.value.length == 0) {
			alert('Alegeti orasul de lucru');
            return false;
		}
        val = post.value+'|'+firma.value+'|'+dom.value+'|'+de_la+'|'+pana_la+'|'+ariies+'|'+o_lucru.value+'|'+resp.value;
        value = firma.value + '|' + de_la + '|' + pana_la;
        if(!$(val)) {
            option = new Element(
                'option', {
                    id: val,
                    value: val
                }
            );
            option.text = value;
            option.selected = true;
            try {
                        $('exp').add(option, null);
            } catch(e) {
                        $('exp').add(option);
            }
        }
        return false;    
    }
    
    this.sterge = function() {
        try {
            $('exp').remove($('exp').selectedIndex);
        } catch(e) {
        }
    }
}

function disable_pana_la() {
    if($('ex_an_p').disabled == false) {
        $('ex_an_p').disabled = true;
        $('ex_luna_p').disabled = true;
        $('ex_zi_p').disabled = true;
    } else {
        $('ex_an_p').disabled = false
        $('ex_luna_p').disabled = false;
        $('ex_zi_p').disabled = false;
    }
}
