var whitespace = " \t\n\r";
var decimalPointDelimiter = ","

function getNChars(n,ch){
	var i,newStr;

	newStr = "";
	for (i = 0; i < n; i++){
		newStr = newStr + ch;
	}
	
	return newStr;
}

function isWhitespace(s){
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
			return false;
	}
	return true;
}

function doFocus(obj)
{
  obj.focus();
  return true;
}

function String2Array(valueString,separator,length){
	var arrayString = new Array(length);
	
	arrayString=valueString.split(separator);

	return arrayString;	
}
function isEmail(email){
	var atSignIndex,dotSign, white;
	if (email != ""){
		// Check for @ sign 
		//alert(email.indexOf("@"));
		atSignIndex = email.indexOf("@");
		if (atSignIndex == -1){
//			alert("Arroba"+ atSignIndex);
			return false;}
	
		// Check that @ sign is no at begin or end
		if (atSignIndex == 0 || atSignIndex == email.length -1){ 
//		alert("Arroba fin inicio "+ atSignIndex);
		return false;}
		
		// Check for . sign 
		dotSign = email.indexOf(".");
		if (dotSign == -1){
//		alert("no hay punto "+ dotSign);
			return false;}
	
		// Check that . sign is no at begin or end
		if (dotSign == 0 || dotSign == email.length -1){
//	alert("Punto inicio fin "+ dotSign);
			return false;}

		// Check that there are no spaces inside
		//if (email.indexOf(" ") != -1){ se modifico ya que sonda envia blancos despues del mail

if((email.indexOf(" ")< dotSign+3) && (email.indexOf(" ")> -1)){
//alert("aaaaa"+ email+ "aaaaa");
//alert("espacio" + " "+email.indexOf(" "));
return false;}


//*****************************************
//check white spaces at the end

//white=email.indexOf(" ");
//if (white == email.length -3 and white == email.length -2 and white == email.length -1){return true;}


//*****************************************
	}
	
	// Passed all checks, OBS! is also email if empty
	return true;
}

function LTrim(str){	
	while(str.length > 0 && str.charAt(0) == " "){
		str = str.substr(1,str.length)
	}

	return str
}

function RTrim(str){
	while(str.length > 0 && str.charAt(str.length-1) == " "){
		str = str.substr(0,str.length-1);
	}
	
	return str
}

function Trim(theStr){
	theStr = RTrim(theStr);
	theStr = LTrim(theStr);
	
	return theStr 
}

function isEmpty(s){
	return ((s == null) || (s.length == 0));
}

function isDigit (c){
	return ((c >= "0") && (c <= "9"));
}

function isInteger (s){
	var i;

	if (isEmpty(s)) {return false;}
	
	for (i = 0; i < s.length; i++){   
		if (!isDigit(s.charAt(i))){return false;}
	}

	return true;
}

function isDecimal (s){
	var i,nums;

	if (isEmpty(s)) {return false;}
	nums = s.split(",");

	if (nums.length == 1){
		return isInteger(nums[0]);
	}else if(nums.length == 2){
		return isInteger(nums[0]) && isInteger(nums[1]);
	}
	
	return false;
}

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return false;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = false;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        if ( (s.charAt(0) == "-"))
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isPositiveInteger(s)
{   var secondArg = false;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];
    
    return (isSignedInteger(s, secondArg) && ((isEmpty(s) && secondArg) || (parseInt(s,10) > 0)));
}

