///////////////////////////////////////////////////////////////////////////////////
// Desc:     Functions always existing but content can change
// Author:   Linus Lövholm
// History:
// 14 Sep 2005 - Created - Linus Lövholm
///////////////////////////////////////////////////////////////////////////////////


/***********************************************
 * SESS CHECK COMPLETE
 *
 * Done when system has checked if session
 * should be renewed
 ***********************************************/
function _sessSETCheckComplete(secondsLeft,userId){
	if(window.sessCheckTimer){
		clearTimeout(window.sessCheckTimer);
		window.sessCheckTimer = null;
	}
	_performSETSessCheck(secondsLeft,userId);	
}

function _performSETSessCheck(seconds,userId){

	if(isNaN(seconds))
		return;
	
	seconds = parseInt(seconds,10);

	if(seconds<=0){
		_ajaxFWGet("FW_SESSCHECK",userId,null,"GET_EXEC");
	}else{
		window.sessCheckTimer=setTimeout("_performSETSessCheck(0,'"+userId+"');",(seconds-1)*1000); //One second less to account for various delays in JS
	}

	// Note: if a more complex system for session logout checks is desired (with warning dialogs etc), expand
	// this standard solution based on the solution used for OC

}

function _sessSETRenew(userId){
	if(window.sessCheckTimer){
		clearTimeout(window.sessCheckTimer);
		window.sessCheckTimer = null;
	}
	_ajaxFWGet("FW_SESSRENEW",userId,null,"GET_EXEC");
}



/***********************************************
 * SHOW VALIDATION FAILURES
 *
 * Displays a warning of failed validation.
 * Not FW since we may want to do other things
 * than alert(), change content of a div for ex.
 ***********************************************/
var setValidationFailures = "";
var setValidationFailedFields = ",";
function _showSETValidationFailures() {
	if (setValidationFailures!= "") {
		if(_showCMSInfoDialog){
			_showCMSInfoDialog("set_validationFailureDialog","",setValidationFailures,
				[{title:"Ok",action:"_hideCMSInfoDialog(false,\"set_validationFailureDialog\");"}],true);
			document.body.style.cursor = 'auto';
		}else{
			alert(setValidationFailures.replace("<br>","\n"));
		}
		
	}
}


/***********************************************
 * ADD VALIDATION FAILURE
 *
 * Adds failure message, marks invalid and 
 * adds to failed fields string.
 * Not FW since we may want to do other things
 * here as well!
 ***********************************************/
function _addSETValidationFailure(obj,str) {
	if(str!="")
		setValidationFailures += str+"<br>";
	setValidationFailedFields += "," + obj.name + ",";
	_markSETInvalid(obj);
}

/***********************************************
 * ALREADY FAILED
 *
 * Checks if obj is in failed fields string, 
 * which means it has already failed
 ***********************************************/
function _alreadySETFailed(obj){
	if(setValidationFailedFields.indexOf(","+obj.name+",") != -1)
		return true;
	return false;
}

/***********************************************
 * MARK INVALID
 *
 * Marks field as invalid by adding "fail" to
 * its css class.
 * Not FW since we may want to do other things
 * here as well!
 ***********************************************/
function _markSETInvalid( obj ) {
	if(!obj)
		return;

	//obj.style.backgroundColor = failcolor;
	if(String(obj.type).toUpperCase() == "HIDDEN"){
		if($(obj).attr("markInvalidSelector")) $($(obj).attr("markInvalidSelector")).addClass("SET_cssFail");
		obj = $(obj);
		obj.addClass("SET_cssFail");
		return;
	}
	// Add more classnames here for valid graphical fields to fail in special manner
	switch(obj.className){
		// Some fields require special handling, i.e. replace background image etc. 
		// others suffice to simply have the fail class added
/*		case "cssClassNameForSpecificObject":
			_appendFWClass(obj,"_fail",true);
			break;*/
		default:
			$(obj).addClass("SET_cssFail");
	}
	
	_setFWOnlyFirstFocus( obj );
}
	
function _unmarkSETInvalid(obj) {

	if(window.noUnmark) return;
	if(!obj){
		setValidationFailedFields = "";
		setValidationFailures = "";

		// Reset all colors
		$(".SET_cssFail:input",document.body).each(function(){
			var n = String(this.name);
			if(!window.doUnmarkId || n.substr(n.length-String(window.doUnmarkId).length).indexOf(String(window.doUnmarkId))!=-1){
				var self = $(this);
				if(self.attr("markInvalidSelector")) $(self.attr("markInvalidSelector")).removeClass("SET_cssFail");
				self.removeClass("SET_cssFail");
				_resetFWClass(this,"_fail",true);
			}
		});
		
	}else{
		$(obj).removeClass("SET_cssFail");
		_resetFWClass(obj,"_fail",true);
	}
}
