var gdata = null;

function ltrim(s) {
   return s.replace(/^\s+/, "");
}

function rtrim(s) {
   return s.replace(/\s+$/, "");
}

function trim(s) {
   return rtrim(ltrim(s));
}

function MM_goToURL() { //v2.0
  for (var i=0; i< (MM_goToURL.arguments.length - 1); i+=2) //with arg pairs
    eval(MM_goToURL.arguments[i]+".location='"+MM_goToURL.arguments[i+1]+"'");
  document.MM_returnValue = false;
}
/*
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
*/


function removeRow(tabla,r) {
	if (isNaN(r)) r = 0;
	var t = document.getElementById(tabla);
	t.deleteRow(r);
}

/*
function showImage(myimage) {
	var html = "<HTML><HEAD><TITLE>Conexi&oacute;n Hispana</TITLE>" +
	"</HEAD><BODY LEFTMARGIN=0 " +
	"MARGINWIDTH=0 TOPMARGIN=0 MARGINHEIGHT=0 BOTTONMARGIN=0><CENTER>" +
	"<A HREF='javascript:window.close();'><IMG SRC='" + myimage + "' BORDER='0' NAME='image' " +
	"onload='window.resizeTo(document.image.width+15,document.image.height+70)' title='Cerrar'></A></CENTER>" +
	"</BODY></HTML>";
	popup=window.open('Conexi&oacute;n Hispana','image','toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=1');
	popup.document.open();
	popup.document.write(html);
};
*/


function removeOption(id) {
  var elSel = document.getElementById(id);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}


function appendOption(from,to) {
  var elOptNew = document.createElement('option');
  var source = document.getElementById(from); // Una caja de texto
	
	
	if( !isEmail( source.value ) ) {
		alert("direccion de correo no valida");
		return false;
	}
	
	elOptNew.text = source.value.toLowerCase();
  elOptNew.value = 0;
  var theSel = document.getElementById(to);
	
	var k = 0;
	if ( theSel.length > 0 ) {
		for( i=0; i<theSel.length; i++ ) {
			
			// alert( 'ORIGEN: ' + source.value + ' DESTINO: ' + theSel.options[i].text );
			
			if( theSel.options[i].text.toLowerCase() == source.value.toLowerCase() ) {	k++; }

		}
		if( k == 0 ) {
			try {
				theSel.add(elOptNew, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				theSel.add(elOptNew); // IE only
			}
		}
	}
	else {
		try {
			theSel.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			theSel.add(elOptNew); // IE only
		}
	}
	
	source.value = "";
	
}



function addOption(theSel, theText, theValue) {
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
	
	var k = 0;
	if ( theSel.length > 0 ) {
		for( i=0; i<theSel.length; i++ ) {
			if( theSel.options[i].value == theValue ) {	k++; }
		}
		if( k == 0 ) theSel.options[selLength] = newOpt;
	}
	else {
		theSel.options[selLength] = newOpt;
	}
	
}

function isEmail(s) {
	var rx = new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");
	if( !rx.test(s) ) return false;
	return true;
}



function deleteOption(theSel, theIndex) { 
  var selLength = theSel.length;
  if(selLength>0) {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(from, to) {
  
	var theSelFrom = document.getElementById(from);
	var theSelTo = document.getElementById(to);
	
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--) {
    if(theSelFrom.options[i].selected) {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
	
  for(i=selectedCount-1; i>=0; i--) {
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}
  
}

function onKeyEnter(e) {
	key = e.keyCode ? e.keyCode : e.which;
	return key == 13 ? true : false;
}

function check_all(checkall,checkone) {
	if(checkall.checked) {
		if(checkone.length > 0 ) {
			for (i = 0; i < checkone.length; i++) {
				checkone[i].checked = true;
			}
		}
		else {
			checkone.checked = true;
		}
	}
	else {
		if(checkone.length > 0 ) {
			for (i = 0; i < checkone.length; i++) {
				checkone[i].checked = false;
			}
		}
		else {
			checkone.checked = false;
		}
	}
}

function check(checkone,checkall) {
	var k = false;
	if( checkone.length > 0 ) {
		for (i = 0; i < checkone.length; i++) {
			if (checkone[i].checked == false) { k = true; break; }
		}
	}
	else {
		if (checkone.checked == false) { k = true; }
	}
	// alert(k);
	if( k == true ) checkall.checked = false;
	else checkall.checked = true;
}

function toggle(id) {
	if ( document.getElementById(id).style.display == 'none' ) {
		document.getElementById(id).style.display = '';
	}
	else {
		document.getElementById(id).style.display = 'none';
	}
}

function load_region(p) {
	new Ajax.Updater({success: "estado_region"},'load.php',{method: 'post', parameters: 's=pais&o=' + p});
}

function ajax_request(u) {
	new Ajax.Request(u, {
		method : 'post',
		onSuccess: function(transport) { 
			eval(transport.responseText);
		},
		evalScripts : true
	});
}

function hideCountry(i) {
	if ( i > 0 ) {
		load_region(i);
		document.getElementById('ch_pais').style.display='none';
		document.getElementById('estado_region').style.display='';
		document.getElementById('ch_region').style.display='';
	}
}

function showCountry() {
	document.getElementById('ch_pais').style.display='';
	document.getElementById('estado_region').style.display='none';
	document.getElementById('ch_region').style.display='none';
	document.getElementById('region').options[0].selected=true;
}


/*
load_content = function(c,u,f) {
	// alert( 'CAPA: ' + c + ' URL: ' + u + ' FORM: ' + f );
	new Ajax.Updater(c,u,{
		method : 'post',
		parameters : Form.serialize(f),
		onComplete : function(transport){ var w=1;},
		evalScripts : true
	});
};
*/
