/***************************************************************************************************************************
									libliveauth.js
									v1 beta
									by: randall j. houser
****************************************************************************************************************************


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
to use, first:

	myAuthObj = new liveauthObj();
	myAuthObj.add_liveauth ( element_id, 'string name of test', 0, 0);


setErrMsg(testName, errMsg)

customTest(testName, pregTestString, errMsg)

add_liveauth(obj_id, testName, minLength, maxLength, opt_err_redir_id)
stored:	[obj]
		[testName]
		[minLength]
		[maxLength]
		[opt_err_redir_id]
		[custom authentication function]
		
testElement(obj)
		pass the element itself.  is done onblur by default and set by this script when add_liveauth is run on it
		
testAll()
		run this to test all configured elements in document
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

*/

function liveauthObj(){
	
	var dasauthObj=this;
	var udef_Elements = new Array();
	var liveauthTests = new Array();
	var liveauthMsgs = new Array();
	
	
	
	this.add_liveauth = function (obj_id, testName,  minLength, maxLength, opt_err_redir_id, udef_authfunc, udef_authfunc_error){
		
		var index=udef_Elements.length;
		
		udef_Elements[index] = new Array();
		udef_Elements[index][0]=document.getElementById(obj_id);
		udef_Elements[index][1]=testName;
		udef_Elements[index][2]=(minLength?minLength:0);
		udef_Elements[index][3]=(maxLength?maxLength:0);
		udef_Elements[index][4]=(opt_err_redir_id ? document.getElementById(opt_err_redir_id):null);
		udef_Elements[index][5]=(udef_authfunc?udef_authfunc:null);
		udef_Elements[index][6]=(udef_authfunc_error?udef_authfunc_error:null);
		
		if (document.addEventListener){
			udef_Elements[index][0].addEventListener( 'blur', function (event) { dasauthObj.testElement(this) } , false);
		} else {
			udef_Elements[index][0].onblur=function (event) { dasauthObj.testElement(this) };
		}
		
		var libliveauthErrorSpan=document.createElement('span');
		libliveauthErrorSpan.className='libliveauth_error';
		libliveauthErrorSpan.id=obj_id+'_libliveauth_error';
		if(udef_Elements[index][4]){
			udef_Elements[index][4].appendChild(libliveauthErrorSpan);
		} else {
			udef_Elements[index][0].parentNode.insertBefore(libliveauthErrorSpan, udef_Elements[index][0].nextSibling);
		}
	}
	
	
	
	//define custom test
	this.customTest = function (testName, pregTestString, errMsg){
		
		var index=liveauthTests.length;
		
		liveauthTests[index]=new Array();
		liveauthTests[index][0]=testName;
		liveauthTests[index][1]=pregTestString;
		liveauthTests[index][2]=errMsg;
	}
	
	
	//populate standard tests
	this.customTest('alphanumeric', /^[0-9a-zA-Z]*$/, 'Letters and Numbers Only');
	this.customTest('alpha', /^[a-zA-Z]*$/, 'Letters Only');
	this.customTest('numeric', /^[0-9]*$/, 'Numbers Only');
	this.customTest('sqlDate', /^[0-9]{4,4}\-[0-9]{2,2}\-[0-9]{2,2}$/, 'YYYY-MM-DD Only');
	this.customTest('phone', /^[0-9a-zA-Z\(\)\s\.\-]*$/, 'Phone Numbers Only');
	this.customTest('zipcode', /^[0-9\-]*$/, 'ZIP Code Only');
	this.customTest('float', /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/, 'Numbers and one dot (.) Only');
	this.customTest('usMoney', /^((\d+(\.[\d]{0,2})?)|((\d*\.)?[\d]{1,2}))$/, 'Whold Dollars and Cents Only');
	this.customTest('email', /^[0-9a-zA-Z\._\-]+@[0-9a-zA-Z\._\-]+\.[0-9a-zA-Z]{2,}$/, 'Bad Email Address');
	this.customTest('url', /^[hH][tT][tT][pP]+\:\/\/[0-9a-zA-Z\._\-]+\.[0-9a-zA-Z]{2,}[0-9a-zA-Z\._\-\/]*$/, 'Invalid or Incomplete URL');
	this.customTest('text', /^[0-9a-zA-Z\s\.\?\!\,\r\n\'\"\+\-]*$/, 'Bad Character');
	
	
	
	
	//set custom error message
	this.setErrMsg = function (testName, errMsg){
		
		for( var i = 0; i < liveauthTests.length; i++){
			if ( liveauthTests[i][0]==testName ) liveauthTests[i][2]=errMsg;
		}
	}
	
	
	// element test
	this.testElement = function(obj){
		
		var dasElement, dasTest, dasErrSpan, dasError='', dasErrMsg, invalidChar, failState=true;
		
		//locate the element in the list, so we can test it.  also make sure we have an error span...
		for(var i=0; i<udef_Elements.length; i++){
			if (udef_Elements[i][0]==obj){
				dasElement=i;
				if(!(document.getElementById(udef_Elements[dasElement][0].id+'_libliveauth_error'))){
					//make span, if not exist
					var libliveauthErrorSpan=document.createElement('span');
					libliveauthErrorSpan.className='libliveauth_error';
					libliveauthErrorSpan.id=udef_Elements[dasElement][0].id+'_libliveauth_error';
					if(udef_Elements[i][4]){
						udef_Elements[i][4].appendChild(libliveauthErrorSpan);
					} else {
						udef_Elements[i][0].parentNode.insertBefore(libliveauthErrorSpan, udef_Elements[i][0].nextSibling);
					}

				}
				dasErrSpan=document.getElementById(udef_Elements[dasElement][0].id+'_libliveauth_error');
			}
		}
		
		//locate test string and error message
		for( var i = 0; i < liveauthTests.length; i++){
			if ( liveauthTests[i][0]==udef_Elements[dasElement][1] ){
				dasTest=liveauthTests[i][1];
				dasErrMsg=liveauthTests[i][2];
			}
		}
		
		//does it pass
		if(!(udef_Elements[dasElement][5])){
			if(!((dasTest).test(obj.value))){
				if(udef_Elements[dasElement][2] > 0 ){
					failState=false;
					for(var i=0; i < (obj.value).length; i++){
						if(!((dasTest).test((obj.value).charAt(i))) ){
							invalidChar=(obj.value).charAt(i);
							break;
						}
					}
					dasError+=' '+dasErrMsg+' : '+invalidChar+' is Invalid!';
				}
			}
		} else {
			if( udef_Elements[dasElement][2] > 0 ){
				if(! udef_Elements[dasElement][5](obj.value)){
					failState=false;
					dasError=( udef_Elements[dasElement][6] ? udef_Elements[dasElement][6]  : ' Fail!' );
				}
			}
		}
			
		
		//is it too short???
		if( udef_Elements[dasElement][2] && (((obj.value).replace(/^\s+|\s+$/g, '')).length < udef_Elements[dasElement][2]) ){
			dasError+=' Too Short!';
			failState=false;
		}
		
		//is it too long???
		if( udef_Elements[dasElement][3] && (((obj.value).replace(/^\s+|\s+$/g, '')).length > udef_Elements[dasElement][3]) ){
			dasError+=' Too Long!';
			failState=false;
		}
		
		//set the error message...
		dasErrSpan.innerHTML=dasError;
		
		//return the failstate (true=pass, false=fail)
		return failState;
	}
	
	
	// test all elements
	this.testAll = function(){
		
		var dasElement, dasTest, dasErrSpan, dasError='', dasErrMsg, invalidChar, failState=true, obj;
		
		//locate the element in the list, so we can test it.  also make sure we have an error span...
		for(var dasElement=0; dasElement<udef_Elements.length; dasElement++){
			dasError='';
			obj=udef_Elements[dasElement][0];
			if(!(document.getElementById(udef_Elements[dasElement][0].id+'_libliveauth_error'))){
				//make span, if not exist
				var libliveauthErrorSpan=document.createElement('span');
				libliveauthErrorSpan.className='libliveauth_error';
				libliveauthErrorSpan.id=udef_Elements[dasElement][0].id+'_libliveauth_error';
				if(udef_Elements[dasElement][4]){
					udef_Elements[dasElement][4].appendChild(libliveauthErrorSpan);
				} else {
					udef_Elements[dasElement][0].parentNode.insertBefore(libliveauthErrorSpan, udef_Elements[dasElement][0].nextSibling);
				}

			}
			dasErrSpan=document.getElementById(udef_Elements[dasElement][0].id+'_libliveauth_error');
			
			//locate test string and error message
			for( var x = 0; x < liveauthTests.length; x++){
				if ( liveauthTests[x][0]==udef_Elements[dasElement][1] ){
					dasTest=liveauthTests[x][1];
					dasErrMsg=liveauthTests[x][2];
				}
			}
			
			//does it pass
			if(!(udef_Elements[dasElement][5])){
				if(!((dasTest).test(obj.value))){
					if(udef_Elements[dasElement][2] > 0 ){
						failState=false;
						for(var i=0; i < (obj.value).length; i++){
							if(!((dasTest).test((obj.value).charAt(i))) ){
								invalidChar=(obj.value).charAt(i);
								break;
							}
						}
						dasError+=' '+dasErrMsg+invalidChar+'is Invalid! ';
					}
				}
			} else {
				if(udef_Elements[dasElement][2] > 0 ){
					if(! udef_Elements[dasElement][5](obj.value)){
						failState=false;
						dasError=( udef_Elements[dasElement][6] ? udef_Elements[dasElement][6] : ' Fail!' );
					}
				}
			}
			
			
			//is it too short???
			if( udef_Elements[dasElement][2] && (((obj.value).replace(/^\s+|\s+$/g, '')).length < udef_Elements[dasElement][2]) ){
				dasError+=' Too Short!';
				failState=false;
			}
			
			//is it too long???
			if( udef_Elements[dasElement][3] && (((obj.value).replace(/^\s+|\s+$/g, '')).length > udef_Elements[dasElement][3]) ){
				dasError+=' Too Long!';
				failState=false;
			}
		
			//set the error message...
			dasErrSpan.innerHTML=dasError;
		}
		//rewrite of testElement for speed, if any fail put errMsg and set failState=false
		
		return failState;
	}
	
	this.clearAllErrors = function(){
		for(var x=0; x<udef_Elements.length; x++)
			document.getElementById(udef_Elements[x][0].id+'_libliveauth_error').innerHTML='';
	}
}