/* Common.js
 * 
 * Form validation routines.
 * version: 2.2
 * 
 * author: Gabor Klok
 * 
 * fixed password allownull return, was missing jquery enclosure  
 * added "nopolicy" attribute check to skip password complexity checks.
 * added the check for an empty object to isNull
 */

var msgError = "";
var hasj = {};
var formDebug = false;

function isNull( objObj ){
	if( typeof(objObj) == 'object') {
		for(var i in objObj){ return false;}
		return true;
	}
	
	if( typeof( objObj ) == 'undefined' ){ return true; }
	if( objObj === null ){ return true; }
	if( objObj == "" ){ return true; }
	if( objObj == 0 ){ return true; }
	return false;
}

function ifNull( objObj, strReplace ){
	if( isNull( objObj ) === true ){ return strReplace; }
	return objObj;
}

function getCSSobj( objCss, objElement ){
	var myCSS = {};
	for( i in objCss ){
		myCSS[ i ] = $(objElement).css( i );
	}
	return myCSS;
}

function setBorder( objElement, intFlag){
	var css = {
		'background-color': 'rgb(255, 150, 150)',
		'color': 'maroon',
		'border-width': '1px',
		'border-style': 'solid',
		'border-color': 'red'
	};

	var strElement = objElement.id;
	if( isNull( strElement ) === true ){
		msgError += "DEVELOPER ALERT: your element [" + ifNull(objElement.name, '??') + " type:" + objElement.type + "] has not got an ID\n"; 
	} 

	if( /checkbox/.test( objElement.type ) === true ){
		objElement = $(objElement).parent().get(0);
	}

	if( intFlag === true ){
		if( isNull( hasj[ strElement ] ) === false ){
			$(objElement).css( hasj[ strElement ] );
			delete hasj[ strElement ];
		}
	} else {
		if( isNull( hasj[ strElement ] ) === true ){
			hasj[ strElement ] = getCSSobj( css, objElement );
		}
		$(objElement).css( css );
	}
}

function chPassword(objPass){
	var passes = $(objPass).find(":password");
	var cpass = 0;
	var ret = true;

	passes.each( function(i){
		if( isNull(this.value) === true ){ 
			ret = isNull($(this).attr("allownull")); 
			return; 
		}

		if( cpass == 0 ){
			cpass = this.value;
		} else {
			if( cpass !== this.value ){
				msgError = "Passwords do not match!\n";
				ret =  false; return;
			}
		}
		
		if( isNull($(this).attr("nopolicy")) === true ){
			if ( /^([a-z]+|[A-Z]+|\d+|\s+)$/.test( this.value ) || this.value.length < 5 ){
				msgError = "Password too simple..";
				ret = false; return;
			}
		}
	} );

	return ret;
}

function chValue( objObj ){
	var val = "";
	if( /checkbox/.test( objObj.type ) ){
		val = ( objObj.checked === true ? 1 : null );
	} else {
		val = objObj.value;
	}

	if( isNull(val) === true && isNull($(objObj).attr("allownull")) === true ){
		setBorder( objObj, false );
		if( formDebug === true ){ msgError += objObj.name + ' = empty\n'; }
		if( isNull( $(objObj).attr("errortext") ) === false ){
			msgError += $(objObj).attr("errortext") + "\n";
		}
		return false;
	} else {
		setBorder( objObj, true );
		return true;
	}
}

function chForm( objForm, boolDebug ){
	if( boolDebug === true ){ formDebug = true; }
	var ch = true;
	var strForm;
	
	if( isNull(objForm) === false ){ strForm = '#' + objForm.id; }

	var passFields = ifNull( $(objForm).find(":password").length , 0);

	$(objForm).find(":input").each( function(i){
		if ( /password|hidden|submit|button/.test( this.type ) === false ){
			if( chValue(this) === false ){ ch = false; }
		}

		if( passFields == 1 && /password/.test( this.type ) === true ){
			if( chValue(this) === false ){ ch = false; }
		}
	});

	if( passFields > 1 ){
		var t = chPassword( objForm );
		if( t === false ){ ch = false; }
		$(objForm).find(":password").each( function(i){
			setBorder(this, t);
		});
	}
	if( msgError && !ch ){ alert( msgError ); msgError = "";}

	return ch;
}

function instMO(){
	$(".grid tr").mouseover(
		function(){
			if( $(this).attr("nohi") ){ return; }
			$(this).toggleClass("lite");
		}
	).mouseout(
		function(){
			if( $(this).attr("nohi") ){ return; }
			$(this).toggleClass("lite");
		}
	);
}

function getmethis(uri){
	window.location = uri;
}