/*httpRequestObjects*/

var xmlHttpNotes = notesRequestObject();

function notesRequestObject(){
    var xmlHttpNotes;
	
	try{
		xmlHttpNotes = new XMLHttpRequest();
	}
	catch(e){
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for(var i = 0; i < XmlHttpVersions.length && !xmlHttpNotes; i++){
			try{
				xmlHttpNotes = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	if(!xmlHttpNotes){
		alert("Error al crear el objeto XMLHttpRequest");
	}
	else{
		return xmlHttpNotes;
	}
}

var xmlHttpNote = notesRequestObject();

function notesRequestObject(){
    var xmlHttpNote;
	
	try{
		xmlHttpNote = new XMLHttpRequest();
	}
	catch(e){
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for(var i = 0; i < XmlHttpVersions.length && !xmlHttpNote; i++){
			try{
				xmlHttpNote = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	if(!xmlHttpNote){
		alert("Error al crear el objeto XMLHttpRequest");
	}
	else{
		return xmlHttpNote;
	}
}

var xmlHttpMail = notesRequestObject();

function notesRequestObject(){
    var xmlHttpMail;
	
	try{
		xmlHttpMail = new XMLHttpRequest();
	}
	catch(e){
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for(var i = 0; i < XmlHttpVersions.length && !xmlHttpMail; i++){
			try{
				xmlHttpMail = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	if(!xmlHttpMail){
		alert("Error al crear el objeto XMLHttpRequest");
	}
	else{
		return xmlHttpMail;
	}
}

/*functions*/

function showNotes(){
    if(xmlHttpNotes){
		try{
			xmlHttpNotes.open("POST","sideS/notes.php",true);
			xmlHttpNotes.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttpNotes.onreadystatechange = notesResponse;
			xmlHttpNotes.send(null);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function notesResponse(){
    if(xmlHttpNotes.readyState == 1){
        try{
            notesCont = document.getElementById('body_notes');
            notesCont.innerHTML = "<p class='note_war'>Cargando...</p>";
        }
        catch(e){
            notesCont.innerHTML = "Error al leer la respuesta";
        }
    }
    else if(xmlHttpNotes.readyState == 4){
        if(xmlHttpNotes.status == 200){
            try{
               notesResult(); 
            }
            catch(e){
               notesCont.innerHTML = "Hubo un error al recibir la respuesta"; 
            }
        }
    }
}

function notesResult(){
    var xmlResponse = xmlHttpNotes.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttpNotes.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
    xmlNotesResponse = xmlResponse.documentElement;
    idNotes = xmlNotesResponse.getElementsByTagName('id');
    textNotes = xmlNotesResponse.getElementsByTagName('title');
    
    if(idNotes.item(0).firstChild.data == "none"){
        notesCont.innerHTML = "<p class='note_war'>No hay notas que mostrar...</p>";
    }
    else{
        listNotes = "<ul id='list_notes'>";
        for(i = 0; i < idNotes.length; i++){
            listNotes += "<li class='notes'>" + textNotes.item(i).firstChild.data;
            listNotes += "<p class='more_note' onclick='showNote(" + idNotes.item(i).firstChild.data + ")'>Leer más...</p></li>";
        }
        listNotes += "</ul>";
        notesCont.innerHTML = listNotes;
    }
}

function showNote(id){
    parameter = "id=" + id;
    
    if(xmlHttpNote){
		try{
			xmlHttpNote.open("POST","sideS/note.php",true);
			xmlHttpNote.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttpNote.onreadystatechange = noteResponse;
			xmlHttpNote.send(parameter);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function noteResponse(){
   if(xmlHttpNote.readyState == 1){
        try{
            noteCont = document.getElementById('head_note');
            noteCont.innerHTML = "<p class='note_war'>Cargando...</p>";
        }
        catch(e){
            noteCont.innerHTML = "Error al leer la respuesta";
        }
    }
    else if(xmlHttpNote.readyState == 4){
        if(xmlHttpNote.status == 200){
            try{
               noteResult(); 
            }
            catch(e){
               noteCont.innerHTML = "Hubo un error al recibir la respuesta**"; 
            }
        }
    } 
}

function noteResult(){
    var xmlResponseNote = xmlHttpNote.responseXML;
	if(!xmlResponseNote && !xmlResponseNote.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttpNote.responseText);
	}
	var rootNodeName = xmlResponseNote.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
    
    xmlNoteResponse = xmlResponseNote.documentElement;
    idNotice = xmlNoteResponse.getElementsByTagName('id');
    titleNote = xmlNoteResponse.getElementsByTagName('title');
    textNote = xmlNoteResponse.getElementsByTagName('text');
    imageNote = xmlNoteResponse.getElementsByTagName('image');
    
    titleCont = document.getElementById('head_note');
    textCont = document.getElementById('body_note');
    
    if(idNotice.item(0).firstChild.data == "none"){
        noteCont.innerHTML = "<p class='note_war'>No hay nota que mostrar...</p>";
    }
    else{
        titleCont.innerHTML = "<h2>" + titleNote.item(0).firstChild.data + "</h2>";
        textCont.innerHTML = "<img class='image_note' src='admin/notes_imgs/" + imageNote.item(0).firstChild.data + "'/><p class='text_contents'>" + textNote.item(0).firstChild.data + "</p>";
    }
}

/*contacto*/

function sendContacto(){
    formContacto = document.form_cto;
    nameContacto = formContacto.name_cto.value;
    telContacto = formContacto.tel_cto.value;
    mailContacto = formContacto.mail_cto.value;
    comentsContacto = formContacto.coments_cto.value;
    
    paramCto = "name=" + nameContacto + "&tel=" + telContacto + "&mail=" + mailContacto + "&coments=" + comentsContacto;
    
    if(xmlHttpMail){
		try{
			xmlHttpMail.open("POST","sideS/sendmail.php",true);
			xmlHttpMail.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttpMail.onreadystatechange = mailRequest;
			xmlHttpMail.send(paramCto);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	} 
}

function mailRequest(){
    if(xmlHttpMail.readyState == 1){
        try{
            mailCont = document.getElementById("status_msg");
            mailCont.innerHTML = "Enviando..."; 
            
        }
        catch(e){
            mailCont.innerHTML = "Error al leer la respuesta";
        }
    }
    else if(xmlHttpMail.readyState == 4){
        if(xmlHttpMail.status == 200){
            try{
                mailResult(); 
            }
            catch(e){
               mailCont.innerHTML = "Hubo un error al recibir la respuesta"; 
            }
        }
    }
}

function mailResult(){
    var xmlResponse = xmlHttpMail.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttpMail.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
    
    logInResponse = xmlResponse.documentElement;
    resLogIn = logInResponse.getElementsByTagName('result');
    userText = resLogIn.item(0).firstChild.data;
    
    if(userText == "Error"){
       mailCont.innerHTML = "Hubo un error durante el envío...<br/>Intenta de Nuevo";
    }
    else{
        mailCont.innerHTML = "Su mensaje ha sido enviado...";
        setTimeout('clearCtoForm()','2000');
    }
}

function clearCtoForm(){
    formCto = document.form_cto;
    formCto.reset();
    msgCto = document.getElementById('status_msg');
    msgCto.innerHTML = "&nbsp;";
}

/*extras*/

function showFrase(){
    var ranNumber = Math.floor(Math.random() * 17);
    
    if(ranNumber == 0){
        var selFrase = "images/frase_1.png";
    }
    else{
        var selFrase = "images/frase_" + ranNumber + ".png";
    }
    $("img#banner_frase").fadeOut(1000,function(){
        $(this).attr("src",selFrase);
        $(this).fadeIn(1000);
    });
}

/*Validation*/

function valContacto(){
    formContacto = document.form_cto;
    nameContacto = formContacto.name_cto.value;
    telContacto = formContacto.tel_cto.value;
    mailContacto = formContacto.mail_cto.value;
    comentsContacto = formContacto.coments_cto.value;
    
    stateMsg = document.getElementById('status_msg');
    
    if(nameContacto == ""){
        stateMsg.innerHTML = "Ingrese su nombre por favor..."
        return false;
    }
    else if(mailContacto == ""){
        stateMsg.innerHTML = "Ingrese su dirección de correo por favor...";
        return false;
    }
    else if(mailContacto.indexOf('@') == -1 || mailContacto.indexOf('.')== -1){
        stateMsg.innerHTML = "Ingrese un correo válido...";
        return false;
    }
    else if(comentsContacto == ""){
        stateMsg.innerHTML = "No ha ingresado ningún comentario...";
        return false;
    }
    else{
        stateMsg.innerHTML = "&nbsp;";
        return true;
    }
}

/*Behavior*/

$(document).ready(function(){
    var screenWidth = $(window).width();
    
    if(screenWidth > 1024){
        var screenAdd = screenWidth - 434;
        $("div#foot").css("width",screenAdd);
    }
    
    $("div#menu_slider").hide();
    $("div#site_contents").load("contents/home.htm");
    showFrase();
    
    $("li#home_btn").click(function(){
        $("div#menu_slider").hide(1000);
        $("div#site_contents").load("contents/home.htm");
    });
    $("li#mail_btn").click(function(){
        $("div#menu_slider").hide(1000);
        $("div#site_contents").load("contents/contacto.htm");
    });
    $("li#somos_btn").click(function(){
        $("div#menu_slider").hide(1000);
        $("div#site_contents").load("contents/somos.htm");
    });
    $("li#clientes_btn").click(function(){
        $("div#menu_slider").hide(1000);
        $("div#site_contents").load("contents/clientes.htm");
    });
    $("li#noticias_btn").click(function(){
        $("div#menu_slider").hide(1000);
        $("div#site_contents").load("contents/noticias.htm");
    });
    $("li#docs_btn").click(function(){
        $("div#menu_slider").hide(1000);
        $("div#site_contents").load("contents/docs.htm");
    });
    
    var evalBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'318px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/360_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'318px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/360_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var organBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'358px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/eco_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'362px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/eco_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var softBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'397px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/software_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'404px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/software_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var reclBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'437px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/rs_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'452px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/rs_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var coachBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'475px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/coach_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'492px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/coach_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var capacBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'515px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/capac_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'536px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/capac_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var consBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'553px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/consu_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'580px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/consu_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var respBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'593px',height:'40px'},1000,function(){
            $("div#site_contents").load("contents/resp_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'624px',height:'40px'},1000,function(){
            $("div#site_contents").load("contents/resp_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var igenterBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'652px',height:'40px'},1000,function(){
            $("div#site_contents").load("contents/igenter.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'687px',height:'40px'},1000,function(){
            $("div#site_contents").load("contents/igenter.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var nominaBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'705px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/nomina_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'745px',height:'23px'},1000,function(){
            $("div#site_contents").load("contents/nomina_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    var psicoBtn = function(){
        document.documentElement.scrollTop = 0;
        var browserIe = navigator.appName;
        $("div#menu_slider").show(1000);
        $("div#menu_slider").stop(true,true);
        if(browserIe == "Microsoft Internet Explorer"){
            $("div#menu_slider").animate({top:'746px',height:'40px'},1000,function(){
            $("div#site_contents").load("contents/psicosocial_han.htm",function(){
                showFrase();
                });
            });
        }
        else{
            $("div#menu_slider").animate({top:'792px',height:'40px'},1000,function(){
            $("div#site_contents").load("contents/psicosocial_han.htm",function(){
                showFrase();
                });
            });
        }
        
    }
    
    $("li#eval_btn,li#evalft_btn").click(evalBtn);
    $("li#organ_btn,li#climaft_btn").click(organBtn);
    $("li#soft_btn,li#softft_btn").click(softBtn);
    $("li#recl_btn,li#recluft_btn").click(reclBtn);
    $("li#coach_btn,li#coachft_btn").click(coachBtn);
    $("li#capac_btn,li#capacft_btn").click(capacBtn);
    $("li#cons_btn,li#consft_btn").click(consBtn);
    $("li#resp_btn,li#respft_btn").click(respBtn);
    $("li#igenter_btn,li#igenterft_btn").click(igenterBtn);
    $("li#nomina_btn,li#nominaft_btn").click(nominaBtn);
    
});
