var global_photo_filename="";

function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest(); } catch(e) {} 
   alert("XMLHttpRequest not supported");
   return null;
};

function uptab(tab){
	for(var i=0; i<3; i++){
		if(document.getElementById('tab_'+i)){
			document.getElementById('tab_'+i).className="tabno";
		}	
	}
	document.getElementById(tab).className="tabok";
	upcontent(tab, 0);
}

function upcontent(tab, ipag) {
	setStatus ('contentdiv');
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function () { 
          if (xmlhttp.readyState==4) {
               if (xmlhttp.status==200) {
                    document.getElementById('contentdiv').innerHTML=xmlhttp.responseText;
               } 
          }
     };
     xmlhttp.open("GET", "worksource/home/includes/tab_content.php?tab="+tab+"&pag="+ipag);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); 
     xmlhttp.send(null);
}

function submitprofile(){
	var na=0;
	for (var i=0; i < document.ws_frm.on_profile.length; i++) {
		if (document.ws_frm.on_profile[i].checked){
      		var idprofile = document.ws_frm.on_profile[i].value;
      		na+=1;
      	}
   	}	
	
	if(na==1){
		document.ws_frm.submit();
	} else if(na==0){
	 	alert("Para continuar é necessário selecionar um perfil");
		return false;
	} else {
		return false;
	}

}

function delete_curriculo(){
 	if (!confirm('Tem certeza que excluir definitivamente seu currículo dos nossos registros?')) return false;
	var xmlhttp=Inint_AJAX();
	xmlhttp.onreadystatechange = function () { 
	      if (xmlhttp.readyState==4) {
	           if (xmlhttp.status==200) {
	                var res=xmlhttp.responseText;
	                if(res==1){
	    				alert("Currículo excluído com sucesso.");
	    				window.location="index.php?ch=trabalheconosco";
	    			} else {
	    				alert("Houve um erro ao tentar excluir seu currículo! Tente novamente mais tarde.");
	    				return false;
	    			}
	           } 
	      }
	 };
	 xmlhttp.open("GET", "worksource/home/includes/curriculo_delete.php");
	 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); 
	 xmlhttp.send(null);	
}

function updateProfile_a(curr_id){
	var objbox=document.getElementById('placeboxprofileinfo');
	var currentcontent=objbox.innerHTML;
	
	var se = document.createElement("select");
	se.name = "selectName";
	se.setAttribute('id', 'n_profile');
	se.className="campo_a";
	se.style.width="160px";	
	se.disabled=true;
	se.onchange=function(){
		updateProfile_b(currentcontent, this);
	}

	objbox.innerHTML="";
	objbox.appendChild(se);	
	var select_inp=document.getElementById('n_profile');
	
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function () { 
          if (xmlhttp.readyState==4) {
               if (xmlhttp.status==200) {
                    var ah=xmlhttp.responseXML;
					var tagItem=ah.getElementsByTagName('item');
					
					select_inp.appendChild(createOption("Selecione um perfil", "0"));
				
					for(var i=0; i<tagItem.length; i++){
						var n_option=createOption(tagItem[i].childNodes[0].data, tagItem[i].getAttribute("value"));
						select_inp.appendChild(n_option);
					}
					selectOptionValue('n_profile', curr_id);
					select_inp.disabled=false;
					                    
               } 
          }
     };
     xmlhttp.open("GET", "worksource/home/includes/return_xml.php?c=profiles");
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
     xmlhttp.send(null);	
	
}

function selectOptionValue(id, value){
	var selectobj=document.getElementById(id);
	var on=0;
	for(var i=0; i<selectobj.length; i++){
		if(selectobj.options[i].value==value){
			selectobj.selectedIndex=i;
			on++;
		}
	}
	if(on==1) return true;
	else return false;
}

function getTextXML(code, tag){
	var getTag=code.getElementsByTagName(tag);
	if(getTag[0].childNodes.length==0) return "";
	return getTag[0].childNodes[0].data;
}

function updateProfile_b(cn, me){
	if(me.value=="0"){ 
		alert("Por favor, selecione um perfil.");
		me.focus();
		return false; 
	}
	me.disabled=true;
	var obb=me.parentNode;
	var selectedname=me.options[me.selectedIndex].text;
	
	var params;
	params = "id_profile="+me.value;	
	
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function () { 
          if (xmlhttp.readyState==4) {
               if (xmlhttp.status==200) {
                	var res=xmlhttp.responseText;
                	if(res=="1"){
						obb.innerHTML=cn;
						var onboxtext=document.getElementById('boxoncurrentnameprofile');
						onboxtext.innerHTML=selectedname;
						document.getElementById('profile_placeurlchange').onclick=function(){
							updateProfile_a(me.value);
						}						
					} else {
						alert("Houve um erro ao tentar salvar o novo perfil.\nPor favor, tente novamente, caso o problema persista, tente novamente daqui a pouco."+res);
						me.disabled=false;						
					}
               } 
          }
     };
     xmlhttp.open("POST", "worksource/home/includes/profile_update.php", true);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xmlhttp.send(params);	
	
}