function isNonnegativeInteger (s)
{   var secondArg = false;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isFloat (s)
{   var i;
    var seenDecimalPoint = false;
    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return false;
       else return (isFloat.arguments[1] == true);
    if (s == decimalPointDelimiter) return false;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;
}

function isSignedFloat (s)
{   if (isEmpty(s)) 
       if (isSignedFloat.arguments.length == 1) return false;
       else return (isSignedFloat.arguments[1] == true);
    else {
        var startPos = 0;
        var secondArg = false;
        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

function isFieldEmpty(theForm,fieldVal,fieldName,alertName){
	if (isEmpty(fieldVal)){
		alert("Debe ingresar "+alertName);
		theForm.elements[fieldName].focus();
		return true;
	}
	
	return false;
}

function checkEmail(emailStr){
	var result = false
	alert(emailStr);
	if (!isEmpty(emailStr)){
		result = isEmail(emailStr);
	}
	
	return result;
}

function isLeapYear(year){

	if (year % 4 == 0){
		if (year % 100 == 0 && year % 400 != 0){
			return false;
		}
		return true;
	}
	
	return false;
}


function getLastDayInMonth(month,year){
	var febDays = 28;
	
	if (isLeapYear(year)) {febDays = 29;}
	if (month < 1 || month > 12) {return 0;}
	if (month == 2){return febDays;}
	if ((month == 4 || month == 6 || month == 9 || month == 11)){return 30;}
	
	return 31;
}


function checkDate(day,month,year){
	var febDays = 28;
	
	if (month < 1 || month > 12) {return false;}
	if (day < 1 || day > 31){return false;}
	if (isLeapYear(year)) {febDays = 29;}
	if (month == 2 && day > febDays){return false;}
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){return false;}
	
	return true;
}


function selectAllOptions(theForm, selectName) {
	var i;
	var selectLength = theForm.elements[selectName].length;

	for(i=0; i<selectLength; i++){
		theForm.elements[selectName].options[i].selected = true;
  }

  return false;
}

function checkLogin(theForm, rutField, digit, rutFullField, passField){

  var rutFull = theForm.elements[rutFullField].value; 
  var pass = theForm.elements[passField].value;  
 
	if (rutFull==''){
		alert('Ingrese su Rut');
		return false;
	}
  
  if (!checkRut(theForm,rutFullField,'Rut de usuario invalido',true)){
		return false;
	}

	if (pass==''){
		alert('Ingrese clave de usuario');	
		return false;
	}

  if (!isInteger(pass)){
		alert('La clave debe ser numerica');	
		return false;
	}
  
	if (pass.length != 4){
		alert('La clave debe ser de 4 digitos');	
		return false;
	}


	theForm.elements[rutField].value  = rutFull.substr(0,rutFull.indexOf("."));
	rutFull = rutFull.substr(rutFull.indexOf(".")+1,rutFull.length);
	theForm.elements[rutField].value += rutFull.substr(0,rutFull.indexOf("."));
	rutFull = rutFull.substr(rutFull.indexOf(".")+1,rutFull.length);	
	theForm.elements[rutField].value += rutFull.substr(0,rutFull.indexOf("-"));
  theForm.elements[digit].value = rutFull.substr(rutFull.indexOf("-")+1,rutFull.length);
	return true;
}


function checkLogin2(theForm, rutField, digit, rutFullField){

  var rutFull = theForm.elements[rutFullField].value; 
 // var pass = theForm.elements[passField].value;  
 
	if (rutFull==''){
		alert('Ingrese su Rut');
		return false;
	}
  
  if (!checkRut(theForm,rutFullField,'Rut de usuario invalido',true)){
		return false;
	}

	/*if (pass==''){
		alert('Ingrese clave de usuario');	
		return false;
	}

  if (!isInteger(pass)){
		alert('La clave debe ser numerica');	
		return false;
	}
  
	if (pass.length != 4){
		alert('La clave debe ser de 4 digitos');	
		return false;
	}

*/
	theForm.elements[rutField].value  = rutFull.substr(0,rutFull.indexOf("."));
	rutFull = rutFull.substr(rutFull.indexOf(".")+1,rutFull.length);
	theForm.elements[rutField].value += rutFull.substr(0,rutFull.indexOf("."));
	rutFull = rutFull.substr(rutFull.indexOf(".")+1,rutFull.length);	
	theForm.elements[rutField].value += rutFull.substr(0,rutFull.indexOf("-"));
  theForm.elements[digit].value = rutFull.substr(rutFull.indexOf("-")+1,rutFull.length);
	return true;
}


function checkFormRUT(theForm, rutField, digit, rutFullField){
	var rutDigits, checkDigit

  var rutFull = theForm.elements[rutFullField].value; 
	rutDigits = rutFull.substr(0,rutFull.length-1);
	rutDigits = removeChar(rutDigits,'.');
	checkDigit = rutFull.substr(rutFull.length-1,1).toUpperCase();

	if (rutFull==''){
		alert('Ingrese un Rut');
		return false;
	}

  if (!checkRut(theForm,rutFullField,'Rut de cliente invalido',true)){
		return false;
	}  
  
	theForm.elements[rutField].value = rutDigits;
  theForm.elements[digit].value = checkDigit;
	return true;
}


var monthNames = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
var usMonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

function createDate(year,month,day,hours,minutes){
	return new Date(usMonthNames[month-1]+" "+day+", "+year+" "+hours+":"+minutes+":00");
}


function bustOutFrames() {
	if (parent.frames.length != 0){
		top.location=document.location;
	}
}

function today() {
  var Today;
  
  Today = new Date();
  return (Today.getDate()+" de "+monthNames[Today.getMonth()]+" de "+Today.getFullYear());
}

function doSubmitForm(theForm){
	theForm.submit();
  return false;
}

function doSubmit(theForm, doAction, page, action, target){
	var tmpAction, tmpTarget;
 	tmpAction = theForm.action;
	tmpTarget = theForm.target;  
	if(typeof(theForm.doAction) != "undefined"){
		theForm.doAction.value = doAction;
	}

	if(typeof(theForm.page) != "undefined"){
		theForm.page.value = page;
	}  
  
  if(action != ""){
		theForm.action = action;
  }
  
  if(target != ""){
		theForm.target = target;
  }
  
//alert("action before submit==["+theForm.action+"]");
 theForm.submit();
//alert("action after submit==["+theForm.action+"]");
  return false;
}

function setHidden(theVariable, theValue){
	if(typeof(theVariable) != "undefined"){
		theVariable.value = theValue;
	}
  return false;
}

function isNumeric(strValue){
	var i;
  
	for (i = 0; i < strValue.length; i++)
    {   
        var c = strValue.charAt(i);
        if ( !isDigit(c)) 
          	return false;
    }
   return true;
}
