// JavaScript Document
// UPDATED TO JQUERY 1.4


// NEW class based core

function fw_core() {
	
	this.spinnerHtml = '<img src="/_pbframework/images/sys/ajax-loader.gif">';
	
	this.ajaxBridge = function(data,replacement) {
		
		this.handleErrors(data);
		
	}
	
	this.handleErrors = function(data) {

		switch(data.status) {
		
		default:
			return true;
		break
		
		
		case "FATAL":
		case "HACK":
			if (data.errors){
			alert('Error: '+data.errors);
			} else {
			alert('Error: Unable to complete action');
			}
			return false;
		break;
		
		}
		
	}
	
	this.customSubmit = function (formID,newActionVar,newTarget,newDestination,confirmMessage) {
		if (!confirmMessage || confirm(confirmMessage)) {
			var fm = document.getElementById(formID);
			if (fm.submitting) { return false; }
			fm.submitting = true;
			var ac = fm.elements['_action'];
			ac.oldValue = ac.value;
			ac.value = newActionVar;
			if (newTarget) {
			fm.oldTarget = fm.target;
			fm.target=newTarget;
			}
			if (newDestination) {
			fm.oldAction = fm.action;
			fm.action = newDestination;
			}
			$(fm).submit();
			ac.value = ac.oldValue;
			fm.target = fm.oldTarget;	
			if (newDestination) {
				fm.action = fm.oldAction;
			}
			if (newTarget) {
				fm.target = fm.oldTarget;
			}
			fm.submitting=false;
		}
	}
	
	this.fieldHint = function (jqObj,hint) {
		var myObj = jqObj[0];
		
		if (!hint) { hint=this.displayFieldName(myObj.name); }
		jqObj.each( function() { 
			this.fwUsed=false; 
			this.hint = hint; 
			
			if (!this.value || this.fwCleared===true) { 
				this.value=hint ;
				this.fwCleared=false;
			} else { 
				if (this.value != this.hint) { 
					this.fwUsed=true; 
				}
			} 
	
			if (!this.fwUsed) { $(this).addClass('fwFieldHint'); }
			
			if (!this.hinted) {
			$(this).focus( function() { if (!this.fwUsed) { this.value=''; this.fwFocused = true; $(this).removeClass('fwFieldHint'); } });
			$(this).keydown( function() { this.fwUsed = true; } );
			$(this).blur( function() { if (!this.fwUsed || !this.value) { this.value=this.hint; $(this).addClass('fwFieldHint'); this.fwUsed=false; } else { $(this).removeClass('fwFieldHint'); } });
			$(this).parents('form:first').submit( function() { if (!myObj.fwUsed) { myObj.value=''; myObj.fwCleared=true; }  } );
			this.hinted = true;
			}
		} );
		
	}
	
	this.hintFields = function (jqObj) {
		var core = this;
		jqObj.each( function() { 
			if ($(this).attr('title')) { 	
			core.fieldHint($(this),$(this).attr('title'));
			}
		});
	}
	
	
	this.displayFieldName = function(name) {
	oldName = new String(name);
	newName = new String('');
	newName += oldName.substr(0,1);
	newChar = new String('');
	newName = newName.toUpperCase(); 
	for (i=1;i<oldName.length;i++) {
		newChar = oldName.substr(i,1);
		if (newChar.charCodeAt(0)>90) {
			newName+=newChar;
		} else {
			newChar = newChar.toUpperCase();
			newName += ' '+newChar;
		}
	}
	return newName;
	}	
	
}

var fwCore = new fw_core();






// LOTS OF LEGACY CODE



var fw_helpWin=false;

function activateMenu(root,url) {
	
	try {
	hnode = toolbox_nodes_getActiveLink(root,url);
	hnode.className = "active";
	par = toolbox_nodes_getFirstParentByType(hnode,'li');
	par.className = "active";
	} catch(e) { }
}

function activateLocation(root) {
	hnode = document.getElementById(root);
	activateMenu(hnode,window.location);
}


function activateImageMenu(root,url) {
	hnode = toolbox_nodes_getActiveLink(root,url);
	inode = toolbox_nodes_getFirstOf(hnote,'img');
	var nSrc = new String(inode.src);
	inode.src = nSrc.replace('_up','_over');
	inode.onmouseover=function(){}
}

function fw_showHelp() {
	fw_helpWin = fw_popWin('/help/home/','IndigoHelp',0,1,0,0,0,1,780,600);
	fw_helpWin.focus();
}

function fw_popWin(url,winName,toolbar,scrollbars,location,statusbar,menubar,resizable,width,height) {
	lft = ',left='+((screen.width-width)/2);
	tp = ',top='+((screen.height-height)/2);
	toolbar = 'toolbar='+toolbar;
	scrollbars=',scrollbars='+scrollbars;
	location=',location='+location;
	statusbar=',statusbar='+statusbar;
	menubar=',menubar='+menubar;
	resizable=',resizable='+resizable;
	width=',width='+width;
	height=',height='+height;
	params = ' '+toolbar+scrollbars+location+statusbar+menubar+resizable+width+height+lft+tp;
	var popWin = window.open(url,winName,params);
	return popWin;
}

