jQuery(document).ready(function($){
  $("#phone_right").tooltip({
    track: true,
    delay: 0,
    showURL: false,
    opacity: 0.85
  });
  
    
  // Image hover stuff
  // we setup our cool mouse overs here.
  $('img#signup_button').hover(function() {
  	$(this).attr("src","/images/signup_button_hover.png");
  		}, function() {
  	$(this).attr("src","/images/signup_button.png");
  });
  
  $('img#login_button').hover(function() {
  	$(this).attr("src","/images/login_button_hover.png");
  		}, function() {
  	$(this).attr("src","/images/login_button.png");
  });

  
});
  

function swapImage(e, src){
  get_id(e).src = src;
}

function swapBkgImage(e, src){
  //alert_debug(get_id(e));
  get_id(e).style.backgroundImage = 'url('+src+')';
  //alert("image swapped :"+get_id(e).style.backgroundImage+":");
}

function swapFont(e, colour){
  get_id(e).style.color = colour;
}

function alert_debug(obj)
{
  // debugging function
  
  var str = "";
  for (var prop in obj) {
    str = str + prop + " = " + obj[prop] + ";";
  }
  //if(get_id('debug')){
    //get_id('debug').innerHTML = str;
  //}else{
    alert("Debug: " + str);
  //}
}

function my_hide(el){
  el.style.display = "none";
}

function my_show(el){
  el.style.display = "block";
}

function get_id(str){
  return document.getElementById(str);
}

function show_option1(){
  my_hide(get_id('option2'));
  my_hide(get_id('option3'));
  my_hide(get_id('option4'));
  my_show(get_id('option1'));
}

function show_option2(){
  my_hide(get_id('option1'));
  my_hide(get_id('option3'));
  my_hide(get_id('option4'));
  my_show(get_id('option2'));
}

function show_option3(){
  my_hide(get_id('option1'));
  my_hide(get_id('option2'));
  my_hide(get_id('option4'));
  my_show(get_id('option3'));
}

function show_option4(){
  my_hide(get_id('option1'));
  my_hide(get_id('option2'));
  my_hide(get_id('option3'));
  my_show(get_id('option4'));
}



function show_answering_service_form()
{
  try {
    my_show(get_id("answering_service_form"));
    my_hide(get_id("faxing_form"));
    my_hide(get_id("simple_form"));
  }
  catch (e)
  {
    //alert('RJS error:\n\n' + e.toString()); alert('my_show(\"new_form\");\nny_hide(\"churn_form\");');
    throw e
  }
}

function show_simple_form(){
  try {
    my_show(get_id("simple_form"));
    my_hide(get_id("answering_service_form"));
    my_hide(get_id("faxing_form"));
  }
  catch (e)
  {
    //alert('RJS error:\n\n' + e.toString()); alert('my_show(\"new_form\");\nny_hide(\"churn_form\");');
    throw e
  }
}

function show_faxing_form()
{
  try {
    my_show(get_id("faxing_form"));
    my_hide(get_id("answering_service_form"));
    my_hide(get_id("simple_form"));
  }
  catch (e)
  {
    //alert('RJS error:\n\n' + e.toString()); alert('my_show(\"new_form\");\nny_hide(\"churn_form\");');
    throw e
  }
}


function select_answer_point_type(type, value){

  document.getElementById('answer_point_type').value = type

  if( type == 'national'){
    my_show(get_id("national_answer_point"));
    my_hide(get_id("mobile_answer_point"));
    my_hide(get_id("international_answer_point"));
    if(value != '' && value != null){
      //alert(value);
      document.getElementById('area_code').value = value.substring(0,2);
      document.getElementById('inbound_numbers_national_answer_point').value = value.substring(2,10);
    }
    if(value == null){
      document.getElementById('inbound_numbers_national_answer_point').value = '';
    }
  }
  else if( type == 'mobile'){
    my_hide(get_id("national_answer_point"));
    my_show(get_id("mobile_answer_point"));
    my_hide(get_id("international_answer_point"));

    if(value != ''){
      document.getElementById('inbound_numbers_mobile_answer_point').value = value;
    }
    if(value == null){
      document.getElementById('inbound_numbers_mobile_answer_point').value = '04';
    }
  }
  else if( type == 'international'){
    my_hide(get_id("national_answer_point"));
    my_hide(get_id("mobile_answer_point"));
    my_show(get_id("international_answer_point"));
    if(value != ''){
      document.getElementById('inbound_numbers_int_answer_point').value = value;
    }
    if(value == null){
      document.getElementById('inbound_numbers_int_answer_point').value = '0011';
    }
  }
  else {
    my_hide(get_id("national_answer_point"));
    my_hide(get_id("mobile_answer_point"));
    my_hide(get_id("international_answer_point"));
  }

}


