// JavaScript Document
function goto(page, number){
	document.formulary.iPage.value = number;
	document.formulary.action = page + '.php';
	document.formulary.submit();	
}

/********************************************************
* makes a popup of determinate picture
*
* @param img (image path)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function PopUp(img, toWidth, toHeight){
	foto1= new Image();
	foto1.src=(img);
	Control(img, toWidth, toHeight);
}

/********************************************************
* http and https cleaner from input email inserts
*
* @param act (delete url action)
*
********************************************************/
function cleanHttp(obj){
	var start = obj.value.substr(0,7);
	var starts = obj.value.substr(0,8);
	if (start == "http://"){
		obj.value = obj.value.substr(7);
	} else {
		if (starts == "https://"){
			obj.value = obj.value.substr(8);
		}
	}
}

/********************************************************
* control popup creation of determinate picture
*
* @param img (image path)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function Control(img, toWidth, toHeight){
	if((foto1.width!=0)&&(foto1.height!=0)){  
		verFoto(img, toWidth, toHeight);
	} else {funcion="Control('"+img+"')";  
		intervalo=setTimeout(funcion,20);
	}
}

/********************************************************
* open a popup of determinate picture on new window
*
* @param img (image path)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function verFoto(img, toWidth, toHeight){  
	ancho=foto1.width; 
	alto=foto1.height;
	ratio = ancho/alto;
	if (ratio > 1 ){ 
		reducc = ancho/toWidth;
		ancho = toWidth;
		alto = alto * 1/reducc;
	} else {
		reducc = alto/toHeight;
		alto = toHeight;
		ancho = ancho * 1/reducc;
	}
	height = alto+20;
	width = ancho+20;
	cadena="width="+width+",height="+height+",resizable=no, centered=yes";
	ventana=window.open(img,"",cadena);
}

/********************************************************
* resize an IMG object
*
* @param slider (IMG object)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function preload(slider, toWidth, toHeight){
	image1 = new Image();
	image1.src = slider.src;
	width = image1.width;
	height = image1.height;
	ratio = width/height;
	if (width != 0 && height != 0){
		if (ratio >= 1){
			reducc = width/toWidth;
			width = toWidth;
			height = height * 1/reducc;
		} else {
			reducc = height/toHeight;
			height = toHeight;
			width = width * 1/reducc;
		}
	} else {
		height = toHeight;
		width = toHeight;
	}
	slider.setAttribute("width",width);
	slider.setAttribute("height",height);
}

/********************************************************
* confirm delete
*
* @param act (delete url action)
*
********************************************************/
function confdel(act){
	if(confirm("Are you sure to delete this item?")){
		document.frm.action = act;
		document.frm.submit();
		return true;
	} else {
		return false;
	}
}

/********************************************************
* letters are not permitted
*
* 
*
********************************************************/
function NoLetters(evt){
	var nav4 = window.Event ? true : false;
	var val = '0123456789,';
	
	var key = nav4 ? evt.which : evt.keyCode;
	for (i=0; i<val.length; i++) {
		if (val.indexOf(String.fromCharCode(key),0) != -1) return true;
	}
	if (key <= 13) return true;
	return false;
}

/********************************************************
* format a number while this are being inserted
*
* @param act (delete url action)
*
********************************************************/		
function FormPrice(obj){
	var val = obj.value;
	var i = val.length - 1;
	var j = 0;
	var cam = 1;
	while (i>=0) {
		if (val.charAt(i) == ','){
			postString = val.substring(i + 1, val.length);
			preString = val.substring(0, i);
			val = preString + postString;
		}
		i--;
	}
	i = val.length - 1;
	if (val.length > 2){
		while (i>=0) {
			j++;
			if (j == 4){
				postString = val.substring(val.length - 3 * cam - cam + 1, val.length);
				preString = val.substring(0, val.length - 3 * cam - cam + 1);
				val = preString + ',' + postString;
				cam++;
				i++;
			}
			if ((j == 4)){
				j = 0;
			}
			i--;
		}
		obj.value = val;
	}
}


/********************************************************
* send resort list values
*
* @param field (send field to sort list)
*
********************************************************/
function reSort(field){
	if (document.frm.orderDir.value == "DESC"){
		document.frm.orderDir.value = "ASC";
	} else {
		document.frm.orderDir.value = "DESC";
	}
	document.frm.orderField.value = field;
	document.frm.submit();
}

