var baseURL = '';

// notice 
function hideNotice(userID, noticeID) {
	var url = baseURL + 'scripts/hide_notice.php';
	var params = 'user=' + userID + '&notice=' + noticeID;
	new Ajax.Request(url, {parameters:params, unescape:true, asynchronous:true, onComplete:fadeNotice}); 
	return false;
}

function fadeNotice() {
	$('notice_message').hide();
}
	

// form functions
//
function toggleDateRange() {
  var dateRange = $F('date_range')
	if (dateRange == '1') {
		$('start_date_span').show(); 
		$('end_date_span').show(); 
		$('start_td').addClassName('arrow');
	}
	else {
		$('start_date_span').hide(); 
		$('end_date_span').hide(); 
		$('start_td').removeClassName('arrow');
	}
}

function resetDateRange() {
	$('start_date_span').hide(); 
	$('end_date_span').hide(); 
	$('start_td').removeClassName('arrow');
}

function planSelected() {
  var plan = $F('plan');
	if (plan != '0') {
		var target = 'aligned_dd';
		var url = baseURL + 'scripts/update_aligned_with.php';
		var params = 'instance=' + plan;
		new Ajax.Updater(target, url, {parameters:params, unescape:true, asynchronous:true, onComplete:afterAlignedUpdated}); 

		var target = 'assigned_dd';
		var url = baseURL + 'scripts/update_assigned_to.php';
		var params = 'instance=' + plan;
		new Ajax.Updater(target, url, {parameters:params, unescape:true, asynchronous:true, onComplete:showAssigned}); 

		return false;
	}
	else {
		$('aligned_dt').hide(); 
		$('aligned_dd').hide(); 
		$('assigned_dt').hide(); 
		$('assigned_dd').hide(); 
		return false;
	}
}

function afterAlignedUpdated() {
  var aligned = $F('aligned[]');	
	if (aligned != null) {
		$('aligned_dt').show(); 
		$('aligned_dd').show(); 
	}
	else {
		$('aligned_dt').hide(); 
		$('aligned_dd').hide(); 
	}
}

function showAssigned() {
	$('assigned_dt').show(); 
	$('assigned_dd').show(); 
}

function alignedSelected() {
  var aligned = $F('aligned[]');
	if (aligned.length > 0) {
		$('assigned_dt').show(); 
		$('assigned_dd').show(); 
	}
	else {
		$('assigned_dt').hide(); 
		$('assigned_dd').hide(); 
	}
	
	if (aligned.length > 0 && aligned[0] != '0' && aligned[0] != '-1') {
		toggleRecurrence();
		$('recurrence_dt').show(); 
		$('recurrence_dd').show(); 
		$('recurrence_div').show();
		$('no_recurrence_div').hide();
	}
	else {
		$("recurrence_yes").checked = false;
		$("recurrence_no").checked = true;
		toggleRecurrence();
		$('no_recurrence_div').show();
		$('recurrence_dt').hide(); 
		$('recurrence_dd').hide(); 
		$('recurrence_div').hide();
	}
	return false;
}

function toggleDeadline() {
	if ($F('deadline_no') == 'no') {
		$('timeframe_div').hide();
	}
	else if ($F('deadline_yes') == 'yes') {
		$('timeframe_div').show();
	}
}

function toggleRecurrence() {

	if ($("recurrence_no").checked) {
		$('begin_dt').hide();
		$('begin_dd').hide();
		$('end_dt').hide();
		$('end_dd').hide();
		$('pattern_dt').hide();
		$('pattern_dd').hide();
		$('number_dt').hide();
		$('number_dd').hide();
		$('expires_dt').hide();
		$('expires_dd').hide();
		$('due_dt').show();
		$('due_dd').show();
		$('expiration_dt').show();
		$('expiration_dd').show();
		return false;
	}
	else if ($("recurrence_yes").checked) {
		$('due_dt').hide();
		$('due_dd').hide();
		$('expiration_dt').hide();
		$('expiration_dd').hide();
		$('begin_dt').show();
		$('begin_dd').show();
		$('end_dt').show();
		$('end_dd').show();
		$('pattern_dt').show();
		$('pattern_dd').show();
		$('number_dt').show();
		$('number_dd').show();
		$('expires_dt').show();
		$('expires_dd').show();
	 	return false;	
	}
}