function select_answer_point_type_lo(type, value){
  //alert(type+":"+value);
  document.getElementById('answer_point_type_lo').value = type

  if( type == 'national'){
    my_show(get_id("national_answer_point_lo"));
    my_hide(get_id("mobile_answer_point_lo"));
    my_hide(get_id("international_answer_point_lo"));
    if(value != '' && value != null){
      // break it apart
      document.getElementById('area_code_la').value = value.substring(0,2);
      document.getElementById('inbound_numbers_national_answer_point_la').value = value.substring(2,10);
    }
    if(value == null){
      document.getElementById('inbound_numbers_national_answer_point_la').value = '';
    }
  }
  else if( type == 'mobile'){
    my_hide(get_id("national_answer_point_lo"));
    my_show(get_id("mobile_answer_point_lo"));
    my_hide(get_id("international_answer_point_lo"));
    if(value != ''){
      document.getElementById('inbound_numbers_mobile_answer_point_la').value = value;
    }
    if(value == null){
      document.getElementById('inbound_numbers_mobile_answer_point_la').value = '04';
    }
  }
  else if( type == 'international'){
    my_hide(get_id("national_answer_point_lo"));
    my_hide(get_id("mobile_answer_point_lo"));
    my_show(get_id("international_answer_point_lo"));
    if(value != ''){
      document.getElementById('inbound_numbers_int_answer_point_la').value = value;
    }
    if(value == null){
      document.getElementById('inbound_numbers_int_answer_point_la').value = '0011';
    }
  }
  else {
    my_hide(get_id("national_answer_point_lo"));
    my_hide(get_id("mobile_answer_point_lo"));
    my_hide(get_id("international_answer_point_lo"));
  }
}

function hide_existing_login()
{
  my_hide(get_id("existing_customer_login"));
  my_show(get_id("signup"));
}

function show_existing_login()
{
  my_show(get_id("existing_customer_login"));
  my_hide(get_id("signup"));
}


function hide_dd_form()
{
  try {
    my_show(get_id("credit_card_form"));
    my_show(get_id("signup_buttons"));
    my_hide(get_id("direct_debit_form"));
  }
  catch (e)
  {
    alert('RJS error:\n\n' + e.toString()); alert('my_show(\"credit_card_form\");\nny_hide(\"direct_debit_form\");');
    throw e
  }
}

function hide_cc_form()
{
  try {
    my_hide(get_id("credit_card_form"));
    my_show(get_id("signup_buttons"));
    my_show(get_id("direct_debit_form"));
  }
  catch (e)
  {
    alert('RJS error:\n\n' + e.toString()); alert('ny_hide(\"credit_card_form\");\nmy_show(\"direct_debit_form\");');
    throw e
  }
}



function handle_dest_num_menu(obj){
  var type = obj.options[obj.selectedIndex].value;

  if( type == 'national'){
    document.getElementById('national_answer_point').style.display = '';
    document.getElementById('mobile_answer_point').style.display = 'none';
    document.getElementById('international_answer_point').style.display = 'none';
  }
  else if( type == 'mobile'){
    document.getElementById('national_answer_point').style.display = 'none';
    document.getElementById('mobile_answer_point').style.display = '';
    document.getElementById('international_answer_point').style.display = 'none';
  }
  else if( type == 'international'){
    document.getElementById('national_answer_point').style.display = 'none';
    document.getElementById('mobile_answer_point').style.display = 'none';
    document.getElementById('international_answer_point').style.display = '';
  }
  else {
    document.getElementById('national_answer_point').style.display = 'none';
    document.getElementById('mobile_answer_point').style.display = 'none';
    document.getElementById('international_answer_point').style.display = 'none';
  }
}


