


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer. " + ver
    else
      msg = "You should upgrade your copy of Internet Explorer. " + ver;
  }
  alert( msg );
}






function SetBestSavings(savings) {
  obj = document.getElementById('bestsavings');
  //savingsFont
  //bestsavings
  if (obj) {
		obj.innerHTML=savings;
	}
}










function ShowObj(aname) {
  obj = document.getElementById(aname);
  if (obj) {
    obj.style.visibility = 'visible';
    obj.style.display = '';
  }
}


function DisableObj(aname,bolEnable) {
  obj = document.getElementById(aname);
  if (obj) {
    obj.disabled = bolEnable;
  }
}


function HideObj(aname) {
  obj = document.getElementById(aname);
  
  if (obj) {
    obj.style.visibility = 'hidden';
    obj.style.display = 'none';
  } //else {alert(aname);}
  
}



function ChangeMoveIn(postcode) {
  var bShowRetailers;
  var bShowUsageHouseHold;
  var bShowUsageSpend;

  objMovein = document.getElementById('movein');
  objEstimate = document.getElementById('estimateusage');

  if ((objMovein) && (objEstimate)) {

    if (objMovein.value == 'no')
    {
        //if (objEstimate.value == 'spend')
        //    bShowRetailers = false;
        //else
            bShowRetailers = true;
    }
    else
      bShowRetailers = (objEstimate.value == 'no');;


    bShowUsageSpend = (objEstimate.value == 'spend');
    bShowUsageHouseHold = (objEstimate.value == 'yes');


    if (objEstimate.value == 'no')
        {ShowObj('usage01');}
    else
        {HideObj('usage01');}


    if (bShowRetailers)
       {ShowObj('addinfo02');}
    else
       {HideObj('addinfo02');}


    if (bShowUsageHouseHold)
       ShowObj('usage02')
    else
       HideObj('usage02');

    if (bShowUsageSpend)
       ShowObj('usage022')
    else
       HideObj('usage022');


    ChangeFuel(postcode);

  } //((objMovein) && (objEstimate)) {

  HideObj('hideit');
}




function ChangeFuel(postcode) {
	movein = document.getElementById('movein').value;
	estimate = document.getElementById('estimateusage').value;
	fuel = document.getElementById('packageid').value;
	
	
	function HideGroup(grpname) {
		HideObj(grpname+'span00');
      HideObj(grpname+'span00a');
		HideObj(grpname+'span01');
		HideObj(grpname+'span02');
      HideObj(grpname+'span02a');
		HideObj(grpname+'span03');
		HideObj(grpname+'span03s0');
		HideObj(grpname+'span03s');
		HideObj(grpname+'span04');
		HideObj(grpname+'span05');
	}

	function ShowGroup(grpname) {
		ShowObj(grpname+'span00');
      ShowObj(grpname+'span00a');
		ShowObj(grpname+'span01');
		ShowObj(grpname+'span02');
      ShowObj(grpname+'span02a');
		ShowObj(grpname+'span03');
    
		if ((postcode) && (postcode.substr(0,1) == '2')) {
			ShowObj(grpname+'span03s0');
			ShowObj(grpname+'span03s');
		}
    
		ShowObj(grpname+'span04');
		ShowObj(grpname+'span05');
	}
    

	if (estimate == 'yes') {
		ShowObj('usage02');//estimate
		HideObj('usage01');//recent bill

	}else if (estimate == 'no') {				
		HideObj('usage02');//estimate
		ShowObj('usage01');	//recent bill
	}else if (estimate == 'spend') {
		HideObj('usage02'); //estimate		
		HideObj('usage01'); //recent bill
		//HideObj('addinfo02'); //supplier
      ShowObj('usage022');	//recent bill

	}

  //fuel
  //0: select
  //1: elec
  //2: gas
  //3: dual
  ShowGroup('gas');
  ShowGroup('elec');  
  if (fuel==1) HideGroup('gas'); 
  if (fuel==2) HideGroup('elec');  
   		  

}





//---------------------------------------------------------------
// StrToDate()
//---------------------------------------------------------------
function StrToDate(adate) {

  var dayfield = adate.split("/")[0]
  var monthfield = adate.split("/")[1]
  var yearfield = adate.split("/")[2]
  
  return new Date(yearfield, monthfield-1, dayfield);
}

//---------------------------------------------------------------
// addWorkingDays()
//---------------------------------------------------------------
function addWorkingDays(myDate,days) { // adate = starting date, days = no. working days to add.

//  var monthfield = adate.split("/")[0]
//  var dayfield = adate.split("/")[1]
//  var yearfield = adate.split("/")[2]
//  var myDate = new Date(yearfield, monthfield-1, dayfield)

  var temp_date = new Date();
  var i = 0;
  var days_to_add = 0;

  while (i < (days)) {
    temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));

    //0 = Sunday, 6 = Saturday, if the date not equals a weekend day then increase by 1
    if ((temp_date.getDay() != 0) && (temp_date.getDay() != 6)) {
      i+=1;
    }

    days_to_add += 1;
  }

  return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
}

