/*  NeoPortal JavaScript framework, version 1.0
 *  (c) 2009 Meganexus
 *
 *	author =  James Parry
 *
 *
 *
 ______   _______    _        _______ _________
(  __  \ (  ___  )  ( (    /|(  ___  )\__   __/
| (  \  )| (   ) |  |  \  ( || (   ) |   ) (   
| |   ) || |   | |  |   \ | || |   | |   | |   
| |   | || |   | |  | (\ \) || |   | |   | |   
| |   ) || |   | |  | | \   || |   | |   | |   
| (__/  )| (___) |  | )  \  || (___) |   | |   
(______/ (_______)  |/    )_)(_______)   )_(   
                                               
 _______           _______  _        _______  _______  _  _ 
(  ____ \|\     /|(  ___  )( (    /|(  ____ \(  ____ \( )( )
| (    \/| )   ( || (   ) ||  \  ( || (    \/| (    \/| || |
| |      | (___) || (___) ||   \ | || |      | (__    | || |
| |      |  ___  ||  ___  || (\ \) || | ____ |  __)   | || |
| |      | (   ) || (   ) || | \   || | \_  )| (      (_)(_)
| (____/\| )   ( || )   ( || )  \  || (___) || (____/\ _  _ 
(_______/|/     \||/     \||/    )_)(_______)(_______/(_)(_)
                                                            
_________         _________ _______ 
\__   __/|\     /|\__   __/(  ____ \
   ) (   | )   ( |   ) (   | (    \/
   | |   | (___) |   | |   | (_____ 
   | |   |  ___  |   | |   (_____  )
   | |   | (   ) |   | |         ) |
   | |   | )   ( |___) (___/\____) |
   )_(   |/     \|\_______/\_______)
                                    
          _______  _______  _______ _________ _______  _       
|\     /|(  ____ \(  ____ )(  ____ \\__   __/(  ___  )( (    /|
| )   ( || (    \/| (    )|| (    \/   ) (   | (   ) ||  \  ( |
| |   | || (__    | (____)|| (_____    | |   | |   | ||   \ | |
( (   ) )|  __)   |     __)(_____  )   | |   | |   | || (\ \) |
 \ \_/ / | (      | (\ (         ) |   | |   | |   | || | \   |
  \   /  | (____/\| ) \ \__/\____) |___) (___| (___) || )  \  |
   \_/   (_______/|/   \__/\_______)\_______/(_______)|/    )_)
                                                               
_________ _______    _______  _        ______              _______  _______ 
\__   __/(  ____ \  (  ___  )( \      (  __  \   |\     /|(  ____ \(  ____ \
   ) (   | (    \/  | (   ) || (      | (  \  )  | )   ( || (    \/| (    \/
   | |   | (_____   | |   | || |      | |   ) |  | |   | || (_____ | (__    
   | |   (_____  )  | |   | || |      | |   | |  | |   | |(_____  )|  __)   
   | |         ) |  | |   | || |      | |   ) |  | |   | |      ) || (      
___) (___/\____) |  | (___) || (____/\| (__/  )  | (___) |/\____) || (____/\
\_______/\_______)  (_______)(_______/(______/   (_______)\_______)(_______/
                                                                            
 _______     _______ 
/ ___   )   (  __   )
\/   )  |   | (  )  |
    /   )   | | /   |
  _/   /    | (/ /) |
 /   _/     |   / | |
(   (__/\ _ |  (__) |
\_______/(_)(_______)
                     
 * This Version has been deprecated. please use version 2.0
 *--------------------------------------------------------------------------*/

NeoPortal = {};

NeoPortal.Ajax = {};
/* */
NeoPortal.Ajax.addOnClick = function(observeId,targetId, urlLocation){Event.observe($(observeId), 'click', function(e){var loadingTarget = $$('#'+targetId+' div.listing'); NeoPortal.Ajax.Update(targetId,urlLocation,loadingTarget); Event.stop(e);});};
NeoPortal.Ajax.Update=function(targetId, urlLocation,loadingTarget){ new Ajax.Updater(targetId,urlLocation,{method: 'post',evalScripts:true, onCreate: function(transport){if(loadingTarget!=null){NeoPortal.Ajax.showLoading(loadingTarget);}} ,onComplete: function(transport){ if(jQuery){ jQuery(document).trigger('neoPaginatorReady'); } } });};
NeoPortal.Ajax.showLoading = function(targetId){targetId.each(function(s) {s.update("<span class=\"loading ui-neo-loading\">Loading...</span>");});};

