// JavaScript Document

	var featureSearchTimeout;

	function featureSearchSpring()
	{
		featureSearchTimeout = setTimeout('featureSearch();', 350);
	}

	function featureSearch ()
	{
		q = document.getElementById('search').value;
		
		if (!q.length)
		{
			hideBox('searchResults');
			return;
		}

		var cObj = YAHOO.util.Connect.asyncRequest('GET','/lib/request/featureSearch.php?search='+URLEncode(q),featureSearchCallback,null);
	}
	
	var featureSearchSuccess = function(o)
	{
		if (o.responseText == '0')
		{
			hideBox('searchResults');
			return;
		}
		
		searchBox = document.getElementById('searchResults');
		searchBox.innerHTML = o.responseText;
		
		var fieldbox = YAHOO.util.Region.getRegion('search');
		var pos = Array();
		//showBox('searchResults');			
		searchBox.style.display = 'block';
		pos[0] = (fieldbox.left + 118) - searchBox.offsetWidth;
		pos[1] = fieldbox.top + document.getElementById('search').offsetHeight + 5;	
		YAHOO.util.Dom.setXY(searchBox, pos);
	}
	
	var featureSearchFailure = function(o)
	{
		hideBox('searchResults');
	}
	
	var featureSearchCallback =
	{
		success: featureSearchSuccess,
		failure: featureSearchFailure
	}	
	