//---------------------------------------------------------------
// CheckDate
//---------------------------------------------------------------
function CheckDate(adate) {
  var validformat = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
  var returnval = true;
  
  if (adate == '') {
    return true;
  }
  
  if (!validformat.test(adate)) {
    returnval = false;
//    alert("Invalid Date Format. Please correct and submit again.")
  }
  else { //Detailed check for valid date ranges
    var dayfield=adate.split("/")[0]
    var monthfield=adate.split("/")[1]
    var yearfield=adate.split("/")[2]
    var dayobj = new Date(yearfield, monthfield-1, dayfield)

    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) {
      returnval = false;
//      alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
    }
    else {
      returnval=true;
    }
  }

//  if (returnval==false) input.select()
  return returnval;
}

//---------------------------------------------------------------
// PopupURL()
//---------------------------------------------------------------
function PopupURL(url,small) {
  if (small) {
    size = 'width=500,height=460,';
  }
  else {
    size = 'width=1000,height=700,';
  }
  
  window.open( url,
   	       'popup', size+'status=no,resizable=no,top=100,left=100,dependent=yes,alwaysRaised=yes'
	);
}



//---------------------------------------------------------------
// PopupTermsConditions()
//---------------------------------------------------------------
function PopupTermsConditions() {
  
  window.open('/terms-conditions/index.asp','popupTermsConditions', 'width=1050,height=700,status=0,resizable=0,top=100,left=100,dependent=1,alwaysRaised=1,scrollbars=1'
	);
}


//---------------------------------------------------------------
// MailThis()
//---------------------------------------------------------------
function MailThis(url) {
  if (url != '') {
    title = document.title;
  }
  else {
    url = document.location.href;
    title = document.title;
  }
  
  if (title == '') {
    title = 'Switchwise';
  }
  
  newurl = '/emailthispage.asp?title='+title+'&link='+url;
  
  window.open( newurl,
   	       'emailpage', 'width=500,height=460,status=no,resizable=no,top=100,left=100,dependent=yes,alwaysRaised=yes'
	);
}

function HideTC() {
  document.getElementById('myframe').src = '/empty.htm';
  document.getElementById('tandc').style.visibility = 'hidden';
  document.getElementById('tandc').style.display = 'none';
}
function ShowTC(url) { 
  if (!document.getElementById('myframe')) {
    alert('myframe not found!');
  }
  else {
    document.getElementById('myframe').src = url;
  }

  if (!document.getElementById('tandc')) {
    alert('tandc not found!');
  }
  else {
    document.getElementById('tandc').style.visibility = 'visible';
    document.getElementById('tandc').style.display = '';
  }

  handleScrollEvent();
}

//---------------------------------------------------------------
// ShowDetails()
//---------------------------------------------------------------
function ShowDetails(prodId,packageid) {
  url = '/step-02-detail_popup.asp?packageid='+packageid+'&id='+prodId;
  ShowTC(url);
//  document.location.href = url;  
//  window.open(url, "_blank", "height=500,width=800,left=0,top=20,scrollbars=yes");
}

//---------------------------------------------------------------
// SwitchOrShowDetails()
//---------------------------------------------------------------
function SwitchOrShowDetails(prodId,packageid) {
  
  url = '/step-03.asp?packageid='+packageid+'&pid='+prodId;
//  ShowTC(url);

	if (top.location.host=='www.switchwise.com.au') {
		url = 'https://' + top.location.host + url;}

//	if (window.location.protocol == "https") {
//		url = 'https://' + top.location.host + url;
//	}
	
		
  top.location.href = url;

  //  document.location.href = url;  
//  window.open(url, "_blank", "height=500,width=800,left=0,top=20,scrollbars=yes");


}

//---------------------------------------------------------------
// Secure()
//---------------------------------------------------------------
function Secure(url) {
}

//---------------------------------------------------------------
// SetSelectIdx()
//---------------------------------------------------------------
function SetSelectIdx(sel, value) {
  if (sel) {
	  for (cnt=0; cnt<sel.options.length; cnt++) {
	    //alert('val:' + value + '-' + sel.name + ' value[' + cnt + '] = "' + sel.options[cnt].value + '"');
/*		 
		 if ( (sel.options[cnt].value == value) && 
				(sel.selectedIndex != cnt) ) {
			sel.selectedIndex = cnt;
			break;
		 }
*/		 
		 if (sel.options[cnt].value == value) {
			sel.selectedIndex = cnt;
			break;
		 }
		 
	  }
  }
}

/* switchwise jquery scripts */
var temp, status, isMac, ieHover;