/********************************************************
* select all checks
*
*
********************************************************/
function selectAll(chkbox){
	for (var i=0;i < document.forms["frm"].elements.length;i++){ 
		var elemento = document.forms["frm"].elements[i]; 
		if (elemento.type == "checkbox"){ 
			elemento.checked = chkbox.checked 
		} 
	} 
}

<!-------------------------------- Some ajax code -------------------------------------------------->

// Define a list of Microsoft XML HTTP ProgIDs.
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
	"Msxml2.XMLHTTP.7.0",
	"Msxml2.XMLHTTP.6.0",
	"Msxml2.XMLHTTP.5.0",
	"Msxml2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP"
);

// Define ready state constants.
var XMLHTTPREQUEST_READY_STATE_UNINITIALIZED = 0;
var XMLHTTPREQUEST_READY_STATE_LOADING       = 1;
var XMLHTTPREQUEST_READY_STATE_LOADED        = 2;
var XMLHTTPREQUEST_READY_STATE_INTERACTIVE   = 3;
var XMLHTTPREQUEST_READY_STATE_COMPLETED     = 4;

//-----------------------------------------------------------------------------
// Returns an XMLHttpRequest object.
//-----------------------------------------------------------------------------
function getXMLHttpRequest()
{
	var httpRequest = null;

	// Create the appropriate HttpRequest object for the browser.
	if (window.XMLHttpRequest != null)
		httpRequest = new window.XMLHttpRequest();
	else if (window.ActiveXObject != null)
	{
		// Must be IE, find the right ActiveXObject.
		var success = false;
		for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++)
		{
			try
			{
				httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
				success = true;
			}
			catch (ex)
			{}
		}
	}

	// Display an error if we couldn't create one.
	if (httpRequest == null)
		alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");

	// Return it.
	return httpRequest;
}

//-----------------------------------------------------------------------------
// clean selection field initially
//-----------------------------------------------------------------------------

function cleanField() {
	var option;
	
	for (i=0; document.forms[0].elements["opt_from"].options.length = i ; i++)
    	document.forms[0].elements["opt_from"].remove(0);

	option = document.createElement("OPTION");
	option.text = "Select option...";
	option.value = 0;

	try{
		document.forms[0].elements["opt_from"].add(option, null);
	} catch(ex) {
		// For IE.
		document.forms[0].elements["opt_from"].add(option);
	}
}


var option = getXMLHttpRequest();

function optionLookup(event){

  // Clear the zip code drop-down list.
  while (document.forms[0].elements["opt_from"].options.length > 1)
    document.forms[0].elements["opt_from"].remove(1);

  // Abort any currently active request.
  option.abort();
 
 var opt_level = document.forms[0].elements["levels"].options[document.forms[0].elements["levels"].selectedIndex].value;
  // Perform an asynchronous request to get a list of zip codes for
  // that city and state.
  if(opt_level!=""){
	opt_level = opt_level-1;
  	var url = "getOption.php?level=" + encodeURI(opt_level);
  	option.onreadystatechange = optionCodeReadyStateChange;
  	option.open("GET", url, true);
  	option.send(null);
  }
}

//-----------------------------------------------------------------------------
// Returns an XMLHttpRequest response
//-----------------------------------------------------------------------------

function optionCodeReadyStateChange(){
	var statusText;
	// Check the ready state.
	switch (option.readyState){
		case XMLHTTPREQUEST_READY_STATE_COMPLETED:
			// Get the XML document returned from the request and fill in the
			// form fields.
			try{
				var xmlDoc = option.responseXML;
				
				// Copy the city and state attributes from the root XML node to the appropriate form fields.
				// Get all the zip code tags returned from the request.
				var  optionnamelist = xmlDoc.getElementsByTagName("title");
				var optionidlist = xmlDoc.getElementsByTagName("option_id");
				
				// Add an option to to the drop-down list for each zip code returned from the request.
				for (var i = 0; i < optionnamelist.length; i++){
					option1 = document.createElement("OPTION");
					option1.text = optionnamelist[i].firstChild.nodeValue;
					option1.value = optionidlist[i].firstChild.nodeValue;
					try{
						document.forms[0].elements["opt_from"].add(option1, null);
					}
					catch(ex)
					{
						// For IE.
						document.forms[0].elements["opt_from"].add(option1);
					}
				}

			}
			catch (ex)
			{}
			break;

		default:
			statusText = "Unknown error.";
			break;
	}

}