function createOption(intext, value) {
     var newoption = document.createElement("option");
	 var texto = document.createTextNode(intext); 
	 newoption.setAttribute("value", value); 
	 newoption.appendChild(texto);
	 return newoption; 
}

function getContent(ixbox, ServerPage) {
	setStatus(ixbox);
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function () { 
          if (xmlhttp.readyState==4) {
               if (xmlhttp.status==200) {
                    document.getElementById(ixbox).innerHTML=xmlhttp.responseText;
               } 
          }
     };
     xmlhttp.open("GET", ServerPage);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); 
     xmlhttp.send(null);
}

function startUploadPhoto(){
	if(!validateFileExtensionPic(document.getElementById('arquivo_foto'))){
		document.frmadfile.reset();
		return false;
	}
	document.getElementById('processupload_feedback').style.visibility="visible";
	document.frmadfile.submit();
}

function returnUploadPhoto(rs, msg, n_photo){
	document.getElementById('processupload_feedback').style.visibility="hidden";
	if(rs==false){
		alert(msg);
		document.frmadfile.reset();
		return false;
	}
	var photoimg=document.getElementById('curriculo_minhafoto');
	photoimg.src="worksource/u_photos/"+n_photo;
	global_photo_filename=n_photo;
}

function validateFileExtensionPic(fld) {
	if(!/(\.gif|\.jpg|\.png)$/i.test(fld.value)) {
		alert("O tipo de arquivo selecionado é inválido.");
		//fld.form.reset();
		fld.focus();
		return false;
	}
	return true;
}

function cancelWindowPhoto(){
	windmalopenwindow('windmalbox');
	if(global_photo_filename=="") return false;
	var params = "photo_name="+global_photo_filename;	
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function() { 
          if(xmlhttp.readyState==4) {
               if(xmlhttp.status==200) {
                	var res=xmlhttp.responseText;
                	if(res=="1"){
                		global_photo_filename="";
                		return true;
                	} else {
                		alert("Houve um erro interno inesperado.\nO erro retornado foi:\n\n"+res);
                		return false;
                	}	
               } 
          }
     };
     xmlhttp.open("POST", "worksource/home/includes/photo_manage.php?op=cancel_odp", true);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xmlhttp.send(params);		
}

function saveWindowPhoto(){
	if(global_photo_filename==""){
		windmalopenwindow('windmalbox'); 
		return false;
	}	
	var params = "photo_name="+global_photo_filename;	
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function() { 
          if(xmlhttp.readyState==4) {
               if(xmlhttp.status==200) {
                	var res=xmlhttp.responseText;
                	if(res=="1"){
                		windmalopenwindow('windmalbox'); 
                		var mn_photo=global_photo_filename.replace(".", "_mn.");
                		document.getElementById('curriculo_user_photo').src="worksource/u_photos/"+mn_photo;
                		global_photo_filename="";
                		return true;
                	} else {
                		alert("Houve um erro interno inesperado.\nO erro retornado foi:\n\n"+res);
                		return false;
                	}
               } 
          }
     };
     xmlhttp.open("POST", "worksource/home/includes/photo_manage.php?op=save", true);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xmlhttp.send(params);		
}

function onlyRemovePhoto(){
	if(!confirm("Se clicar em OK, o seu currículo ficará sem foto até que seja inserida outra.\nDeseja confirmar?")) return false;
	var params = "photo_name="+global_photo_filename;	
 	var xmlhttp=Inint_AJAX();
    xmlhttp.onreadystatechange = function() { 
          if(xmlhttp.readyState==4) {
               if(xmlhttp.status==200) {
                	var res=xmlhttp.responseText;
                	if(res=="1"){
                		document.frmadfile.reset();
                		global_photo_filename="";
                		document.getElementById('curriculo_minhafoto').src="worksource/u_photos/default.gif";
                		document.getElementById('curriculo_user_photo').src="worksource/u_photos/default_small.gif";
                		return true;
                	} else {
                		alert("Houve um erro interno inesperado.\nO erro retornado foi:\n\n"+res);
                		return false;
                	}	
               } 
          }
     };
     xmlhttp.open("POST", "worksource/home/includes/photo_manage.php?op=remove", true);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xmlhttp.send(params);		
}