$(document).ready( function(){
	
	isMac = (navigator.appVersion.indexOf("Mac")!=-1);
	
	// content expand and colapse (step 04)
	$('#detailtab1') .toggle(function() {
		$('.content1').slideDown (100);
		 $(this).addClass('open');
	},
	function() { 
		$('.content1').slideUp (100);
		$(this).removeClass('open');
	});
	
	$('#detailtab2') .toggle(function() {
		$('.content2').slideDown (100);
		$(this).addClass('open');
	},
	function() { 
		$('.content2').slideUp (100);
		$(this).removeClass('open');
	});
	
	$('#detailtab3') .toggle(function() {
		$('.content3').slideDown (100);
		$(this).addClass('open');
	},
	function() { 
		$('.content3').slideUp (100);
		$(this).removeClass('open');
	});
	

	//inner tabs
	// tab array - advanced search | filter search & corresponding elements
	var innerTabs = new Array();
	innerTabs[0] = new Object();
	innerTabs[0].tab = 'innertab1';
	innerTabs[0].element = 'advancedTab';
	innerTabs[1] = new Object();
	innerTabs[1].tab = 'innertab2';
	innerTabs[1].element = 'filterTab';
	innerTabs[2] = new Object();
	innerTabs[2].tab = 'tab1';
	innerTabs[2].element = 'advancedTab';
	innerTabs[3] = new Object();
	innerTabs[3].tab = 'tab2';
	innerTabs[3].element = 'filterTab';

	$('.innerTabLink, .searchTab').click(function() {
		
		var currentTabId = $(this).attr("id");
		var currentOpen = $(this).hasClass('open');
		var currentTabIndex = -1;

		for (var i = 0; i < innerTabs.length; i++)
		{
			if (innerTabs[i].tab == currentTabId)
			{
				currentTabIndex = i;
				break;
			}
		}

		if (currentTabIndex == -1) return

		if (currentOpen)
		{
			// close current tab
			$('.' + innerTabs[currentTabIndex].element).slideUp (100);
			$('#' + innerTabs[currentTabIndex].tab).removeClass('open');

			// show form dropdown
			$('.questionItem select, .perPage select').show();
		}
		else
		{
			// open current tab
			// close other element first if open
			var tabClosed = false;
			var tabToClose = -1;
			for (var i = 0; i < innerTabs.length; i++)
			{
				if ($('#' + innerTabs[i].tab).hasClass('open'))
				{
					tabToClose = i;
					tabClosed = true;
					break;
				}
			}

			if (tabToClose > -1)
			{
				// close other element, open current element			
				$('.' + innerTabs[tabToClose].element).hide();
				$('#' + innerTabs[tabToClose].tab).removeClass('open');
				$('.' + innerTabs[currentTabIndex].element).show();
				$('#' + innerTabs[currentTabIndex].tab).addClass('open');
			}
			else
			{
				// open current element
				$('.' + innerTabs[currentTabIndex].element).slideDown (100);
				$('#' + innerTabs[currentTabIndex].tab).addClass('open');

				// hide form dropdown for ie 6
				$('.questionItem select, .perPage select').hide();				
			}
		}
		return false;
	});

	//column height detector	
    //column height detector	
    profH = $('.profileContent').height();	// 1
    topqH = $('.topQuestion').height();		// 3
    ftrH = $('.featuredContent').height();	// 4

    visL = profH;
    visR = topqH + ftrH;
    
    maxH = visL;
    if (visR > maxH) maxH = visR;

//do not set    $('.profileContent').height( maxH + 48 );
//do not set    $('.featuredContent').height( maxH - topqH );
	
//	if( ($('.profileContent').height() - $('.topQuestion').height()) > $('.featuredContent').height() )
//	{
//		$('.featuredContent').height( $('.profileContent').height() - $('.topQuestion').height() - 22);
//	}
//	else
//	{
//		$('.profileContent').height( $('.TopResults').height() - 86 );
//	}	
	
	//Transparent Hover
	// transparency on these hovers, doesn't work for ff on mac
	if (!isMac || !$.browser.mozilla)
	{
		$('.sizeUp, .sizeDown, .homeSuppliers a span, .backtotop, .switchsubmit, .featuredTable .logo a img, .resultsListTable .logo a img, .backList ul li a, .utilsList ul li a, .utilsListGeneric ul li a, .utilsListShort a, .utilsReceipt ul li a, .advancedBut input, .switchlgsubmit input, .modifiyDetails input, .detailssubmit, .genericButton div, .genericContent table .viewbut span, .modifysubmit25/06/2008').hover( function(){ $(this).css({'opacity': 0.7}); },	function(){ $(this).css({'opacity': 1}); });
	}	
	
	//
	
	//IE6 Dropdown Hover Script
	$('#mainNav li, .questionDial a').hover(function(){
		$(this).addClass('iehover');
	},
	function(){
		$(this).removeClass('iehover');
	});
	
		
})



