// constants to define the title of the alert and button text.
var ALERT_TITLE = "Oops!";
var ALERT_BUTTON_TEXT = "ACEPTAR";
var path_img_alert = "images/iconos/";
var path_imgs_alert = new Object();
path_imgs_alert["info"] = "info.jpg";
path_imgs_alert["alerta"] = "alerta.jpg";
path_imgs_alert["pregunta"] = "pregunta.gif";
var path_imgs_title_alert = "world2.png";


function init_alert(){
	// over-ride the alert method only if this a newer browser.
	// Older browser will see standard alerts
	if(document.getElementById) {
		//alert("ayumi...");
		window.alert = function(txt,tipo_msg,titulo) {
			createCustomAlert(txt,tipo_msg,titulo);
		}
	}
}

function createCustomAlert(txt,tipo_msg,titulo) {
	hide_elements_forms("SELECT",true);
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;
		// create the modalContainer div as a child of the BODY element
		mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
		mObj.id = "modalContainer";
		 // make sure its as tall as it needs to be to overlay all the content on the page
		mObj.style.height = document.documentElement.scrollHeight + "px";
	
		// create the DIV that will be the alert 
		alertObj = mObj.appendChild(d.createElement("div"));
		alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
		// center the alert box
		alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
	
		// create an H1 element as the title bar
		div_top_alert = d.createElement("div");
		div_top_alert.id = "div_top_alert";		
		tabla_head = d.createElement("TABLE");
		tabla_head.id = "tabla_head_alert";
		tabla_head.border = "0";
		tabla_head.align = "center";
		tabla_head.width = "73%";
		tr_head = tabla_head.insertRow(-1);
		td_head_1 = tr_head.insertCell(-1);
		td_head_2 = tr_head.insertCell(-1);
		td_head_1.width = "10%";
		td_head_1.align = "center";
		src_img = "<img src='"+path_img_alert+path_imgs_title_alert+"'>";
		td_head_1.innerHTML = src_img;
		td_head_2.width = "90%";
		td_head_2.align = "left";
		if(!titulo){
			td_head_2.innerHTML = ALERT_TITLE;	
		}
		else{
			td_head_2.innerHTML = titulo;	
		}
		div_top_alert.appendChild(tabla_head);
		//h1 = div_top_alert.appendChild(d.createElement("h1"));
		//h1.appendChild(d.createTextNode(ALERT_TITLE));
		alertObj.appendChild(div_top_alert);
	
		//cuerpo
		// create a paragraph element to contain the txt argument
		div_body_alert = d.createElement("div");
		div_body_alert.id = "div_body_alert";
		div_body_alert.style.height = "100%";
		div_body_alert.style.border = "none";
		tabla = div_body_alert.appendChild(d.createElement("TABLE"));
		tabla.id = "tabla_body_alert";
		tabla.border = "0";
		tabla.cellSpacing = "0px";
		tabla.cellPadding = "0px";
		tabla.style.marginBottom = "0px";
		tabla.align = "center";
		tabla.width = "70%";
		tabla.height = "100%";
		//tabla.style.marginTop = "10px";
		tr = tabla.insertRow(-1);
		td_1 = tr.insertCell(-1); 
		td_2 = tr.insertCell(-1);
		td_1.width = "25%";
		switch(tipo_msg){
			case "I":{ //INFORMACION
				tipo_msg = "info";
				break;
			}
			case "A":{
				tipo_msg = "alerta";	
				break;
			}
			case "P":{
				tipo_msg = "pregunta";	
				break;
			}
		}
		src_img = "<img src='"+path_img_alert+path_imgs_alert[tipo_msg]+"'>";
		alert(src_img);
		td_1.innerHTML = src_img;
		td_2.width = "75%";
		td_2.align = "left";
		td_2.innerHTML = txt;
		
		tr_2 = tabla.insertRow(-1);
		td_2_1 = tr_2.insertCell(-1);
		td_2_1.colSpan = 2;
		td_2_1.innerHTML = "&nbsp;";
		
		tr_3 = tabla.insertRow(-1);
		td_3_1 = tr_3.insertCell(-1);
		td_3_1.colSpan = 2;
		
		//msg.appendChild(d.createTextNode(txt));								
		
		// create an anchor element to use as the confirmation button.
		
		btn = td_3_1.appendChild(d.createElement("INPUT"));
		btn.id = "closeBtn";
		//btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
		btn.value = ALERT_BUTTON_TEXT;
		//btn.href = "#";
		// set up the onclick event to remove the alert when the anchor is clicked
		btn.onclick = function() { removeCustomAlert();return false; }
		
		alertObj.appendChild(div_body_alert);

		//pie de pagina
		div_bottom_alert = d.createElement("div");
		div_bottom_alert.id = "div_bottom_alert";
		alertObj.appendChild(div_bottom_alert);
	
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
	hide_elements_forms("SELECT",false);
}
