// JavaScript Document
// ajaxy.js por Edson Lemus =o) edsonnlb@gmail.com
// The secret behind that ajaxy thing! wajajajajaja!

var htmlEditorContainers = new Array();

function setValueById(id, value) {
	var object = document.getElementById(id);
	object.value = value;
}

function setHTMLById(id, html) {
	var object = document.getElementById(id);
	object.innerHTML = html;
}

function addHTMLEditor(elementID) {
	nicEditors.editors.push(new nicEditor({iconsPath : 'http://www.viviendo.com.gt/images/nicEditorIcons.gif'}).panelInstance(elementID));
	htmlEditorContainers[htmlEditorContainers.length] = elementID;
}

function getFormValues(idForm) {
	var objForm = document.getElementById(idForm);
	var str = '';
	for (var i = 0; i < objForm.elements.length; i++) {
		if (i > 0) {
			str = str + "&";
		}
		str = str + objForm.elements[i].name + '=' + escape(objForm.elements[i].value);
	}

	return str;
}

function getAjaxObject() {
	var object = false;
	if (typeof XMLHttpRequest != 'undefined') {
		object = new XMLHttpRequest();
	} 
	else {
		try {
			object = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e1) {
			try {
				object = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2) {}
		}
	}
	return object;
}

function postAjax(serverDoc, strPost) {
	objAjax = getAjaxObject();
	try {
		objAjax.open("POST", serverDoc, true);
		objAjax.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		objAjax.setRequestHeader('Content-Length', strPost.length);
		objAjax.onreadystatechange = function() {
						if (objAjax.readyState == 4) {
							if (objAjax.status == 200) {
								
							}
						}
					}
		objAjax.send(strPost);
	}
	catch (E) {}
}

function execAction(action, ID_CATEGORY, start) {
	var tempHTML = '';
	if (action == 'get_items_first') {
		tempHTML = '<div align="left"><img src="images/loading.gif"></div>'
		setHTMLById('content_'+ID_CATEGORY, tempHTML);
		var strPost = 'ID_CATEGORY='+ID_CATEGORY+'&start='+start;
		postAjax('items.php', strPost, action, ID_CATEGORY);
	}
	else if (action == 'post_item') {
		var objAuthor = document.getElementById('strAuthor');
		var objEmail = document.getElementById('strEmail');
		var objContent = document.getElementById('txtContent');
		objAuthor.value = trim(objAuthor.value);
		objEmail.value = trim(objEmail.value);
		objContent.value = trim(objContent.value);
		if (objAuthor.value == '') {
			alert('Debes colocar tu nombre');
		}
		else {
			/*if (objEmail.value == '') {
				alert('Debes colocar tu e-mail');
			}
			else {*/
				if (objContent.value == '') {
					alert('Escribe tu mensaje');
				}
				else {
					var objButton = document.getElementById('submit');
					objButton.style.display = 'none';
					var objMessage = document.getElementById('sending');
					objMessage.style.display = '';
					var objID_CATEGORY = document.getElementById('ID_CATEGORY');
					var strPost = "ID_CATEGORY="+objID_CATEGORY.value+"&strAuthor="+escape(objAuthor.value)+"&strEmail="+escape(objEmail.value)+"&txtContent="+escape(objContent.value);
					postAjax('submit_post.php', strPost, action, objID_CATEGORY.value);
				}
			//}
		}
	}
	else if (action == 'get_items') {
		tempHTML = '<div align="left"><img src="images/loading.gif"></div>'
		setHTMLById('content_'+ID_CATEGORY, tempHTML);
		var strPost = 'ID_CATEGORY='+ID_CATEGORY+'&start='+start;
		postAjax('items.php', strPost, action, ID_CATEGORY);
	}
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function trim(str) {
	while (str.substring(0, 1) == ' ') {
		str = str.substring(1, str.length);
	}
	while (str.substring(str.length-1, str.length) == ' ') {
		str = str.substring(0, str.length-1);
	}

	return str;
}// JavaScript Document

function fade(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpacity(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpacity(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpacity(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}

function submitForm(formID){
	saveHTMLEditors();
	var objForm = document.getElementById(formID);
	objForm.submit();
}

function saveHTMLEditors(){
	for (var i = 0; i < htmlEditorContainers.length; i++){
		try{
			nicEditors.findEditor(htmlEditorContainers[i]).saveContent();
		}
		catch (E) {}
	}
}