var response_id='response';
AIM = {
    response: function(rid){
            this.response_id=rid;
    },
    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete(d.body.innerHTML,this.response_id);
        }
    }

}
function startCallback() {
    // make something useful before submit (onStart)
    return true;
}

function fnNewUsr(forma){
    var si=vFrmNewUsr.exec();
    //si=1;
    if(si){
        return AIM.submit(forma, 
            {'onStart' : submit_off('frmNewUsr'), 'onComplete' : 
                function(response,response_id){
                /*inicio*/
                    if(response=='' || !IsNumeric(response)){
                        submit_on('frmNewUsr');
                        mensaje('response',response,'',1);
                    }else{
                        submit_off('frmNewUsr');
                        mensaje('response','','',0);
                    }
                /*fin*/
                }
            }
        );
    }
    return false;
}
function fnAmigo(forma){
    var si=vFrmAmigo.exec();
    //si=1;
    if(si){
        return AIM.submit(forma, 
            {'onStart' : submit_off('frmAmigo'), 'onComplete' : 
                function(response,response_id){
                /*inicio*/
                    if(response=='' || !IsNumeric(response)){
                        submit_on('frmAmigo');
                        mensaje('response',response,'',1);
                    }else{
                        submit_off('frmAmigo');
                        mensaje('response','','',0);
                    }
                /*fin*/
                }
            }
        );
    }
    return false;
}

var a_fieldsNewUsr = {
    'cc' : {
        'l': 'No. Identificacion',  // label
        'r': true,    // required
        'f': 'unsigned',  // format (see below)
        't': 't_cc',// id of the element to highlight if input not validated
        
        'm': null,     // must match specified form field
        'mn': 7,       // minimum length
        'mx': null       // maximum length
    },
    'nombres' : {'l':'Nombres','r':true,'t':'t_nombres'},
    'movil' : {'l':'Telefono Movil','r':true,f:'unsigned','t':'t_movil'},
    'email' : {'l':'E-Mail','r':true,f:'email','t':'t_email'}
},
o_configNewUsr = {
    'to_disable' : ['Submit'],
    'alert' : 1
}

// validator constructor call
var vFrmNewUsr = new validator('frmNewUsr', a_fieldsNewUsr, o_configNewUsr);


var a_fieldsAmigo = {
    'email' : {'l':'Su E-Mail','r':true,f:'email','t':'t_email'},
    'emaila' : {'l':'El E-Mail de su amig','r':true,f:'email','t':'t_emaila'}
},
o_configAmigo = {
    'to_disable' : ['Submit'],
    'alert' : 1
}

// validator constructor call
var vFrmAmigo = new validator('frmAmigo', a_fieldsAmigo, o_configAmigo);

function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
function mensaje(id,texto,refresh,reload){
    obj=document.getElementById(id);
    if(!reload || reload==undefined){
        texto+='<div align="center"><p style="font-size: 10px;">Espere...</p><img src="loading/_loading.gif" /></div>';
    }
    if(texto.indexOf('jsCmd=')!=-1){
        texto=texto.replace(/jscmd=/i,"");
        texto=texto.replace(/amp;/i,"");
        eval(texto);
    }else{
        obj.innerHTML=texto;
    }
    obj.className='mensaje';
    if(!reload || reload==undefined){
        if(refresh.length()>0){
            setTimeout("window.location.href='"+refresh+"'",1875);
        }else{
            setTimeout("window.location.reload();",1875);
        }
    }else{
    
    } 
}
function this_submit(form,on){
    if(form){
        e=form.elements;
        n=e.length;
        for(i=0;i<n;i++){
            if(e[i].type=='submit'){
                   if(on){e[i].disabled='';}else{e[i].disabled='disabled';}
            }
         }
    }
    return true;
}
function submit_on(name){
    form=document.getElementById(name);
    this_submit(form,1);
    return true;
}
function submit_off(name){
    form=document.getElementById(name);
    this_submit(form,0);
    return true;
}

