/* function show_me(Provider Name)
   - Used for : Showing the appropriate tabs and information on user clicks.
   
   - Logic : Hide those tabs that are not clicked and display those tabs that are clicked.
             It shows active tag images and flips images that are not active.

*/
function show_me(me)
{
  // This logic controls the button display.
  $$('.tab_button').each(function(button)
  {
    var activeClassName = me+"active";
    var inactiveClassName = me+"inactive";
    
    if (button.down().className == activeClassName)
    {   
      button.childElements().last().down().style.display="none";
      button.childElements().first().down().style.display="block";
    }
    else{
      //alert(button.down().className);
      button.childElements().last().down().style.display="block";
      button.childElements().first().down().style.display="none";
    }
    var y = 0;
    
  }
  );
  //This logic controls the content display.
  
  $$('.tab_content_text').each(function(content){
  
  var activeContent = me+"Content";
  
  var x = 0;
  
  if (content.childElements().first().className == activeContent)
  {
    content.ancestors().first().style.display="block";
  }
  else{
    content.ancestors().first().style.display="none";   
  }
  
    
  }
  );  
}//End of function show_me(me)

function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}

/* function chkNotNull - Used directly as given in the form provided by FOX
   This function is used to validate the phone number phone entry that the user does.
*/
function chkNotNull(objItem, strType, strText)
  {
     if (strType.toUpperCase() == "TEXT")
     {
      if ((objItem.value == null) || (objItem.value == ""))
      {
       $(objItem).up('form').down('.synctree_error').update(strText);
        //alert(strText);
        objItem.focus();
        objItem.select();
        return false;
      }
     }
 
     return true;
}
/*  function chkInteger - Used directly for form validation provided by FOX    
    Check Integer
    purpose  :  Determine if integer value (non-negative)
    
    arguments:  objItem , errorMessage
    
    returns  :  boolean  True if meets validation criteria, false otherwise
*/
function chkInteger (objItem, errorMessage)
    {
        for (var i=0; i < (objItem.value).length; i++)
        {
            var ch = (objItem.value).substring(i, i+1);
            if (!(ch >= "0" && ch <= "9"))
            {
                 $(objItem).up('form').down('.synctree_error').update(errorMessage);
     
                 //objItem.focus();
                 //objItem.select();
                 return false;
            }
        }
        return true;
}
/*  function chkLength - Used directly for form validation provided by FOX */
function chkLength (objItem, objLength, strText)
    {
       if (objItem.value.length != objLength
           && objItem.value.length != 0)
       {
         $(objItem).up('form').down('.synctree_error').update(strText);
         //objItem.focus();
         //objItem.select();
         return false;
       }
       return true;
}

/*  function frmCheck - Used directly for form validation provided by FOX */
function frmCheck(objForm){
  $('successUrlId').value=window.location.href;
  $('errorUrlId').value=window.location.href;
  if(!objForm.terms.checked){ $(objForm.phone).up('form').down('.synctree_error').update("You must agree to Terms and Conditions"); return false; }
  if (!chkNotNull(objForm.phone, 'TEXT', 'Please specify your phone number')) return false;
  if (!chkInteger(objForm.phone, 'Use only digits from 0-9')) return false;
  if (!chkLength(objForm.phone, '10', 'Your phone number must be 10 digits'))  return false;
  //alert("Select Carrier checking");
  if (objForm.carrier.value == ""){$(objForm.phone).up('form').down('.synctree_error').update("Please select a carrier"); return false;}
  //alert("Carrier check passed");
  //alert("Your number has been received");
  return true;
}

// This function is used to validate email address in the FAQ page.
function checkEmail(objForm){
	// check for @ , . and that @ comes before .
	// check for no email address entered.
	
	var at = "@";
	var dot = ".";
	
	// checking if the user forgot to enter the email address
	if(( $('email').value == null )||( $('email').value == ""))
	{
	  $('emailError').update("Please enter an E-mail Address");
      return false;
	}
	
	// checking for the presense of @ in the email address
	
	if ( ( $('email').value.indexOf(at) == -1 )||( $('email').value.indexOf(dot) == -1))
	{
		$('emailError').update("Invalid E-mail Address");
		return false;
	}
	return true;
	
}