NeoPortal.Form = {};
NeoPortal.Form.submit = function(targetForm) {
	
	
	
	var	extraJSON = $H({usingAjaxCall:'true'});	
	new Ajax.Updater("ajaxColorBoxHolder",$(targetForm).action,
			{
				parameters: extraJSON.merge($(targetForm).serialize(true)),
				method: 'post',
				evalScripts:true, 
				onCreate: function(transport){jQuery.fn.colorbox.resize(); },
				onComplete: function(transport){ jQuery.fn.colorbox({inline:true, href:"#ajaxColorBoxHolder"});} 
			}
	);

	return false;
}

NeoPortal.Form.submitWithIFrame = function(targetForm) {
	$(targetForm).target = 'upload_target'; //'upload_target' is the name of the iframe
	$(targetForm).submit();
	return false;
}


NeoPortal.Form.initSubSelect = function(sourceName, targetName,targetFullName, targetAcronymName, additionalFunction) {
	var source = $(sourceName);
	var target = $(targetName);
	var targetAcronym = $(targetAcronymName);
	var targetFull = $(targetFullName);
	
	subSelect = {
		// some stuff our 'generic' function needs
  		change: function(event) {
			NeoPortal.Form.changeSubSelect(event.element(),target,targetFull,targetAcronym);
    		
    		//alert(additionalFunction);
    		if (additionalFunction != null) {
    		additionalFunction();
    		}
  		}
	};
	
	source.observe('change', subSelect.change.bindAsEventListener(subSelect));

	
	if (source.getValue() != -1)
	{
		NeoPortal.Form.changeSubSelect(source,target,targetFull,targetAcronym)
		
	}

	for (var i=0; i < target.options.length; i++)
	{
		var option = target.options[i];
		
		if (option.value == targetFull.getValue())
		{
			option.selected = true;	
		}
	} 
} 

NeoPortal.Form.changeSubSelect =  function(source,target,targetFull,targetAcronym) {

  	superValue = $(source).getValue();
  	
  	var subs =new Array();
  	var x=0;
  	// Make array of ints
  	for (var i=0; i < targetAcronym.options.length; i++)
  	{
  		var option = targetAcronym.options[i];
  		
  		var optionArray = option.text.split(",");
  		
  		for (var j=0; j < optionArray.length; j++)
  		{
  			if (optionArray[j] == superValue)
  			{
  				subs[x] = option.value;
  				x++;
  			}
  		}
  	}

  	//Remove all Current
  	for (var j = target.length - 1; j>=1; j--)	
  	{	
  		target.remove(j)
  	}
  	
  	for (var k=0; k < targetFull.length; k++)
  	{
  		var option = targetFull.options[k];
  		if ($(subs).indexOf(option.value) != -1)
  		{
  			 var optNew = document.createElement('option');
  			 optNew.text = option.text;
  			 optNew.value = option.value;
             		
  		
 	 		try {
	  		  target.add(optNew, null); // standards compliant; doesn't work in IE
	  		}
	  		catch(ex) {
	  		  target.add(optNew); // IE only
	  		}	
  		}
  	}
}


NeoPortal.Form.addToSelect = function(selectId, id, value) {

	 var newOption=document.createElement('option');
	 newOption.text = value;
	 newOption.value = id;
	 try
	  {
		 $(selectId).add(newOption,null); // standards compliant
	  }
	 catch(ex)
	  {
		 $(selectId).add(newOption); // IE only
	  }
	 $(selectId).selectedIndex = $(selectId).options.length -1;
	 $(selectId).focus();
	
} 


NeoPortal.Form.checkbox = {};
NeoPortal.Form.checkbox.selectAll = function(className,checkBoxAll)
{
	
	toCheck = $$(className);
	
	for (var index = 0; index < toCheck.length; ++index) {
	  var item = toCheck[index];
	  item.checked = checkBoxAll.checked;	
	}
	
}


