// JavaScript Document
function show_times() {
	var date = $('date_field').value;
	$('start_times').innerHTML = '<strong>Fetching available times...</strong>';
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'date='+date,
		// Handle successful response
		onSuccess: function(t) {
			$('start_times').innerHTML = t.responseText;
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('form_processing/show_times.php', opt);
}

function validate_promo() {
	var code = $('promo_code').value;
	$('promo_status').innerHTML = '<strong>Validating, please wait...</strong>';
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'code='+code,
		// Handle successful response
		onSuccess: function(t) {
			switch(t.responseText) {
				case 'invalid':
					$('promo_status').innerHTML = '<strong>Invalid Code</strong>';
					break;
				case '100':
					var promo_date = $('promo_date').value;
					var promo_start_time = $('promo_start_time').value;
					var promo_end_time = $('promo_end_time').value;
					var promo_message = escape($('promo_message').value);
					var url_string = "?date="+promo_date+"&start_time="+promo_start_time+"&end_time="+promo_end_time+"&message="+promo_message;
					$('promo_status').innerHTML = '<strong>Valid Code, your order will be free, <a href="https://www.just-between-us.net/create_chat_process_final.php'+url_string+'">click here</a> to process.</strong>';
					break;
				default:
					var percentage = parseInt(t.responseText);
					var price = $('chat_price').value;
					$('chat_price').value = price-((percentage*0.01)*price);
					$('promo_status').innerHTML = '<strong>Valid Code, please continue with your order. You will recieve '+percentage+'% off your order</strong>';
					break;
					
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('form_processing/validate_promo.php', opt);
}

function delete_chat( chat_id ) {
	
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'chat_id='+chat_id,
		// Handle successful response
		onSuccess: function(t) {
			Effect.Fade('chat_num_'+chat_id);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('delete_chat.php', opt);
}
function send_message( message ) {
	$('new_message').value = "";
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'message='+message,
		// Handle successful response
		onSuccess: function(t) {
			//alert(t.responseText);
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('send_message.php', opt);
}
var last_message_id;
function update_chat(){
	//new Insertion.Bottom('chat_dialog', 'test<br />');
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'last_message='+last_message_id,
		// Handle successful response
		onSuccess: function(t) {
			new Insertion.Bottom('chat_dialog', t.responseText);
			$('chat_dialog').scrollTop = $('chat_dialog').scrollHeight;
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('messages.php', opt);
	var delay = 1100;
	id = setTimeout("update_chat()", delay);
}

var timer_id = 0;
var timer_start = null;
function update_timer() {
	if (timer_id) {
		clearTimeout(timer_id);
		clockID  = 0;
	}
	if (!timer_start) {
		timer_start   = new Date();
	}
	var timer_date = new Date();
	var timer_diff = timer_date.getTime() - timer_start.getTime();
	timer_date.setTime(timer_diff);
	var timer_secs = timer_date.getSeconds();
	if (timer_secs < 10) {
		timer_secs = "0"+timer_secs;
	}
	var timer_mins = timer_date.getMinutes();
	if (timer_mins < 10) {
		timer_mins = "0"+timer_mins;
	}
	$('chat_timer').innerHTML = "" + timer_mins + ":" + timer_secs;   
	timer_id = setTimeout("update_timer()", 1000);
}
function start_timer() {
   timer_start = new Date();
   $('chat_timer').innerHTML = "00:00";
   timer_id  = setTimeout("update_timer()", 1000);
}
function stop_timer() {
	if(timer_id) {
		clearTimeout(timer_id);
		timer_id  = 0;
	}
	timer_start = null;
}

function reset_timer() {
	timer_start = null;
	$('chat_timer').innerHTML = "00:00";
}

function toggle_change_password() {
	var val= $('change_password').checked;
	switch (val) {
		case true:
			$('password_fields').style.display = "block";
			break;
		case false:
			$('password_fields').style.display = "none";
			break;
	}
}

// This is to delete an appointment, either 
function delete_appt( type, id ) {
	if ( ( !type ) || ( !id ) ) {
		return false;
	}
	var opt = {
		// Use POST
		method: 'post',
		// Send this lovely data
		postBody: 'type='+type+'&id='+id,
		// Handle successful response
		onSuccess: function(t) {
			switch (t.responseText) {
				case 'done':
					new Effect.Fade(type+'_'+id);
				break;
			}
		},
		// Handle 404
		on404: function(t) {
			alert('Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	new Ajax.Request('form_processing/delete_appt.php', opt);
}

function copy_to_list(from,to)
{
  fromList = eval("document.forms[1].elements['"+from+"']");
  toList = eval("document.forms[1].elements['"+to+"']");
  if (toList.options.length > 0 && toList.options[0].value == 'temp')
  {
    toList.options.length = 0;
  }
  var sel = false;
  for (i=0;i<fromList.options.length;i++)
  {
    var current = fromList.options[i];
    if (current.selected)
    {
      sel = true;
      if (current.value == 'temp')
      {
        alert ('You cannot move this text!');
        return;
      }
      txt = current.text;
      val = current.value;
      toList.options[toList.length] = new Option(txt,val);
      fromList.options[i] = null;
      i--;
    }
  }
  if (!sel) alert ('You haven\'t selected any options!');
}
function select_all()
{
  List = document.forms[1].elements['start[]'];
  if (List.length && List.options[1].value == 'temp') return;
  for (i=0;i<List.length;i++)
  {
     List.options[i].selected = true;
  }
}