


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}


function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { 
if (form.btnContinueOrder == true)
{
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
  }
}

function getEl(id) {
	// Helper to reduce typing
	return document.getElementById(id);
}

function checkPromotionCode(src, arg) {
	var i = getEl("txtPromotionCode").value.length;
	if (i != 0) {
		arg.IsValid = (i >= 10 && i <=15);
	} else {
		arg.IsValid = true;
	}
}

function checkSortCode1(src, arg) {
	var sSortCode = getEl("CheckOut:CardPaymentDetailsTemplate_SortCode1").value;	
	arg.IsValid = (sSortCode.length == 2);
}

function checkSortCode2(src, arg) {
	var sSortCode = getEl("CheckOut:CardPaymentDetailsTemplate_SortCode2").value;
	arg.IsValid = (sSortCode.length == 2);
}

function checkSortCode3(src, arg) {
	var sSortCode = getEl("CheckOut:CardPaymentDetailsTemplate_SortCode3").value;
	arg.IsValid = (sSortCode.length == 2);
}

function checkSecurityCode(src, arg) {
	var sSortCode = getEl("CheckOut:CardPaymentDetailsTemplate_SecurityCode").value;
	arg.IsValid = (sSortCode.length > 2);
}

function checkFirstNameLength(src, arg) {
	var sName = getEl("CheckOut:PersonalDetailsTemplate_FirstName").value;
	arg.IsValid = (sName.length > 1);
}

function checkLastNameLength(src, arg) {
	var sName = getEl("CheckOut:PersonalDetailsTemplate_LastName").value;
	arg.IsValid = (sName.length > 1);
}

function checkEmails(src, arg) {
	if (getEl("txtEmail").value.length != 0 && getEl("txtEmailConfirm").value.length != 0) {
		arg.IsValid = getEl("txtEmail").value == getEl("txtEmailConfirm").value;
	} else {
		arg.IsValid = true;
	}
}

function popup(url, windowName, width, height, top, left) {
	remote = window.open(url, windowName, "resizable=yes,toolbar=0,location=0,directories=0,status=no,menubar=0,scrollbars=1,copyhistory=0,width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + ",screeny=" + top + ",screenx=" + left);
}

function checkTandC(src, arg)
{
	arg.IsValid = getEl("chkTerms").checked;
}

function isNumeric(str) {
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len) {
    ch= str.charAt(p);
    if ('0'<=ch && ch<='9')
      p++;
    else
      ok= false;
  }
  return ok;
}

function checkDOB(src, arg) {
	var day = getEl("Checkout:PersonalDetailsTemplate_DOB_Day").value;
	var month = getEl("Checkout:PersonalDetailsTemplate_DOB_Month").value;
	var year = getEl("Checkout:PersonalDetailsTemplate_DOB_Year").value;
	
	if( (day.length < 1 || day.length > 2 || ! isNumeric(day) ) ||
		(month.length < 1 || month.length > 2 || ! isNumeric(month) ) ||
		(year.length < 1 || year.length > 2 || ! isNumeric(year) ) ) {
			arg.IsValid = false;
	}
	else {
		arg.IsValid = true;
	}		
}

function checkTimeWithEmployer(src, arg) {
	var months = getEl("Checkout:PersonalDetailsTemplate_MonthsWithEmployer").value;
	var years = getEl("Checkout:PersonalDetailsTemplate_YearsWithEmployer").value;
	
	if( (months.length < 1 || months.length > 2 || ! isNumeric(months) ) ||
		(years.length < 1 || years.length > 2 || ! isNumeric(years) ) ) {
			arg.IsValid = false;
	}
	else {
		arg.IsValid = true;
	}		
}

function changeImage(layer,imgName,imgObj) {

	if (document.layers && layer!=null) { 
		eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src');
	}
	else { 
		document.images[imgName].src = eval(imgObj+".src"); 
		
	}
}

