// JavaScript Document


/////FANCY FORM
////////////////
///form functions
////////////////
function toggleChecked(id,callback,notNull){
	var theBox=document.getElementById('select'+id);
	var theLink=document.getElementById('selectLink'+id);
	var isChecked=theBox.checked;	
	var topDrop=theBox.parentNode.parentNode;
	var runIt=1;
	if (notNull==1){
		var numChecked=getNumChecked(topDrop);
		if (isChecked && numChecked==1){
			runIt=0;
		}
	}
	if (runIt==1){
		if (isChecked){
			theBox.checked=false;
			theLink.className='';
		}else{
			theBox.checked=true;
			theLink.className='selected';
		}
		checkList(theBox.parentNode.parentNode);
		if (callback!=undefined && callback!=''){
			saveFancySelect(topDrop,callback);
		}
	}
}

function checkList(div){
	theLinks=div.getElementsByTagName("a");
	theInputs=div.getElementsByTagName("input");
	var selAllId=theInputs[0].id;
	if (selAllId.indexOf('selectAll')!=-1){
		theInputs[0].checked=false;
		theLinks[0].className='';
	}
	for (x=0;x<theInputs.length;x++){
		if(theInputs[x].checked==true && theLinks[x].className!='selected'){
			theLinks[x].className='selected';
		}
		if(theInputs[x].checked==false && theLinks[x].className!=''){
			theLinks[x].className='';
		}
	}
}
function selectAll(id){
	var theBox=document.getElementById('selectAll'+id);
	var theLink=document.getElementById('selectLinkAll'+id);
	var isChecked=theBox.checked;	
	
	var topDiv=theBox.parentNode.parentNode;
	var theLinks=topDiv.getElementsByTagName("a");
	var theInputs=topDiv.getElementsByTagName("input");
	
	if (isChecked){
		theBox.checked=false;
		theLink.className='';
	}else{
		theBox.checked=true;
		theLink.className='selected';
	}
	
	for (x=0;x<theInputs.length;x++){
		if(theBox.checked){
			theLinks[x].className='selected';
			theInputs[x].checked=true;
		}else{
			theLinks[x].className='';
			theInputs[x].checked=false;
		}
	}
}
function saveFancySelect(div,callback){
	var theLinks=div.getElementsByTagName("a");
	var theInputs=div.getElementsByTagName("input");
	var labels=new Array();
	var values=new Array();
	for (x=0;x<theInputs.length;x++){
		if(theInputs[x].checked==true && theInputs[x].name!='selectAll'){	
			labels[labels.length]=theLinks[x].innerHTML;
			values[values.length]=theInputs[x].value;
		}
	}
	if (theInputs[0].name=='selectAll' && theInputs[0].checked==true){
		var labelsStr='All';	
	}else{
		var labelsStr=labels.join(', ');
		if (labelsStr==''){labelsStr='None';}
	}
	div.parentNode.getElementsByTagName("span")[0].innerHTML=labelsStr;
	var valuesStr=values.join(',');
	callback=str_ireplace('{selections}',valuesStr,callback )
	eval(callback);
	new Effect.BlindUp(div.id,{ duration: .4 });
}

function getNumChecked(topDrop){
	var theInputs=topDrop.getElementsByTagName("input");
	var numSel=0;
	for (x=0;x<theInputs.length;x++){
		if(theInputs[x].checked){
			numSel+=1;
		}
	}
	return numSel;
}

////////////////
///end form functions
////////////////
///////END FANCY FORM


////////////ajax
var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

////////END AJAX///////////


////////////////
///utilities
////////////////
function btn_overs(sel, newImage){
	var theImage=document.getElementById(sel);
	theImage.src=newImage;
}
//uploader
function getUploadResults(theResults){
	var callback=document.getElementById("uploadCallback").value;
	var parsedCallback=str_ireplace('{uploads}',theResults,callback);
	eval (parsedCallback);
	//resetForm();
	//document.getElementById("uploader").innerHTML=theResults;
}
function adjustUploaderIframeHeight(uniqueId,newHeight){
	if (newHeight>60){
		var frameWrap=document.getElementById('uploadWrap'+uniqueId);
		frameWrap.style.height=newHeight+'px';
	}
}

/*function insertDom(type,parentid,newid,html){
	alert (type+' '+parentid+' '+newid+' '+html);
	var theNewElement = document.createElement(type);
	theNewElement.setAttribute('id', newid);
	document.getElementById(parentid).insertBefore(theNewElement,document.getElementById(parentid).childNodes[0]);
	document.getElementById(newid).innerHTML=html;
}*/

function refreshSortable(){
	Sortable.create('listWrap_notes',{	
						tag:'li',
						ghosting:false,
						constraint:'vertical',
						handles:$$('#actions_notes b'),
						onUpdate:function(){
							saveSelections('reorder','entries',parseStandardReorderString('listWrap_notes',Sortable.serialize('listWrap_notes')));
							}
						}
	);	
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function str_ireplace ( search, replace, subject ) {
    var i, k = '';
    var searchl = 0;
    search += '';
    searchl = search.length;
    if (!(replace instanceof Array)) {
        replace = new Array(replace);
        if (search instanceof Array) {
            while (searchl > replace.length) {
                replace[replace.length] = replace[0];
            }
        }
    }
    if (!(search instanceof Array)) {
        search = new Array(search);
    }
    while (search.length>replace.length) {
        replace[replace.length] = '';
    }
    if (subject instanceof Array) {
        for (k in subject) {
            subject[k] = str_ireplace(search, replace, subject[k]);
        }
        return subject;
    }
    searchl = search.length;
    for (i = 0; i < searchl; i++) {
        reg = new RegExp(search[i], 'gi');
        subject = subject.replace(reg, replace[i]);
    }
   return subject;
}
//pop up div
function popupDiv(id,pg,ypos){
	//clear old shit
	var theText=document.getElementById("popText");
	theText.innerHTML='Loading ...';
	
	//move in Box
	var theBox=document.getElementById("popupDiv");
	theBox.style.top=ypos+'px';
	
	//load new shit
	ajaxpage(pg, 'popText');
}

function closePopDiv(){
	var theBox=document.getElementById("popupDiv");
	theBox.style.top='-1000px';
	
	//clear old shit
	var theText=document.getElementById("popText");
	theText.innerHTML='';
}
////////////////
///end utilities
////////////////