function fw_modalWin(url,winName,scrollbars,width,height,resizable) {
	if (window.showModalDialog) {
	var popWin = window.showModalDialog(url,winName,"dialogWidth:"+width+"px;dialogHeight:"+height+"px");
	} else {
	var popWin = window.open(url,winName,'height='+height+',width='+width+',toolbar=no,directories=no,status=no,menubar=no,scrollbars='+scrollbars+',resizable='+resizable+',modal=yes');
	}
	return popWin;
} 

function fw_changeActSubmitToBlank(formName,actValue,action,message) {

	doit = true;
	if (message) { doit = confirm(message); }

	if (doit) {

		myfm = document.forms[formName];
		myfm.oldAct = myfm.elements['_action'].value;
		myfm.elements['_action'].value = actValue;
		myfm.oldTarget = myfm.target;
		myfm.oldAction = myfm.action;
		myfm.target='_blank';
		myfm.action=action;
		
		try{myfm.onsubmit();}catch(err){}
		

		myfm.submit();

		myfm.elements['_action'].value=myfm.oldAct;
		myfm.action = myfm.oldAction;
		myfm.target = myfm.oldTarget;

	}
}

function fw_changeActSubmit(formName,actValue,message) {

	doit = true;
	if (message) { doit = confirm(message); }

	if (doit) {
		myfm = document.forms[formName];
		myfm.oldAct = myfm.elements['_action'].value;
		myfm.elements['_action'].value = actValue;
		try{
			myfm.onsubmit();
		} catch (err) {
		}
		myfm.submit();
		myfm.elements['_action'].value=myfm.oldAct;

	}
}



function fw_redirectOnConfirm(message,loc) {
	if (confirm(message)) {
		location = loc;
	}
}

function fw_showHide(caller,objID) {
	var obj = document.getElementById(objID);
	var result = toolbox_style_toggleDisplay(obj);
	if (result) {
		caller.className = 'toggleOpen';
	} else {
		caller.className = 'toggleClose';
	}
}


function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function killCookie(name) {
	setCookie(name,"",-1);
}






function nothing(obj) {
	return false;
}




// # AJAX

function ajaxReplace(url,replacementID) {
	var myRep = replacementID;
	$(myRep).html="LOADING...";
	$.get(url, function(data) {
		  $('#'+myRep).html(data);
	});
}

function ajaxSubmit(formID,replacementID) {
	var fm = document.getElementById(formID);
	var myreplacement = $('#'+replacementID);
	$.post(fm.action,$(fm).serialize(),function(data) { myreplacement.html(data)    });
}

function ajaxOnSubmit(obj,replacementID) {
	var fm = obj;
	var myreplacement = $('#'+replacementID);
	$.post(fm.action,$(fm).serialize(),function(data) { myreplacement.html(data)    });
}

function ajaxAlert(targetID,message) {
	document.getElementById(targetID).innerHTML = message;
}

function nothing() {
	return false;
}

function fwCopyField(ffrom,fto) {
	var cfrom = document.getElementById(ffrom);
	var cto = document.getElementById(fto);
	cto.value = cfrom.value;
}

function fwCopyDD(ffrom,fto) {
	var cfrom = document.getElementById(ffrom);
	var cto = document.getElementById(fto);
	for(i=0;i<cto.options.length;i++) {
		if (cto.options[i].value == cfrom.options[cfrom.selectedIndex].value) {
			cto.selectedIndex = i;
			return true;
		}
	}
	cto.selectedIndex=0;
}

function initRollover(obj) {
	var oldsrc = new String(obj.src);
	obj.upsrc = oldsrc;
	obj.oversrc = oldsrc.replace('_up','_over');
	obj.onmouseover = function() { this.src = this.oversrc; }
	obj.onmouseout = function() { this.src = this.upsrc; }
	obj.onload = function() { }
}

function fwSetTabs(tablabel,tabblock,id) {
	var tabPages = $('#'+tabblock).find('.tabPage');
	tabPages.each(function(item) { this.style.display='none'; });
	var activeTab = $('#'+id);
	activeTab[0].style.display='block';
	var tabLabels = $('#'+tabblock).find('.tabLabelActive');
	tabLabels.each(function(item) { this.className='tabLabel'; });
	tablabel.className='tabLabelActive';
}

function fwToggleByCheckbox(togid,cbox,reverse){
	try{
	cbox = document.getElementById(cbox);
	if (!reverse) {
		if (cbox.checked) {
			$(togid).show();
		} else {
			$(togid).hide();
		}
	} else {	
	if (cbox.checked) {
		$(togid).hide();
	} else {
		$(togid).show();
	}
	}
	} catch(e) {	
	}	
}

function fireEvent(element,event){
    if (document.createEventObject){
        // dispatch for IE
    	alert('event');
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

function showDialog(content,paramObj){
		
		
			
		var params= {'modal':false,
		'title':'',
		'fixed':false};
		
		
		$.extend(params,paramObj);
		
		
		switch(params.type) {
			
			case "iframe":
				var height='300px';
				var width="300px";
				params.unloadOnHide=true;
				if (params.width) { width = params.width; }
				if (params.height) { height = params.height; }
				
				var content = '<iframe src="'+content+'" style="width:'+width+'; height:'+height+'; border:none; padding:0px; margin:0px;"></iframe>';
				
			break;
			
		}

		var myDialog=new Boxy(content,params);
		
		return myDialog;
		
		
	
}