function replacePatternText(text) {
	$('pattern_text').innerHTML = text;
}

function updateExpirationDate() {
	var dueDate = new Date($('due_date').value);
	var expirationDate = new Date($('expiration_date').value);
	if (dueDate > expirationDate) {
		 expirationDate = dueDate.dateAdd("m", 3);
		 var month = expirationDate.getMonth()+1;
		 if (month < 10) month = '0'+month;
		 var date = expirationDate.getDate();
		 if (date < 10) date = '0'+date;
		$('expiration_date').value = month+'/'+date+'/'+expirationDate.getFullYear();
	}
}

function statusSelected() {
  var status = $F('status');
	if (status == '2') {
		$('completed_dt').hide(); 
		$('completed_dd').hide(); 
		$('expired_dt').hide(); 
		$('expired_dd').hide(); 
		$('expiration_dt').show(); 
		$('expiration_dd').show(); 
	}
	else if (status == '3') {
		$('expiration_dt').hide(); 
		$('expiration_dd').hide(); 
		$('expired_dt').hide(); 
		$('expired_dd').hide(); 
		$('completed_dt').show(); 
		$('completed_dd').show(); 
	}
	else if (status == '4') {
		$('expiration_dt').hide(); 
		$('expiration_dd').hide(); 
		$('completed_dt').hide(); 
		$('completed_dd').hide(); 
		$('expired_dt').show(); 
		$('expired_dd').show(); 
	}
	return false;
}


// execute functions
function toggleCompleted(id) {
	if ($('execute_item_'+id).checked) $('completed_item_'+id).show();
	else $('completed_item_'+id).hide();	
}

function toggleCompletedDate(id) {
	if ($('item_expire_'+id).checked) $('item_completed_date_'+id).hide();
	else $('item_completed_date_'+id).show();	
}


// product functions
function updatePlan() {
  var product = $F('product');
	var target = 'plan_dd';
	var url = baseURL + 'scripts/update_plan.php';
	var params = 'product=' + product;
	new Ajax.Updater(target, url, {parameters:params, unescape:true, asynchronous:true}); 
	return false;
}

function updatePlanInstanceExpirationDate() {
  var product = $F('product');
	var target = 'plan_dd';
	var url = baseURL + 'scripts/update_plan_instance_expiration_date.php';
	var params = 'product=' + product;
	new Ajax.Updater(target, url, {parameters:params, unescape:true, asynchronous:true, onComplete:updateInstanceExpirationDate}); 
	return false;
}
updatePlanInstanceExpirationDate

function updateInstanceExpirationDate() {
  var plan = $F('plan');
	var target = 'expiration_dd';
	var url = baseURL + 'scripts/update_instance_expiration_date.php';
	var params = 'plan=' + plan;
	new Ajax.Updater(target, url, {parameters:params, unescape:true, asynchronous:true}); 
	return false;
}

function toggleRegisterSubmit() {
  var accept = $F('accepts_eula')
	if (accept == '1') {
		$('register').enable(); 
	}
	else {
		$('register').disable(); 
	}
}

function setUserID(text, li) {
	$('user_id').value = li.id;
}


// person 
function toggleSendWelcome() {
  var send = $F('send_welcome')
	if (send == '1') {
		$('message_dt').show(); 
		$('message_dd').show(); 
	}
	else {
		$('message_dt').hide(); 
		$('message_dd').hide(); 
	}
}

// generic functions
function setFocus(id) {	$(id).focus(); }

function checkUncheckAll(form) {
	for (var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if ((e.name != 'checkAll') && (e.type == 'checkbox')) {
			e.checked = form.checkAll.checked;
		}
	}
}