function changeImage(imgName, imgObj) {
    document.images[imgName].src = eval(imgObj + ".src");
}

function checkInsuranceTermsAndConditions(src, arg) {
	arg.IsValid = document.forms[0].hid.value == 1;
}

function setChecked(checked) {
	if (checked) {
		document.forms[0].hid.value = 1; 
	} else {
		document.forms[0].hid.value = 0;
	}
}


function clickButton(e)
{
    var intKey = (window.Event) ? e.which : e.keyCode;

    if (intKey == 13)
    {
        document.getElementById('searchBar_searchButton').click();
        return false;
    }

    return true;
}

function clickButton(e, button)
{
    var intKey = (window.Event) ? e.which : e.keyCode;

    if (intKey == 13)
    {
        document.getElementById(button).click();
        return false;
    }

    return true;
}


// move the navigation

    function roll(pos_y, id){
        var move = document.getElementById(id).style.backgroundPosition="0px " + pos_y + "px" ;	
        return move;
    }


    
// expanding my account section + jquery integration
    jQuery(document).ready(function($) {
        // my account area show
        $(".btnMyAccountShow").click(function() {
            if ($("#myAccountAreaWrapper").is(":visible")) {
                $("#myAccountAreaWrapper").hide("slow");
            } else {
                $("#myAccountAreaWrapper").show("slow");
            }
        });

        // my account area hide
        $("#btnMyAccountHide").click(function() {
            $("#myAccountAreaWrapper").hide("slow");
        });

        // faqs section 
        $("#faqsList").accordion({
            header: "h3",
            active: false,
            alwaysOpen: false,
            autoHeight: false
        });

        // referAFriend section 
        $("#referAFriend_List").accordion({
            header: "h3",
            active: false,
            alwaysOpen: false,
            autoHeight: false
        });

        // referAFriend 2 section 
        $("#referAFriend_List2").accordion({
            header: "h3",
            active: false,
            alwaysOpen: false,
            autoHeight: false
        });

        // T and Cs section 
        $("#tandcsList").accordion({
            header: "h3",
            active: false,
            alwaysOpen: false,
            autoHeight: false
        });

        // rewards section 
        $("#rewardList").accordion({
            header: "div.header",
            active: false,
            alwaysOpen: false,
            autoHeight: false
        });

        // homepage tabs
        $("#homePricePlanTabs > ul").tabs();
        //$("#homePricePlanTabs > ul").tabs("select",2);
        //$('#homePricePlanTabs > ul').tabs({select:function(){alert('here');}});
        //$("#homePricePlanTabs > ul").tabs('rotate', 5000);

        // price plans tabs
        $("#pricePlanTabs > .tabs").tabs();
        $("#rewardsTabs > .tabs").tabs();
        $("#roamingTabs > .tabs").tabs();
        $("#christmasdealsTabs > .tabs").tabs();

        $("#worldTabs > ul").tabs();

        $("#perpayTabs > ul").tabs();
        $("#perpayTabs > ul").tabs("select", 1);

        $("#referafriendTabs > ul").tabs();
        $("#simpleTabs > ul").tabs();
        $("#searchTabs > ul").tabs();
        $("#simTabs > ul").tabs();
        $("#internetabs > ul").tabs();

        // new tab script placed here - used on the new paymonthly pages.

        $('.tabs a').click(function() {
            switch_tabs($(this));
        });

        switch_tabs($('.defaulttab'));

        // new jquery tab switching function
        function switch_tabs(obj) {
            $('.tab-content-inner, .tab-content-inner3').hide();
            $('ul.tabs a').removeClass("selected").addClass("unselected");
            $('ul.tabs a').removeClass("defaulttab");
            $('#' + obj.attr("rel")).show();
            obj.addClass("defaulttab").removeClass("unselected");

        }
        

        $("select#country").change(function() {
            var str = "";
            $("select#country option:selected").each(function() {
                str = $(this).parent().attr("value");
            });

            for (i = 0; i < 10; i++) {
                div = "#cost_option_" + i;
                $(div).hide();
            }

            div = "#cost_option_" + str;
            $(div).show();
        }).change();


        jQuery("#mobileAccord").accordion({
            header: "div.header",
            active: false,
            alwaysOpen: false,
            autoHeight: true
        });

        jQuery('#show_TC').toggle(function() {
            jQuery('#tcDetails').show();
        }, function() {
            jQuery('#tcDetails').hide();
        });
        jQuery('#tcDetails').hide();

        jQuery('#toggleControl1').toggle(function() {
            jQuery('#toggleControlPnl1').show();
        }, function() {
            jQuery('#toggleControlPnl1').hide();
        });
        jQuery('#toggleControlPnl1').hide();

        jQuery('#tcDetails').hide();

        jQuery('#basketView').hide();

        basketOpen = false;
        jQuery('.staticBasketWrapper').hover(
        function() {
            basketOpen = true;
            jQuery('#fullBasket').slideDown(function() { if (!basketOpen) { jQuery('#fullBasket').hide(); } });
        }
        , function() {
            basketOpen = false;
            jQuery('#fullBasket').hide();
        }
    )

        function getBaseURL() {
            var url = location.href;  // entire url including querystring - also: window.location.href;
            var baseURL = url.substring(0, url.indexOf('/', 14));


            if (baseURL.indexOf('http://localhost') != -1) {
                // Base Url for localhost
                var url = location.href;  // window.location.href;
                var pathname = location.pathname;  // window.location.pathname;
                var index1 = url.indexOf(pathname);
                var index2 = url.indexOf("/", index1 + 1);
                var baseLocalUrl = url.substr(0, index2);

                return baseLocalUrl + "/";
            }
            else {
                // Root Url for domain name
                return baseURL + "/";
            }

        }

        var jQRootURL = getBaseURL();

        $('#headerFlashContent').flash(
        {


            src: jQRootURL + 'flash/topUpBanner.swf',
            wmode: 'transparent',
            width: 546,
            menu: 'false',
            align: 'left',
            bgcolor: '#140e58',
            height: 126
        },
        { version: 8 }
    );


    });

    