/* dataSuggest */

	function dataSuggest ()
	{
		this.fieldname = new Array('dataSuggestEntryBox', 0);
		this.searchfield = new Array('', 0);
		this.searchtable = '';
		this.anchorobject = '';
		this.setfield = '';
		this.setcolumn = '';
		this.settitle = '';
		this.alsoWhere = '';
		this.args = '';
		
		this.either = 0;
		this.hideOnSelect = '';
		this.showOnSelect = '';
		
		this.iteration = 0;
		
		this.getGenericSuccess = function (o)
		{
			dataSuggestCurrent.showBox();
			document.getElementById('dataSuggestBoxResults').innerHTML = o.responseText;
		}
		
		this.getGeneric = function (url)
		{
			var callback =
			{
				success: dataSuggestCurrent.getGenericSuccess,
				failure: dataSuggestCurrent.failed
			}	
			
			var cObj = YAHOO.util.Connect.asyncRequest('GET','/lib/request/'+url,callback,null);
		}
		
		this.setField = function (value, display)
		{
			console.log('setting '+this.setfield+' to '+value);
			document.getElementById(this.setfield).value = value;
			document.getElementById(this.settitle).innerHTML = display;
			hideBox(this.hideOnSelect);
			showBox(this.showOnSelect);
			this.hide();
		}	

		this.show = function (returndata)
		{			
			var fieldname = returndata[1];
			
			if (returndata[2] == '0')
			{
				if (document.getElementById('dataSuggestEntry').style.display == 'none')
					this.hide();
				return;
			}
		
			dataSuggestCurrent.showBox();
			document.getElementById('dataSuggestBoxResults').innerHTML = returndata[2];
		}
		
		this.success = function (o)
		{
			var returndata = o.responseText.split('|||');
			
			if (returndata[0] < dataSuggestCurrent.iteration)
				return;
				
			dataSuggestCurrent.show(returndata);
		}
		
		this.showBox = function ()
		{		
			if (document.getElementById('dataSuggestBox').style.display == 'none')
				document.getElementById('dataSuggestBoxResults').innerHTML = '';
			
			var fieldbox = YAHOO.util.Region.getRegion(this.anchorobject);
			var pos = Array();
			pos[0] = fieldbox.left;
			pos[1] = fieldbox.top + document.getElementById(this.anchorobject).offsetHeight;
			//alert(pos[0]+' '+pos[1]);
			document.getElementById('dataSuggestBox').style.display = '';		
			YAHOO.util.Dom.setXY('dataSuggestBox', pos);
			if (document.getElementById(this.fieldname))
				document.getElementById(this.fieldname).focus();
		}
		
		this.hide = function ()
		{
			this.iteration++;
			hideBox('dataSuggestBox');
		}
		
		this.failed = function(o)
		{
			console.log('dataSuggest failed');
		}
			
		this.suggest = function (iteration)
		{
			if (iteration < this.iteration)
				return;
				
			var addToRequest = '';
			var doSearch = 0;
			//alert(this.searchfield.length);
			for (var x=0; x < this.searchfield.length; x++) //x in this.searchfield)
			{
				addToRequest += '&searchfield['+x+']='+this.searchfield[x];
				if (this.fieldname[x])
				{
					if (document.getElementById(this.fieldname[x]).value.length)
						doSearch = 1;
					addToRequest += '&sofar['+x+']='+document.getElementById(this.fieldname[x]).value;
				}
			}
			
			if (!doSearch)
			{
				this.hide();
				return;
			}
			
			var callback =
			{
				success: dataSuggestCurrent.success,
				failure: dataSuggestCurrent.failed
			}	
			
			this.iteration++;
			var cObj = YAHOO.util.Connect.asyncRequest('GET','/core/request/dataSuggest.php?iteration='+this.iteration+'&searchtable='+this.searchtable+'&fieldname='+this.fieldname+'&setfield='+this.setfield+'&setcolumn='+this.setcolumn+'&settitle='+this.settitle+'&either='+this.either+'&alsowhere='+this.alsoWhere+this.args+addToRequest,callback,null);
		}	
		
		this.spring = function (hideinbuilt)
		{		
			if (hideinbuilt)
				document.getElementById('dataSuggestEntry').style.display = 'none';
			else
				document.getElementById('dataSuggestEntry').style.display = '';
			
			setTimeout('dataSuggestCurrent.suggest('+this.iteration+');', 350);
		}		
	}
	
	var dataSuggestCurrent = new dataSuggest();
	
	function dataSuggestCreate (fieldname, searchfield, searchtable, anchorobject, setfield, setcolumn, settitle, hideOnSelect, showOnSelect, hideinbuilt)
	{		
		if (!isArray(fieldname))
		{
			dataSuggestCurrent.either = 1;
			fieldname = new Array(fieldname);
		}

		if (!isArray(searchfield))
			searchfield = new Array(searchfield);
		
		dataSuggestCurrent.fieldname = fieldname;
		dataSuggestCurrent.searchfield = searchfield;
		dataSuggestCurrent.searchtable = searchtable;
		dataSuggestCurrent.anchorobject = anchorobject;
		dataSuggestCurrent.setfield = setfield;
		dataSuggestCurrent.setcolumn = setcolumn;
		dataSuggestCurrent.settitle = settitle;
		
		dataSuggestCurrent.hideOnSelect = hideOnSelect;
		dataSuggestCurrent.showOnSelect = showOnSelect;
		
		dataSuggestCurrent.alsoWhere = '';
		dataSuggestCurrent.args = '';
		
		if (hideinbuilt)
			dataSuggestCurrent.spring(1);
		else
			dataSuggestCurrent.showBox();
	}
	
	function contactSelectorPrefill (name_first, name_last, company_id, ph_office, ph_mobile, email)
	{
		this.name_first = name_first;
		this.name_last = name_last;
		this.company_id = company_id;
		this.ph_office = ph_office;
		this.ph_mobile = ph_mobile;
		this.email = email;
	}
	
	function contactSelector(field)
	{
		this.field = field;
		this.theAnchor = 'contactSelectorButtons_'+this.field;
		
		this.createSuccess = function (o)
		{
			if (o.responseText == '0')
			{
				alert('Contact creation failed - please try again.');
				return;
			}
			
			bits = o.responseText.split('|||');
			field = contactSelectorCurrent.field;
			
			document.getElementById(field).value = bits[1];
			document.getElementById('contactSelectorDisplay_'+field).innerHTML = bits[2];
			hideBox('contactSelectorButtons_'+field);
			showBox('contactSelectorDisplay_'+field);
			showBox('contactSelectorRight_'+field);
			hideBox('contactCreateBox');			
		}
		
		this.createFailure = function(o)
		{
			return;
		}
		
		this.create = function (f)
		{
			// do validation here
			
			var callback = 
			{
				success:contactSelectorCurrent.createSuccess,
				failure:contactSelectorCurrent.createFailure
			}
			
			var cObj = YAHOO.util.Connect.asyncRequest('GET','/core/request/contactCreate.php?field='+document.getElementById('contactCreateField').value+'&name_first='+URLEncode(f.contactCreate_name_first.value)+'&name_last='+URLEncode(f.contactCreate_name_last.value)+'&company_id='+URLEncode(f.contactCreate_company_id.value)+'&company_new='+URLEncode(f.contactCreate_company_new.value)+'&ph_office='+URLEncode(f.contactCreate_ph_office.value)+'&ph_mobile='+URLEncode(f.contactCreate_ph_mobile.value)+'&email='+URLEncode(f.contactCreate_email.value),callback,null);			
		}
		
		this.showCreate = function ()
		{
			contactSelectorCurrent = this;

			f = document.getElementById('contactCreateForm');
			f.reset();

			f.contactCreate_name_first.value = this.prefill.name_first;
			f.contactCreate_name_last.value = this.prefill.name_last;
			f.contactCreate_ph_office.value = this.prefill.ph_office;
			f.contactCreate_ph_mobile.value = this.prefill.ph_mobile;
			f.contactCreate_email.value = this.prefill.email;
			
			var fieldbox = YAHOO.util.Region.getRegion(this.theAnchor);
			var pos = Array();
			
			pos[0] = fieldbox.left;
			pos[1] = fieldbox.top + document.getElementById(this.theAnchor).offsetHeight;

			showBox('contactCreateBox');		
			YAHOO.util.Dom.setXY('contactCreateBox', pos);
			
			document.getElementById('contactCreate_name_first').focus();		
		}
		
		this.startSearch = function ()
		{
			hideBox('contactCreateBox');
			document.getElementById('dataSuggestEntryBox').value = '';
			dataSuggestCreate('dataSuggestEntryBox',Array('name_last','name_first'),'contacts',this.theAnchor, this.field,'contact_id','contactSelectorDisplay_'+this.field,'contactSelectorButtons_'+this.field,'contactSelectorRight_'+this.field);
			document.getElementById('dataSuggestEntryBox').focus();
		}
		
		this.clear = function ()
		{
			setField(this.field, '0');
			setHTML('contactSelectorDisplay_'+this.field,'');
			showBox('contactSelectorButtons_'+this.field);
			hideBox('contactSelectorRight_'+this.field);
		}
	}
	
	var contactSelectorCurrent = new contactSelector();
	
	function companySelector (field)
	{
		this.field = field;
		
		this.startSearch = function ()
		{
			document.getElementById('dataSuggestEntryBox').value = '';
			dataSuggestCreate('dataSuggestEntryBox', 'name', 'companies', 'companySelectorButtons_'+this.field, this.field, 'company_id', 'companySelectorDisplay_'+this.field,'companySelectorButtons_'+this.field,'companySelectorRight_'+this.field);
			document.getElementById('dataSuggestEntryBox').focus();
		}
		
		this.clear = function ()
		{
			setField(this.field, '0');
			setHTML('companySelectorDisplay_'+this.field,'');
			showBox('companySelectorButtons_'+this.field);
			hideBox('companySelectorRight_'+this.field);
			hideBox('companySelectorNew_'+this.field);			
			
			if (document.getElementById('companySelectorNewField_'+this.field))
				document.getElementById('companySelectorNewField_'+this.field).value = '';
		}
		
		this.showCreate = function ()
		{
			hideBox('companySelectorButtons_'+this.field);
			showBox('companySelectorNew_'+this.field);
			showBox('companySelectorRight_'+this.field);
			document.getElementById('companySelectorNewField_'+this.field).focus();
		}
	}
	
	var companySelectorCurrent = new companySelector();
	
	function courseSelector (field)
	{
		this.field = field;
		this.selectInstance = false;
		
		this.startSearch = function (custom)
		{
			document.getElementById('dataSuggestEntryBox').value = '';
			dataSuggestCreate('dataSuggestEntryBox', 'title', 'courses', 'courseSelectorButtons_'+this.field, this.field, 'course_id', 'courseSelectorDisplay_'+this.field,'courseSelectorButtons_'+this.field,'courseSelectorRight_'+this.field);
			if (custom)
				dataSuggestCurrent.alsoWhere = 'course_group_id=0';
			else
				dataSuggestCurrent.alsoWhere = 'course_group_id!=0';
			if (this.selectInstance)
				dataSuggestCurrent.args = '&selectInstance=1';
			document.getElementById('dataSuggestEntryBox').focus();
		}
		
		this.clear = function ()
		{
			setField(this.field, '0');
			setHTML('courseSelectorDisplay_'+this.field,'');
			showBox('courseSelectorButtons_'+this.field);
			hideBox('courseSelectorRight_'+this.field);		
			
			if (document.getElementById('courseSelectorNewField_'+this.field))
				document.getElementById('courseSelectorNewField_'+this.field).value = '';
		}
		
		this.showCreate = function ()
		{
			hideBox('courseSelectorButtons_'+this.field);
			showBox('courseSelectorNew_'+this.field);
			showBox('courseSelectorRight_'+this.field);
			document.getElementById('courseSelectorNewField_'+this.field).focus();
		}
	}
	
	var courseSelectorCurrent = new courseSelector();
	
	function itemSelector (field)
	{
		this.field = field;
		this.parentOnly = 0;
		
		this.startSearch = function ()
		{
			document.getElementById('dataSuggestEntryBox').value = '';
			dataSuggestCreate('dataSuggestEntryBox', 'title', 'store_items', 'itemSelectorButtons_'+this.field, this.field, 'item_id', 'itemSelectorDisplay_'+this.field,'itemSelectorButtons_'+this.field,'itemSelectorRight_'+this.field);
			if (this.parentOnly)
				dataSuggestCurrent.alsoWhere = 'parent_id%3D0';
			document.getElementById('dataSuggestEntryBox').focus();
		}
		
		this.clear = function ()
		{
			setField(this.field, '0');
			setHTML('itemSelectorDisplay_'+this.field,'');
			showBox('itemSelectorButtons_'+this.field);
			hideBox('itemSelectorRight_'+this.field);		
			
			if (document.getElementById('itemSelectorNewField_'+this.field))
				document.getElementById('itemSelectorNewField_'+this.field).value = '';
		}
		
		this.showCreate = function ()
		{
			hideBox('itemSelectorButtons_'+this.field);
			showBox('itemSelectorNew_'+this.field);
			showBox('itemSelectorRight_'+this.field);
			document.getElementById('itemSelectorNewField_'+this.field).focus();
		}
	}
	
	var itemSelectorCurrent = new itemSelector();	
	
	
	function addReminder ()
	{
		this.success = function(o)
		{
			if (o.responseText == '1')
				hideBox('addReminderBox');
			else
				this.failure(o);
		}
		
		this.failure = function(o)
		{
			alert('Reminder creation failed, please try again.');
		}
		
		this.add = function(f)
		{
			if (f.rem_repeat_on.checked)
				repeat = 1;
			else
				repeat = 0;
			
			var string = '/lib/request/reminderCreate.php?remote=1&rem_year='+f.rem_year.value+'&rem_month='+f.rem_month.value+'&rem_day='+f.rem_day.value+'&rem_description='+URLEncode(f.rem_description.value)+'&rem_repeat_on='+repeat+'&rem_repeat_period='+f.rem_repeat_period.value;
			if (f.rem_instance_id && f.rem_instance_id.checked)
				string += '&instance_id='+f.rem_instance_id.value;
			if (f.rem_contact_id && f.rem_contact_id.checked)
				string += '&contact_id='+f.rem_contact_id.value;
			if (f.rem_order_id && f.rem_order_id.checked)
				string += '&order_id='+f.rem_order_id.value;			
	
			var callback =
			{
				success: addReminderGlobal.success,
				failure: addReminderGlobal.failure
			}	
	
			var cObj = YAHOO.util.Connect.asyncRequest('GET', string, callback,null);
		}
		
		this.show = function ()
		{
			theAnchor = document.getElementById('addReminderAnchor');
			theDiv = document.getElementById('addReminderBox');
			
			var fieldbox = YAHOO.util.Region.getRegion(theAnchor);
			var pos = Array();
			pos[0] = fieldbox.left;
			pos[1] = fieldbox.top + theAnchor.offsetHeight;
			showBox('addReminderBox');			
			YAHOO.util.Dom.setXY('addReminderBox', pos);
			//alert('pos is '+pos[0]+', '+pos[1]);
		}
		
		this.hide = function ()
		{
			hideBox('addReminderBox');
		}
		
		this.setDateTo = function(day, month, year)
		{
			setField('rem_day', day);
			setField('rem_month', month);
			setField('rem_year', year);
		}
	}
	
	var addReminderGlobal = new addReminder();
	
	
	
	
	
	function categoryManager ()
	{
		var parent_id = 0;
		var category_id = 0;
		
		var subcat;
		
		this.failure = function (o)
		{
			alert('Failure, please try again');
		}
		
		this.getParents = function ()
		{
			var callback =
			{
				success: this.getParentsSuccess,
				failure: this.failure
			}	
	
			var cObj = YAHOO.util.Connect.asyncRequest('GET', '/core/request/lists/catManCategories.php?parent_id=0&selected_id='+this.parent_id, callback, null);			
		}
		
		this.getParentsSuccess = function (o)
		{
			bits = o.responseText.split('|||');
			//document.getElementById('catManParent').innerHTML = bits[1]; // 

			select_innerHTML(document.getElementById('catManParent'),bits[1]); //IE7 fix
			
			//document.getElementById('catManParent').value = bits[0];
		}
		
		this.setParent = function (parent_id)
		{
			if (parent_id == -1)
			{
				title = prompt('What would you like to call the category?');
				
				if (!title)
					return;
					
				string = 'title='+title;
			}
			else
			{
				if (!parent_id)
					return;
				
				this.parent_id = parent_id;
				
				string = 'parent_id='+this.parent_id;
			}
			
			var callback =
			{
				success: this.setParentSuccess,
				failure: this.failure
			}	
	
			var cObj = YAHOO.util.Connect.asyncRequest('GET', '/core/request/lists/catManCategories.php?'+string, callback, null);
		}
		
		this.setParentSuccess = function (o)
		{	
			if (!this.subcat)
				this.subcat = document.getElementById('catManSubcat');

			bits = o.responseText.split('|||');
			catMan.parent_id = bits[0];

			//this.subcat.innerHTML = bits[1];
			
			select_innerHTML(this.subcat,bits[1]); //IE7 fix
			
			this.subcat.disabled = false;
			
			catMan.getParents();
		}
		
		this.addCategorySuccess = function (o)
		{
			if (!this.palette)
				this.palette = document.getElementById('catManPalette');
				
			this.palette.innerHTML += o.responseText;
		}
		
		this.addCategory = function (category_id)
		{
			if (category_id == -1)
			{
				var title = prompt('What would you like to call the subcategory?');
				
				if (!title)
					return;

				string = 'parent_id='+this.parent_id+'&title='+title;
			}
			else
			{
				if (document.getElementById('catBox_'+category_id))
					return;
					
				string = 'category_id='+category_id;
			}
			
			var callback =
			{
				success: this.addCategorySuccess,
				failure: this.failure
			}	
	
			var cObj = YAHOO.util.Connect.asyncRequest('GET', '/core/request/lists/catManCategory.php?'+string, callback, null);
		}
	}
	
	
	
 function select_innerHTML(objeto,innerHTML){

    objeto.innerHTML = ""
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.toLowerCase().indexOf("<option")<0){
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
 	 var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ //IE
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   //getting attributes
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }
   //getting styles
   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }
   //value and text
   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML
   //IE
   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}
	