function handle_dest_num_menu_la(obj){
  var type = obj.options[obj.selectedIndex].value;

  if( type == 'national'){
    document.getElementById('national_answer_point_lo').style.display = '';
    document.getElementById('mobile_answer_point_lo').style.display = 'none';
    document.getElementById('international_answer_point_lo').style.display = 'none';
  }
  else if( type == 'mobile'){
    document.getElementById('national_answer_point_lo').style.display = 'none';
    document.getElementById('mobile_answer_point_lo').style.display = '';
    document.getElementById('international_answer_point_lo').style.display = 'none';
  }
  else if( type == 'international'){
    document.getElementById('national_answer_point_lo').style.display = 'none';
    document.getElementById('mobile_answer_point_lo').style.display = 'none';
    document.getElementById('international_answer_point_lo').style.display = '';
  }
  else {
    document.getElementById('national_answer_point_lo').style.display = 'none';
    document.getElementById('mobile_answer_point_lo').style.display = 'none';
    document.getElementById('international_answer_point_lo').style.display = 'none';
  }
}



function handle_menu(obj){
  // check what value is selected
  if(obj.options[obj.selectedIndex].value == '1'){
    document.getElementById('destination_number').style.display = 'none';
    document.getElementById('ovr_wk1_destination_number').style.display = '';
    document.getElementById('ovr_wk1_start_time').style.display = '';
    document.getElementById('ovr_wk1_end_time').style.display = '';
    document.getElementById('ovr_wk2_destination_number').style.display = '';
    document.getElementById('ovr_wk2_start_time').style.display = '';
    document.getElementById('ovr_wk2_end_time').style.display = '';
    document.getElementById('ovr_ss1_destination_number').style.display = '';
    document.getElementById('ovr_ss1_start_time').style.display = '';
    document.getElementById('ovr_ss1_end_time').style.display = '';
    document.getElementById('ovr_ss2_destination_number').style.display = '';
    document.getElementById('ovr_ss2_start_time').style.display = '';
    document.getElementById('ovr_ss2_end_time').style.display = '';
    document.getElementById('time_based_heading').style.display = '';
    
  }else{
    document.getElementById('destination_number').style.display = '';
    document.getElementById('ovr_wk1_destination_number').style.display = 'none';
    document.getElementById('ovr_wk1_start_time').style.display = 'none';
    document.getElementById('ovr_wk1_end_time').style.display = 'none';
    document.getElementById('ovr_wk2_destination_number').style.display = 'none';
    document.getElementById('ovr_wk2_start_time').style.display = 'none';
    document.getElementById('ovr_wk2_end_time').style.display = 'none';
    document.getElementById('ovr_ss1_destination_number').style.display = 'none';
    document.getElementById('ovr_ss1_start_time').style.display = 'none';
    document.getElementById('ovr_ss1_end_time').style.display = 'none';
    document.getElementById('ovr_ss2_destination_number').style.display = 'none';
    document.getElementById('ovr_ss2_start_time').style.display = 'none';
    document.getElementById('ovr_ss2_end_time').style.display = 'none';
    document.getElementById('time_based_heading').style.display = 'none';

  }
}

function handle_dest_num_menu(obj){
  var type = obj.options[obj.selectedIndex].value;

  if( type == 'national'){
    document.getElementById('national_answer_point').style.display = '';
    document.getElementById('mobile_answer_point').style.display = 'none';
    document.getElementById('international_answer_point').style.display = 'none';
  }
  else if( type == 'mobile'){
    document.getElementById('national_answer_point').style.display = 'none';
    document.getElementById('mobile_answer_point').style.display = '';
    document.getElementById('international_answer_point').style.display = 'none';
  }
  else if( type == 'international'){
    document.getElementById('national_answer_point').style.display = 'none';
    document.getElementById('mobile_answer_point').style.display = 'none';
    document.getElementById('international_answer_point').style.display = '';
  }
  else {
    document.getElementById('national_answer_point').style.display = 'none';
    document.getElementById('mobile_answer_point').style.display = 'none';
    document.getElementById('international_answer_point').style.display = 'none';
  }
}



function handleWeekdayMenu(obj){
  if(obj.options[obj.selectedIndex].value == 'time'){
    document.getElementById('week_start_tr').style.display = '';
    document.getElementById('week_end_tr').style.display = '';
  }else{
    document.getElementById('week_start_tr').style.display = 'none';
    document.getElementById('week_end_tr').style.display = 'none';
  }
}


