String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
function divAlert(msg)
{
    alert(msg);
    // var element = document.getElementById('StatusDiv');
    // if (element)
    //    element.innerHTML ='<font color="FF0000">'+msg+'</font>';

}
function onLoadFunc(){
    var waitTime = 2000;
    var passwordMinLength = 6;
    var accountMinLength = 3;
    var captchaCodeLength = 6;
    
    $("account").focus();
    var account = new LiveValidation( "account", {validMessage: "Ok!", onlyOnSubmit: true } );
    account.add( Validate.Presence, { failureMessage: "Required!" } );
    account.add( Validate.Length, { minimum: accountMinLength } );
    account.add( Validate.Format, { pattern: /^[^\s]([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+((\s){1,1}([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+)*[^\s\\]$/,
                                    failureMessage:  'Invalid characters.'} );
    /* Normal Vivox allowable characters.
    account.add( Validate.Format, { pattern: /^[^\s]([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+((\s){1,1}([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+)*[^\s\\]$/,
                                    failureMessage:  'Invalid characters.'} );
    */
                                        

    var buddy = new LiveValidation( "buddy", {validMessage: "Ok!", onlyOnSubmit: true } );
    buddy.add( Validate.Format, { pattern: /^[^\s]([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+((\s){1,1}([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+)*[^\s\\]$/,
                                    failureMessage:  'Invalid characters.'} );
    /* Normal Vivox allowable characters.
    buddy.add( Validate.Format, { pattern: /^[^\s]([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+((\s){1,1}([A-Za-z0-9\&=+$,\-\.!~\(\)\'@_])+)*[^\s\\]$/,
                                    failureMessage:  'Invalid characters.'} );
    */
                                        
    var verify_num = new LiveValidation('verify_num',  {validMessage: "Ok!", onlyOnSubmit: true } );
    verify_num.add( Validate.Presence, { failureMessage: "Required!" } );
    verify_num.add( Validate.Numericality, { onlyInteger: true } );
    verify_num.add( Validate.Length, { is: captchaCodeLength } );

    var email = new LiveValidation('email', {validMessage: "Ok!", onlyOnSubmit: true });
    email.add( Validate.Email );    
    email.add( Validate.Presence, { failureMessage: "Required!" } );

    var vvxpassword = new LiveValidation('vvxpassword', { validMessage: "Ok!", onlyOnSubmit: true });
    vvxpassword.add( Validate.Presence, { failureMessage: "Required!" } );
    vvxpassword.add( Validate.Length, { minimum: passwordMinLength } );

    var vvxpassword_verify = new LiveValidation('vvxpassword_verify', { validMessage: "Ok!", onlyOnSubmit: true });
    vvxpassword_verify.add( Validate.Confirmation, { match: 'vvxpassword' } );
    vvxpassword_verify.add( Validate.Length, { minimum: passwordMinLength } );
    
    var certify = new LiveValidation('certify');
    certify.add( Validate.Acceptance );
    

}
function failureFunc(){
    alert("Failed to register. :-( " );
}
function successFunc(){
    // alert("Succesfully sent registration!");
}

function completedFunc(){
	var xml = new XMLDoc(this.transport.responseText, null);
	var status = xml.selectNodeText("/level0/status");
	if (status == "ERR"){
		var code = xml.selectNodeText("/level0/body/code");
		var msg = xml.selectNodeText("/level0/body/msg");
		status = " "+ msg + " ("+code+")";
		if ( code == 1500 ){
		    verifyNum = document.getElementById('verify_num');
		    verifyNum.select();
		}
		if ( code == 1203 || code == 1204 || code == 1205 ){
		    account = document.getElementById('account');
		    account.select();
		}
		alert(status);
		
	} else {
	/*  var message  = "Welcome to Voon! \n\n";
	        message += "Your account has been created. \n\n";
	        message += "You will soon receive an email which will contain an account confirmation link ";
	        message += "for your new Voon account. \n\n";
	        message += "You must click on this confirmation link in order to use Voon.\n\n\n";
	        message += "Regards,\n\n";
	        message += "The Voon Network Team";
	    alert(message);
    */	        
	    document.location='/register-finis';
	    
	}
}

function VoonSignup()
{
    var accountNode = document.getElementById('account');
    var account = new String(accountNode.value);
    if ( account.length < 3 ){
		divAlert('An "Account Name" must be specified and must be at least 3 characters in length');
        return false;
    }
    var url='/voon/voon_provision.php?account='+encodeURIComponent(account.valueOf());
    
    // pass along the ext_id and ext_profile
    var extIdNode = document.getElementById('ext_id');
    var extProfileNode = document.getElementById('ext_profile');
    var extId = new String(extIdNode.value);
    var extProfile = new String(extProfileNode.value);
    url = url + '&ext_id='+encodeURIComponent(extId.valueOf())+'&ext_profile='+encodeURIComponent(extProfile.valueOf());
    
    var emailNode = document.getElementById('email');
	var email = new String(emailNode.value);
    if ( email.length <= 0 ){
		divAlert('You must provide us with an email address to signup for a Voon Account.');
        return false;
    }
	var passwordNode = document.getElementById('vvxpassword');
    var passwordVerifyNode = document.getElementById('vvxpassword_verify');
    var password = new String(passwordNode.value);
    var passwordVerify = new String(passwordVerifyNode.value);
    if ( password.length < 5 ){
		divAlert('A "password" must be specified and must be at least 5 characters in length');
        return false;
    }
    if ( password.valueOf() != passwordVerify.valueOf() ){
		divAlert('Passwords do not match!');
		return false;
    }
    url = url+"&password="+encodeURIComponent(password.valueOf());
    if ( email.length < 1 ){
		divAlert('An eMail address must be supplied');
		return false;
    }
    url += '&email='+encodeURIComponent(email.valueOf());
    
    var certify = document.getElementById('certify').checked;
    var age = 0;
    if ( certify ){
        var age=13;
    }
    
    url+= '&age='+age;
    var verify_id = document.getElementById('verify_id').value;
    var verify_num = new String(document.getElementById('verify_num').value);
    
    if ( verify_num.length <= 0 ){
		divAlert('You must enter the Captcha Code!');
        return false;
    }
    url += '&verify_id='+verify_id;
    url += '&verify_num='+verify_num.valueOf();
    var buddy = new String(document.getElementById('buddy').value);
    if ( buddy.length > 0 ){
        url += '&buddy='+buddy;
    }
    
    
    
/*    Debug the call.... 
*/
    // url += '&start_debug=1&debug_host=localhost'
    // alert('calling url '+url);

    var ajaxRequest = new Ajax(url, 
                                {method: 'get', 
                                 onComplete: completedFunc,
                                 onFailure: failureFunc, 
                                 onSuccess: successFunc,
                                 update: status
                                  }
                               ).request();
	return false;
}




function cancelFailureFunc(){
    alert("Failed to cancel. :-( " );
}
function cancelSuccessFunc(){
    // alert("Succesfully sent registration!");
}

function cancelCompletedFunc(){
	var xml = new XMLDoc(this.transport.responseText, null);
	var status = xml.selectNodeText("/level0/status");
	if (status == "ERR"){
		var code = xml.selectNodeText("/level0/body/code");
		var msg = xml.selectNodeText("/level0/body/msg");
		status = " "+ msg + " ("+code+")";
		alert(status);
	} else {
	/*  var message  = "Welcome to Voon! \n\n";
	        message += "Your account has been created. \n\n";
	        message += "You will soon receive an email which will contain an account confirmation link ";
	        message += "for your new Voon account. \n\n";
	        message += "You must click on this confirmation link in order to use Voon.\n\n\n";
	        message += "Regards,\n\n";
	        message += "The Voon Network Team";
	    alert(message);
    */	        
	    document.location='/cancel-finis';
	}
}

function VoonCancelSignup()
{
    var accountNode = document.getElementById('account');
    var account = new String(accountNode.value);
    if ( account.length < 3 ){
		divAlert('An "Account Name" must be specified and must be at least 3 characters in length');
        return false;
    }
    var accountidNode = document.getElementById('accountid');
    var accountid = new String(accountidNode.value);
    var url='/voon/voon_cancel_provisioning.php?account='+encodeURIComponent(account.valueOf())+'&accountid='+accountid;
    
    
/*    Debug the call.... 
*/
    // url += '&start_debug=1&debug_host=localhost'
    // alert('calling url '+url);

    var ajaxRequest = new Ajax(url, 
                                {method: 'get', 
                                 onComplete: cancelCompletedFunc,
                                 onFailure: cancelFailureFunc, 
                                 onSuccess: cancelSuccessFunc,
                                 update: status
                                  }
                               ).request();
	return false;
}