function toggleCustomize() {
	if ($('customize_tab').hasClassName('closed')) {
		$('customize_tab').removeClassName('closed');
		$('customize_tab').addClassName('open');
		$('customize_options').show();
	}
	else {
		$('customize_tab').removeClassName('open');
		$('customize_tab').addClassName('closed');
		$('customize_options').hide();
	}
	//Effect.toggle('customize_options', 'appear', { duration: 0.5 });
	return false;
}


// tinyMCE functions
function editor() {
	tinyMCE.init({
    theme: "advanced",
    skin: "etree",
		mode: "specific_textareas",
		editor_selector: "mceEditor",
		width: "780",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_path_location: "bottom",
		theme_advanced_resizing: true,
		theme_advanced_resize_horizontal: false,
		theme_advanced_buttons1: "bold,italic,underline,strikethrough,separator,bullist,numlist,indentlink,link,unlink,separator,pastetext,pasteword,separator,removeformat,cleanup,separator,charmap,separator,undo,redo",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",
		convert_urls: false,
		relative_urls: false,
		remove_script_host: false,
		force_p_newlines: true,
		force_br_newlines: false,
		convert_newlines_to_brs: false,
		remove_linebreaks: false,
		fix_list_elements: true,
		gecko_spellcheck: true,
		entities: "38,amp,60,lt,62,gt",
		plugins: "paste"
  });
}

function advancedEditor() {
	tinyMCE.init({
    theme: "advanced",
    skin: "etree",
		mode: "specific_textareas",
		editor_selector: "mceEditor",
		width: "780",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_path_location: "bottom",
		theme_advanced_resizing: true,
		theme_advanced_resize_horizontal: false,
		theme_advanced_buttons1: "bold,italic,underline,strikethrough,formatselect,separator,bullist,numlist,indentlink,link,unlink,image,separator,pastetext,pasteword,separator,removeformat,cleanup,separator,charmap,separator,undo,redo",
		theme_advanced_buttons2: "tablecontrols",
		theme_advanced_buttons3: "",
		convert_urls: false,
		relative_urls: false,
		remove_script_host: false,
		force_p_newlines: true,
		force_br_newlines: false,
		convert_newlines_to_brs: false,
		remove_linebreaks: false,
		fix_list_elements: true,
		gecko_spellcheck: true,
		entities: "38,amp,60,lt,62,gt",
		plugins: "paste,table"
  });
}


function dateAddExtention(p_Interval, p_Number){


   var thing = new String();
   
   
   //in the spirt of VB we'll make this function non-case sensitive
   //and convert the charcters for the coder.
   p_Interval = p_Interval.toLowerCase();
   
   if(isNaN(p_Number)){
   
      //Only accpets numbers 
      //throws an error so that the coder can see why he effed up   
      throw "The second parameter must be a number. \n You passed: " + p_Number;
      return false;
   }

   p_Number = new Number(p_Number);
   switch(p_Interval.toLowerCase()){
      case "yyyy": {// year
         this.setFullYear(this.getFullYear() + p_Number);
         break;
      }
      case "q": {      // quarter
         this.setMonth(this.getMonth() + (p_Number*3));
         break;
      }
      case "m": {      // month
         this.setMonth(this.getMonth() + p_Number);
         break;
      }
      case "y":      // day of year
      case "d":      // day
      case "w": {      // weekday
         this.setDate(this.getDate() + p_Number);
         break;
      }
      case "ww": {   // week of year
         this.setDate(this.getDate() + (p_Number*7));
         break;
      }
      case "h": {      // hour
         this.setHours(this.getHours() + p_Number);
         break;
      }
      case "n": {      // minute
         this.setMinutes(this.getMinutes() + p_Number);
         break;
      }
      case "s": {      // second
         this.setSeconds(this.getSeconds() + p_Number);
         break;
      }
      case "ms": {      // second
         this.setMilliseconds(this.getMilliseconds() + p_Number);
         break;
      }
      default: {
      
         //throws an error so that the coder can see why he effed up and
         //a list of elegible letters.
         throw   "The first parameter must be a string from this list: \n" +
               "yyyy, q, m, y, d, w, ww, h, n, s, or ms.  You passed: " + p_Interval;
         return false;
      }
   }
   return this;
}
Date.prototype.dateAdd = dateAddExtention;