function showLegal(){
    if(jQuery('#legal').css("display")=="block"){
        jQuery('#legal').css("display","none");
    }else{
        jQuery('#legal').css("display","block");
    }
}
function showLegal2(){
    if(jQuery('#legal2').css("display")=="block"){
        jQuery('#legal2').css("display","none");
    }else{
        jQuery('#legal2').css("display","block");
    }
}
function toggle(obj,obj2){
    if(jQuery(obj2).css("display")=="block"){
        jQuery(obj2).css("display","none");
    }else{
        jQuery(obj2).css("display","block");
    }
}

function rgbToHex(rgb){
      var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
      if(rgbvals!=null){
          var rval = parseInt(rgbvals[1]);
          var gval = parseInt(rgbvals[2]);
          var bval = parseInt(rgbvals[3]);
          return '#' + (
            rval.toString(16) +
            gval.toString(16) +
            bval.toString(16)
          ).toUpperCase();
      }
}

function CngClass(index) {
    var obj = null
    for (i = 1; i < 5; i++) {
        var id = "imageButton" + i;
        obj = window.document.getElementById(id);
        if (obj != null) obj.className = "ImageButton"
    }
    var selectedId = "imageButton" + index;
    obj = window.document.getElementById(selectedId);
    if (obj != null) obj.className = "SelectedImageButton";
}

jQuery(document).ready(function() {
    jQuery(document).pngFix();
}); 

/* cufon */

Cufon.replace('h1', { fontFamily: 'Helvetica Rounded LT Std' });
Cufon.replace('h2', { fontFamily: 'Helvetica Rounded LT Std' });
Cufon.replace('#mainNavigationContainer ul li .cufonChanger', { hover: true, fontFamily: 'Helvetica Rounded LT Std' });

    