function handleSunMenu(obj){
  if(obj.options[obj.selectedIndex].value == 'time'){
    document.getElementById('sun_start_tr').style.display = '';
    document.getElementById('sun_end_tr').style.display = '';
  }else{
    document.getElementById('sun_start_tr').style.display = 'none';
    document.getElementById('sun_end_tr').style.display = 'none';
  }
}

function handleSatMenu(obj){
  if(obj.options[obj.selectedIndex].value == 'time'){
    document.getElementById('sat_start_tr').style.display = '';
    document.getElementById('sat_end_tr').style.display = '';
  }else{
    document.getElementById('sat_start_tr').style.display = 'none';
    document.getElementById('sat_end_tr').style.display = 'none';
  }
}



function handleAssisted(obj){
  if(obj.options[obj.selectedIndex].value == '1'){
    document.getElementById('platform').style.display = '';
    document.getElementById('ftp_host').style.display = '';
    document.getElementById('ftp_username').style.display = '';
    document.getElementById('ftp_password').style.display = '';
    document.getElementById('install_instructions').style.display = '';
  }else{
    document.getElementById('platform').style.display = 'none';
    document.getElementById('ftp_host').style.display = 'none';
    document.getElementById('ftp_username').style.display = 'none';
    document.getElementById('ftp_password').style.display = 'none';
    document.getElementById('install_instructions').style.display = 'none';
  }
}

function show_click_to_call(){

  var screen = document.getElementById('clicktocall');
  if (screen.style.zIndex != "90000")
  {
  screen.style.position = "fixed";
  screen.style.zIndex   = "90000";
  screen.style.top      = "120px";
  
  screen.style.left     = ((document.body.offsetWidth/2)-175) + "px";
  }
  

  var blocker = document.getElementById( "blocker" );
  blocker.style.filter = "alpha(opacity=90)";
  blocker.style.MozOpacity = ".9";
  blocker.style.opacity = ".9";
  blocker.style.width   = "100%";
  //blocker.style.height  = "100%"; // not enough ..
  
  blocker.style.height  = document.getElementById('body_table').offsetHeight + "px";
  blocker.style.overflow= "hidden";
  blocker.style.left    = "0px";
  blocker.style.top     = "0px";
  blocker.style.position= "absolute";
  blocker.style.zIndex  = "89999"; //phew, just under 9000 ;)
  blocker.style.backgroundColor = "#233552";
  
  my_show( blocker );
  my_show( screen );
}

function hide_click_to_call(){
  my_hide( document.getElementById( "blocker" ) );
  my_hide( document.getElementById( "clicktocall" ) );
}


function hide_and_remove_filter( el )
{
   el.style.visibility = "hidden";
   el.style.display    = "none";
   if(el.style.filter && el.style.removeAttribute){
     el.style.removeAttribute('filter');
   }
   
}

function ofc_ready()
{
    //alert("ofc_ready");
}

function open_flash_chart_data()
{
    //alert( "reading data" );
    return JSON.stringify(data);
}

function get_my_data()
{
    //alert( "reading data" );
    return JSON.stringify(data1);
}


function findSWF(movieName) {
  if (navigator.appName.indexOf("Microsoft")!= -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();



function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}



function load_sublayout_content(e){
  //swapImage(img_id,img_hover);
  // hide everything
  $('.response').each(function(index, elem) {
    //alert(elem);
    my_hide(elem);
  });
  // show the one
  my_show(get_id(e));
 
}

function refreshSubNav(img_id, img_hover){
  // work out who is active
  $('.response').each(function(index, elem) {
    //alert(elem);
    //my_hide(elem);
    if(elem.style.display != 'none'){
      // this is our one to have a hover on.
      var str = elem.id;
      //alert(str);
      //myregexp = new RegExp("\d+");
      var myArray = /(\d+)/.exec(str);  
      //alert(myArray);
      //mymatch = myregexp.exec(str);
      //alert(mymatch);
      my_id = "nav_q"+myArray[0];
      if(my_id == img_id){
        swapImage(img_id, img_hover);
      }
      //swapImage('','')
    }
  });
}




