/*gestine dell'overlay box per le finestre di servizio*/
/*fnOpenServicesBox crea e apre il box di servizio*/
/*
*@stype: string tipo di box di servizio
*@return: void
*
*/

function fnOpenServicesBox(sService,sMessage,iIdItem,sMail){
    //salvo il nodo padre, dove verrā appeso il box
    var oParent = document.getElementById("service_box");
    //creo il div da appendere
    var oChild = document.createElement("div");
    oChild.setAttribute("id","interactive_box");
     //struttura interna, creo l'header, il footer e il body del box
    var oDivHeader = document.createElement("div");
    oDivHeader.setAttribute("id","box_head");
    var oDivBody = document.createElement("div");
    oDivBody.setAttribute("id","box_body");
    if(sService=='login'){
	oDivBody.style.height="134px";
        oDivBody.style.textAlign="center";
    }
    var oDivFooter = document.createElement("div");
    oDivFooter.setAttribute("id","box_footer");
    
    
    oChild.appendChild(oDivHeader);
    oChild.appendChild(oDivBody);
    oChild.appendChild(oDivFooter);
    //riempio le sezioni header e footer del box
    oDivHeader.innerHTML = fnBuildHTMLToHeaderServiceBox(sService);
    oDivFooter.innerHTML = fnBuildHTMLToFooterServiceBox(sService);
    oDivBody.innerHTML = fnBuildHTMLToBodyServiceBox(sService,sMessage,iIdItem,sMail);
   
    
    //adesso ho creato l'oggetto, devo differenziare in base al servizio
    if(sService=='login'){
	oChild.style.width="330px";
	oChild.style.height="213px";
	oChild.style.marginLeft="-165px";
    }
    if(sService=='contatti'){
	oChild.style.width="330px";
	oChild.style.height="253px";
	oChild.style.marginLeft="-165px";
    }
    if(sService=='commenta'){
	oChild.style.width="330px";
	oChild.style.height="245px";
	oChild.style.marginLeft="-165px";
    }
    oParent.appendChild(oChild);
}


function fnBuildHTMLToHeaderServiceBox(sService){
    if(sService == 'messaggio'){
	sHTMLToHead = "<div id=\"close\" style=\"text-align:right;margin-right:10px;\"><strong onclick=\"document.getElementById('service_box').innerHTML='';\">[X] Close</strong><div class=\"sep_overlay\"></div></div>";
    }
    else
	sHTMLToHead = "<div id=\"close\"><strong onClick=\"fnCloseBox();\">[X] Close</strong><div class=\"sep_overlay\"></div></div>";
    return sHTMLToHead;
}


function fnBuildHTMLToBodyServiceBox(sService,sMessage,iIdItem,sMail){
    //eccezione per i box di messaggi di errori o successo
    if(typeof sMessage != 'undefined')
	sHTMLToBody = fnCreateContentFormByService(sService,sMessage,iIdItem,sMail);
    else{
	sHTMLToBody = "<div id=\"form\">";
	sHTMLToBody += fnCreateContentFormByService(sService,sMessage,iIdItem);
	sHTMLToBody += "</div>";
    }
    return sHTMLToBody;
}


function fnBuildHTMLToFooterServiceBox(sService){
	 sHTMLToFooter = "";
	 if(sService == 'login'){
			sHTMLToFooter +="<div class=\"sep_overlay\" style=\"margin-left:11px;\"></div>";
			sHTMLToFooter += "<div id=\"link_overlay\"><a href=\"/template/registrati.php\"><span class=\"text_tools\">Registrati</span></a>";
			//sHTMLToFooter += "&nbsp;&nbsp;<a href=\"#\" ><span class=\"text_tools\">Password smarrita?</span></a>";
			sHTMLToFooter += "</div>";
	 }
	 	return sHTMLToFooter;
}