function whenStateChanges(obj) {
  if     (obj.newstate == "COMPLETED") {
    switchToHTMLReplayScreen();
    playing = false;

  } else if(obj.newstate == "PLAYING") {
    playing = true;

  } else if(obj.newstate == "PAUSED") {
    playing = false;
  }

  $$('.controls').each(function(controls) {
    var play = controls.down('.play');
    var pause = controls.down('.pause');

    if(playing) {
      play.hide();
      pause.show();
    } else {
      play.show();
      pause.hide();
    }
  });
}

function whenTimeChanges(obj) {
  currentTime = obj.position;
}

function whenMuted(obj) {
  muted = obj.state;

  $$('.controls').each(function(controls) {
    var mute = controls.down('.mute');
    var unmute = controls.down('.unmute');

    if(muted) {
      mute.hide();
      unmute.show();
    } else {
      mute.show();
      unmute.hide();
    }
  });
}

function switchToHTMLReplayScreen() {
  Try.these( 
    function() {
      $('html_replay_screen').show();
      $('player_container').hide();
    },
    function() {
      player.sendEvent("SEEK", 0);
      player.sendEvent("PLAY", false);
    },
    function() {}
  );

  showingPlayer = false;
}

function replayVideoFromBeginning() {
  if($('html_replay_screen')) $('html_replay_screen').hide();
  if($('player_container'))   $('player_container').show();

  setTimeout("player.sendEvent('PLAY', true)", 100);
  showingPlayer = true;
}

function nextVideo() {
  for (var index = 0; index < timePoints.length; ++index) {
    var timePoint = timePoints[index];
    if(currentTime <= timePoint && showingPlayer) {
      player.sendEvent("SEEK", timePoint);
      return;
    }
  }
}

function previousVideo() {
  for (var index = timePoints.length; index >= 0; --index) {
    var timePoint = timePoints[index];
    if(currentTime >= timePoint) {
      if(showingPlayer) player.sendEvent("SEEK", timePoints[index == 0 ? 0 : index-1]);
      else replayVideoFromBeginning();
      return;
    }
  }
}

function playerReady(obj) {
  var id = obj['id'];
  player = document.getElementsByName(id)[0];
  player.addModelListener("STATE","whenStateChanges");
  player.addModelListener("TIME", "whenTimeChanges");
  player.addControllerListener("MUTE", "whenMuted");
}; 

function playPause() {
  Try.these( 
    function() { player.sendEvent('PLAY', !playing); },
    function() { replayVideoFromBeginning(); }
  );
}

function muteUnmute() {
  player.sendEvent('MUTE', !muted);
}


// CSS Browser Selector   v0.2.5
// Documentation:         http://rafael.adm.br/css_browser_selector
// License:               http://creativecommons.org/licenses/by/2.5/
// Author:                Rafael Lima (http://rafael.adm.br)
// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
var css_browser_selector = function() {
  var 
    ua=navigator.userAgent.toLowerCase(),
    is=function(t){ return ua.indexOf(t) != -1; },
    h=document.getElementsByTagName('html')[0],
    b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
    os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
  var c=b+os+' js';
  h.className += h.className?' '+c:c;
}();



/* toggle_image(ImageID, Total Number of Images to be toggled)
   Used to toggle images on the accordion : When one clicks accordion,
   The image with an arrow filps to point down, on the accordion which is clicked.
   This function achieves that flipping and also reverting the flips of earlier clicks.

   Logic : Takes in the image to be flipped, flips the image using prototype functions.
   Takes all other images present on the accordion and flips it back to off.
  
   The images are named suched that onicon image stands for the downward pointing image,
   and the officon image points to rightwards pointing image.
*/

function toggle_image(img_id,total)
{
  var imageUrl = $(img_id).src;
  //Replacing the off in the url with on
  var replacedUrl = imageUrl.replace(/(.*)officon(.*)/,"$1"+"onicon"+"$2");
  $(img_id).src=replacedUrl;

  for (i=1; i<=total ; i++)
  {  
    var prefix ="acc_";
    var currImageId = prefix+i;
    if (currImageId != img_id)
    {
      var flipUrl = $(currImageId).src.replace(/(.*)onicon(.*)/,"$1"+"officon"+"$2");
      $(currImageId).src = flipUrl;
    }
  
  }

  return false;
}
