function separaDireccion(dir)//Supone que "dir" tiene valor
{
	var resp=new Array(2);
	resp[0]="";
	resp[1]="";
	var palabras=dir.split(",");
	var fi=false;
	var buscarnum=false;
	var i=1;
	var numcomas="0";
	if(palabras) numcomas=palabras.length-1;
	if(numcomas==0)
	{
		resp[0]=dir.charAt(0);
		while(!fi && i<dir.length)
		{
			if(dir.charAt(i)!=' ')
			{
				resp[0]+=dir.charAt(i);
			}
			else
			{
				if(i+1<dir.length && dir.charAt(i+1)!=' ' && isNumeric(dir.charAt(i+1)))
				{
					fi=true;
					buscarnum=true;
				}
				else if(i+1<dir.length && dir.charAt(i+1)!=' ' && !isNumeric(dir.charAt(i+1)))
				{
					resp[0]+=" ";
				}
			}
			i++;
		}
		if(buscarnum)
		{
			fi=false;
			resp[1]=dir.charAt(i);
			i++;
			while(!fi && i<dir.length)
			{
				if(dir.charAt(i)!=' ' && isNumeric(dir.charAt(i)))
				{
					resp[1]+=dir.charAt(i)
				}
				else fi=true;
				i++;
			}
			
		}
	}
	else
	{
		if(numcomas>1)alert("Formato incorrecto: \n Indique 'Calle, núm.' o bien 'Calle núm.'");
		else
		{
			resp[0]=palabras[0];
			resp[1]=palabras[1];
		}
	}

	if(navigator.userAgent.indexOf("MSIE")<0 && resp && resp[0] && resp[0].length>0)resp[0]=escape(resp[0]);

	return resp;
}

function isNumeric(variable)
{
	var caracValids = "0123456789.-";
	var car;
	var res = true;

	if (variable.length == 0) res = false;
	
	//  test variable consists of valid characters listed above
	for (i = 0; i < variable.length && res == true; i++)
	  {
	  car = variable.charAt(i);
	  if (caracValids.indexOf(car) == -1)
		 {
		 res = false;
		 }
	  }
	return res;
}

function trimRight( str ) {
	var resultStr = "";
	var i = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
		// Loop through string starting at the end as long as there
		// are spaces.
		i = str.length - 1;
		while ((i >= 0) && (str.charAt(i) == " "))
			i--;
			
		// When the loop is done, we're sitting at the last non-space char,
		// so return that char plus all previous chars of the string.
		resultStr = str.substring(0, i + 1);
	}
	
	return resultStr;
}

function trimLeft( str ) {
	var resultStr = "";
	var i = len = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {
		// Loop through string starting at the beginning as long as there
		// are spaces.
		// len = str.length - 1;
		len = str.length;

		while ((i <= len) && (str.charAt(i) == " "))
			i++;

	// When the loop is done, we're sitting at the first non-space char,
		// so return that char plus the remaining chars of the string.
		resultStr = str.substring(i, len);
	}
	return resultStr;
}


function stringWidth(str,classe)
{
	var span=document.createElement('SPAN');
	span.innerHTML=str;
	span.className=classe;
	document.body.appendChild(span);
	var width=getWidth(span);
	document.body.removeChild(span);
	return width;
}


function getWidth(Elem)
{
	var elem=Elem;
	var xPos;
	if(typeof(Elem.tagName)=="undefined"){
		if(document.getElementById){
			elem=document.getElementById(Elem);
		}
		else if(document.all)
		{
			elem=document.all[Elem];
		}
	}
	else
	{
		elem=Elem;
	}
	if(elem.style.width)xPos=elem.style.width;
	else xPos=elem.offsetWidth;

	return xPos;
}