/*distingue, in base al */
function fnCreateContentFormByService(sService,sMessage,iIdItem,strMail){
    switch(sService){
	case "login":{
	    sHTML = "<form action = \"../lib/checkLogin.php\" method = \"POST\" id=\"form_login\">";
	    sHTML += "<table style=\"top:60px;position:absolute;left:47px;font-size:11px;\"><tr><td colspan=\"2\"><div id=\"error\"></div></td></tr><tr><td>";
	    sHTML += "<strong>Username</strong>: </td><td><input name=\"username\" type=\"text\" id=\"user\" value=\"Inserisci il tuo username\" onclick=\"fnChangeLoginInput('form_login');\"></td>";
	    sHTML += "</tr><tr><td>";
	    sHTML += "<strong>Password</strong>: </td><td><input name=\"password\" type=\"text\" id=\"passw\" value=\"password\" onclick=\"fnChangeLoginInput('form_login');\"></td></tr>";
	    sHTML +="</tr><tr><td></td><td style=\"padding-left:90px;\">"
	    sHTML += "<div style=\"margin-top:10px\" id=\"pulsante_login\" class=\"pulsanti_form\" type=\"submit\" name=\"submit\" value=\"Invia Dati\" onCLick=\"fnCheckData(document.getElementById('user').value,document.getElementById('passw').value, null,'"+sService+"');return false;\"></div>";
	    sHTML += "</td></tr></table><div class=\"clear\"></div></form><div class=\"clear\"></div>";
	    break;
	}
	case "contatti":{
	    var sMail = (typeof strMail == 'undefined') ? "Email" : strMail;
	    sDisabled = (typeof strMail == 'undefined') ? "" : "disabled='disabled'";
	    sHTML = "<form action = \"../lib/inviaMail.php\" method = \"POST\" id=\"form_contatti\">";
	    sHTML += "<table style=\"font-size:11px;position:absolute;left:38px;top:33px;\" id=\"tab_contatti\"><tr><td></td><td><div id=\"error\"></div></td></tr><tr><td>";
	    sHTML += "<strong>Mail</strong>: </td><td><input name=\"usermail\" "+sDisabled+" type=\"text\" id=\"mail\" style=\"width:178px;color:#DDD;\" value='"+sMail+"' onclick=\"fnChangeContattiInput('form_contatti',event);\"></td></tr>";
	    sHTML += "<tr><td>";
	    sHTML += "<strong>Oggetto</strong>: </td><td><select name=\"subject\" id=\"oggetto\" style=\"width:185px;\" onchange=\"fnChangeContattiInput('form_contatti',event);\">";
	    sHTML += "<option value=\"1\">Scegli l'oggetto";
	    sHTML += "<option value=\"2\">Informazioni sulla fotografia";
	    sHTML += "<option value=\"3\">Informazioni su grafica/design";
	    sHTML += "<option value=\"4\">Informazioni su pittura/disegno";
	    sHTML += "<option value=\"5\">Informazioni di collaborazione";
	    sHTML += "<option value=\"6\">Rivista";
	    sHTML += "<option value=\"7\">Comunicazioni e stampa";
	    sHTML += "<option value=\"8\">Problemi nell'upload di file";
	    sHTML += "<option value=\"9\">Richieste generiche";
	    sHTML += "<option value=\"10\">Segnalazioni di errori";
	    sHTML += "</select></td></tr>";
	    sHTML += "<tr><td>";
	    sHTML += "<strong>Testo</strong>: </td><td><textarea name=\"commento\" rows=\"4\" cols=\"20\" id=\"testo\" style=\"color:#DDD;\" onclick=\"fnChangeContattiInput('form_contatti',event);\">Scrivi il testo della richiesta</textarea></td></tr>";
	    sHTML += "<tr><td></td><td style=\"padding-left:126px;\">";
	    sHTML += "<div id=\"pulsante_invia\" class=\"pulsanti_form\" onCLick=\"fnCheckData(document.getElementById('mail'), document.getElementById('oggetto'), document.getElementById('testo'), '"+sService+"');return false;\"></div>";
	    sHTML += "</td></tr></table></form>";
	    break;
	 }  
	 case "commenta":{
	    sHTML = "<form action = \"../lib/memorizza_commento.php\" method = \"POST\">";
	    sHTML += "<input type=\"hidden\" name=\"item\" value="+'"'+iIdItem+'"'+"\">";
	    sHTML += "<textarea name=\"commento\" rows=\"10\" cols=\"10\" id=\"testo\" style=\"color:#DDD;\">Scrivi il teso della richiesta</textarea>";
	    sHTML += "<input type=\"submit\" name=\"submit\" value=\"Invia Dati\" onCLick=\"fnChangeContattiInput('form_contatti',event);fnCheckData(document.getElementById('testo').value,null,null,'"+sService+"');return false;\">Invia</input>";
	    break;
	 }
	 case "messaggio":{
	    sHTML = sMessage;
	    break;
	 }
	 case "error":{
	    sHTML = sMessage;
	    break;
	}
    }
    return sHTML;
}


