/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str){
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox){
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);

}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue){
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function getxmlhttp (){

	//Create a boolean variable to check for a valid Microsoft active x instance.
	var xmlhttp = false;

	//Check if we are using internet explorer.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using internet explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-internet explorer browser.
			xmlhttp = false;
		}
	}
	
	// If not using IE, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}//cierra getxmlhttp


function processAjaxPost(serverPage, str, obj){
	
	xmlhttp = getxmlhttp();

	xmlhttp.open("POST", serverPage, true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = "";
				obj.innerHTML = xmlhttp.responseText;
			}//cierra if
		}// cierra function de onreadystatechagne
	
	xmlhttp.send(str);
	
}//cierra processAjaxPost

function processAjaxGet(serverPage, objID) {
	xmlhttp = getxmlhttp();
	var obj = document.getElementById(objID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = "";
			obj.innerHTML = xmlhttp.responseText;
		}else{
			objID.innerHTML = "";
			objID.innerHTML = "<img src='images/loading.gif' />";
		}
	}
	xmlhttp.send(null);
}//cierra processAjaxGet

//funcion de ajax para post de formas
function processAjaxForm(serverPage, str, obj){
	
	xmlhttp = getxmlhttp();
	
	xmlhttp.open("POST", serverPage, true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                obj.innerHTML = xmlhttp.responseText;
            }else{
                obj.innerHTML = "";
                obj.innerHTML = "<img src='images/loading.gif' />";
            }
        }// cierra function de onreadystatechagne
	xmlhttp.send(str);
}//cierra processAjaxForm


function AddCant(){
	var cant;
	var sum;
	cant = document.getElementById("cant").innerHTML;
	sum = parseInt(cant) + 1;
	document.getElementById('cantidad').value = sum;
	document.getElementById("cant").innerHTML = sum;
}

function SubstractCant(){
var cant;
var sum;
cant = document.getElementById("cant").innerHTML;
if (parseInt(cant) > 1){
    sum = parseInt(cant) - 1;
    document.getElementById('cantidad').value = sum;
    document.getElementById("cant").innerHTML = sum;
}
}

function AgregarACarrito(){
var radio_choice = false;
	if(document.getElementById('color1').value != ''){
	    var items = document.frmCart.radio.length;
	
	    for (counter = 0; counter < items; counter++){
		//alert(document.frmCart.radio[counter].checked);
		if (document.frmCart.radio[counter].checked){
	        	radio_choice = true;
		}
	    }
			
	    if(radio_choice){
		document.frmCart.submit();
	    }else{
	        alert('Debe seleccionar la orientacion');
	    }
			
	}else{
	    alert('Debe seleccionar al menos 1 color');
	}
}

function changeOrientation(or){
	if(or == 1){
	    document.flashfile.GotoFrame(1);
	}else{
	    document.flashfile.GotoFrame(4);
	}
}

function cambiaMun(estado,obj){
		processAjaxGet('library/cambiaMun.php?state=' + estado+'&obj=' + obj, obj);
	
}