/*checkdata controlla la coerenza dei valori dei campi*/
function fnCheckData(sData1, sData2, sData3, sService){
    switch(sService){
	case "contatti":{
	        if(sData1.value == "Email"){
		    document.getElementById('error').innerHTML="Mail non inserita";
		    return false;
		    break;
		}
		else{
		     if(!fnCheckValidMail(sData1.value)){
			document.getElementById('error').innerHTML="Indirizzo mail non valido";
			return false;
			break;
			}
		}
		if(sData2.value == 1){
		    document.getElementById('error').innerHTML="Scegliere un oggetto";
		    return false;
		    break;
		}
		if(sData3.value == ""){
		    document.getElementById('error').innerHTML="Testo non inserito";
		    return false;
		    break;
		}
	    }
	   
	case "commenta": {
	    if(fnCheckContactText(sData1))
	    break;
        }
	case "login": {
	    if(sData1 == "" || sData2 == "" || sData2=="password"){
		if(sData1 == "" && sData2 == "")
		    document.getElementById('error').innerHTML="Compilare i campi";
		else{
		    if(sData1 == "" || sData1=="Inserisci il tuo username")
			document.getElementById('error').innerHTML="Username non inserito o Password non inserita";
		    if(sData2 == "" || sData2=="password")
		        document.getElementById('error').innerHTML="Password non inserita o Username non valido";
		    }
		return false;
		}
	    break;
        }
	default: {
	    alert("errore");
	}
    }
    fnRequestToServer(sService);
}


function fnStartLoading2(oElement){
    document.getElementById('error').innerHTML="";
	
    oElement.childNodes[0].style.display="none";
    var oDivElement = document.createElement("div");
    oDivElement.setAttribute("id","wait");
    oDivElement.innerHTML = "<div id = \"accesso_msg\" style=\"display:block;text-align:center;\"><div class=\"wait_message\" style=\"margin-top:10px;\">Accesso in corso</div><img src='../images/LoadingSmall.gif'></div>";
    oElement.appendChild(oDivElement);
}



function  fnStopLoader(sService,sMessage,bIsOK){
    document.getElementById('box_body').removeChild(document.getElementById('wait'));
    //to do: fare lo switch
    if(typeof sService != 'undefined' && sService == 'contatti')
	document.getElementById('box_body').innerHTML = "<div>Mail inviata con successo!</div><div onClick=\"fnCloseBox();\">Chiudi</div>";
    if(typeof sService != 'undefined' && sService == 'commenta')
	document.getElementById('box_body').innerHTML = "<div>Il tuo commento č stato inoltrato, a breve sarā visibile su E-LAB Blog</div><div onClick=\"fnCloseBox();\">Chiudi</div>";
    if(sService == "login" ){
	if(typeof bIsOK != 'undefined' && bIsOK == false){
	    document.getElementById('error').innerHTML = sMessage.replace('"','');
	    document.getElementById('box_body').childNodes[0].style.display = "block";
	}
	else{
	    document.getElementById('box_body').innerHTML = sMessage.replace('"','');
	    document.getElementById('box_head').innerHTML="";
	    document.getElementById('box_footer').innerHTML="";
	    var oDivReload = document.createElement("div");
	    oDivReload.setAttribute("id","reload");
	    oDivReload.innerHTML="<div onclick=\"window.location.replace(unescape(window.location));\">[ close ]</div>"
	    document.getElementById('box_head').appendChild(oDivReload);
	}
    }
}




/*
 *created from admin 28/05/2010 ore 23:19
 *
 */

//antispam e anti-simpatici :)
function fnCheckContactText(sString){
    if(typeof sString != 'undefined' || sString != "")
	return true;
    else
	return false;
}


//serve a sostituire eventuali apici o caratteri strani
function fnCheckFromServer(sString){
    return sString;
}

