// JavaScript Document

/* Generic MM functions */

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
	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_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
/* Start of hosting table row toggle scripts */
function showHideAll(hideOrShow) {
	var tbodies = $("hostingGrid").getElementsByTagName("tbody");
	var i;
	for (i=0; i<tbodies.length; i++) {
		if (tbodies[i].id.match(/^row(.+)Data$/)) {
			var type = tbodies[i].id.replace(/^row(.+)Data$/, "$1");
			(hideOrShow=='show')?hostingon(type):hostingoff(type);
		}
	}
	(hideOrShow=='show')?hideOrShow='hide':hideOrShow='show';
	Element.update("showHide", "<a href='#' onClick='return showHideAll(\""+hideOrShow+"\");'>"+hideOrShow+" all</a>");
	return false;
}

function hostingtoggle(type) {
	moo_e_hostingtoggle = new fx.Height("row" + type + "Data");
	//moo_e_hostingtoggle.toggle();
	Element.toggle("row" + type + "Data");
	updatearrow(type);
	return false;
}
function hostingoff(type) {
	Element.hide("row" + type + "Data");
	updatearrow(type);
	return false;
}
function hostingon(type) {
	Element.show("row" + type + "Data");
	updatearrow(type);
	return false;
}
function updatearrow(type) {
	if (Element.visible("row" + type + "Data")) {
		MM_swapImage('arrow' + type,'','/images/arrow_down.gif',0)
	}
	else {
		MM_swapImage('arrow' + type,'','/images/arrow.gif',0)
	}
}
/* End of hosting table row toggle scripts */


/* Start of temp script which handles nav highlights, to be replaced server side */
function setActiveNav(whichOnes){
	navList = whichOnes.split(",")
	for(i=0; i<navList.length; i++){
		var navItem = $(navList[i]);
		Element.addClassName(navItem, 'active');
	}
}
/* end of temp script which handles nav highlights, to be replaced server side */
/* Start of replace en/fr in URL and return same page - switch lang */
function switchLang(){
	curURL = document.URL+'';
	if(curURL.match(/\/en\//)){
		newURL = curURL.replace(/\/en\//, '/fr/');
		document.location = newURL;
	}else{	
		newURL = curURL.replace(/\/fr\//, '/en/');
		document.location = newURL;
	}	
}
/* End of replace en/fr in URL and return same page - switch lang */

/* start of tv membershipGrid highlight function */

function highLightPackage(which){
	  packages = new Array('bronze','silver','gold');
	  x=0
	  for(x=0; x<packages.length; x++){
		$(packages[x]+'Tab').className =''
		$(packages[x]+'Col').className =''
	  }
	  if(which == 'bronze'){	  	
		$('bronzeTab').className ='bronze'
		$('bronzeCol').className ='bronze'
	  }else if(which == 'silver'){	  	
		$('bronzeCol').className ='silver'	  	
		$('silverTab').className ='silver'
		$('silverCol').className ='silver'
	  }else if(which == 'gold'){	  	
		$('bronzeCol').className ='gold'
		$('silverCol').className ='gold'	  	
		$('goldTab').className ='gold'
		$('goldCol').className ='gold'
	  }
  }

/* end of tv membershipGrid highlight function */
 
/* This function was added to pop new site into new window from tv membershipGrid */

function outLink(linkURL) {
	URL = "http://"+linkURL;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=1,resizable=1');");
}

/* This function was added to pop new site into new window from tv membershipGrid */

/* These functions handle page redirection for homepage, to be replaced server side */
function setCookie (newCookie){
	var futdate = new Date()		//Get the current time and date
	var expdate = futdate.getTime()  //Get the milliseconds since Jan 1, 1970
	expdate +=  1000*60*60*24*365  //expires in 10 minutes = 1000*60*10  //expires in 1 year = 1000*60*60*24*365
	futdate.setTime(expdate)
	newCookie += "; path=/;"	//Set the new cookie values up
	newCookie += " expires=" + futdate.toGMTString()
	window.document.cookie=newCookie //Write the cookie
}
function getCookie ( cookie_name ){
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}
function homepageRedir(){
	if(getCookie ("language")=='en'){
		document.location="http://new.www.look.ca/en/home.html"
	}else if(getCookie ("language")=='fr'){
		document.location="http://new.www.look.ca/fr/home.html"
	}
}
function tvPageRedir(){
	if(getCookie ("tvRegion")=='Ont'){
		document.location="/tv/tv_ontario.html"
	}else if(getCookie ("tvRegion")=='Ott'){
		document.location="/tv/tv_ottawa.html"
	}else if(getCookie ("tvRegion")=='Qc'){
		document.location="/tv/tv_quebec.html"
	}else if(getCookie ("language")=='en'){
		document.location="/tv/tv_ontario.html"
	}else if(getCookie ("language")=='fr'){
		document.location="/tv/tv_quebec.html"
	}else{
		document.location="/tv/tv_ontario.html"
	}
}
function tvNavActive(){
	if(getCookie ("language")=='en'){
		setRegion = 'regionOnt'
	}else if(getCookie ("language")=='fr'){
		setRegion = 'regionQc'
	}else{
		setRegion = 'regionOnt'
	}
	if(getCookie ("tvRegion")!=null){
		setRegion = 'region'+getCookie ("tvRegion")
	}
	return setRegion
}
/* These functions handle page redirection for homepage, to be replaced server side */
function slideOpen(which){
moo_e_existingcustomer = new fx.Height(which);
moo_e_existingcustomer.toggle();
}
//the main function, call to the effect object
	function FaqFold(){		
			var stretchers = document.getElementsByClassName('answer'); //div that stretches
			var toggles = document.getElementsByClassName('faqQuestion'); //h3s where I click on
			//accordion effect
			var myAccordion = new fx.Accordion(
				toggles, stretchers, {opacity: true, duration: 400}
			);
	
			//hash function
			function checkHash(){
				var found = false;
				toggles.each(function(h3, i){
					if (window.location.href.indexOf(h3.title) > 0) {
						myAccordion.showThisHideOpen(stretchers[i]);
						found = true;
					}
				});
				return found;
			}
			
			//if (!checkHash()) myAccordion.showThisHideOpen(stretchers[0]);
		}
		
/*
http://new.www.look.ca/lookup.cgi
accepts parameters "phone", "postalcode" or both
outputs JSON
"success":1
"availability":
	"residential":
		"wireless_uf2":1
		"dsl":1
		"dsl_maxspeed":5
		"wireless_uf1":1
		"wireless_tv":1
		"wireless_docsis":0
	"business":
		"wireless_uf2":1
		"dsl":1
		"dsl_maxspeed":5
		"wireless_uf1":1
		"wireless_tv":1
		"wireless_docsis":0
"error":null

*/
function stretchy(thisDiv){
moo_e_existingcustomer = new fx.Height(thisDiv);
moo_e_existingcustomer.toggle();
}
function resetResults(){
	resultList = $('lookUpResults').getElementsByTagName('div')
	for(i=0;i<resultList.length;i++){
		if(Element.hasClassName(resultList[i], 'showExtraInfo')){
			//alert('works')
			Element.removeClassName(resultList[i], 'showExtraInfo')
			//$(resultList[i]).style.display!='none'
		}else if($(resultList[i]).style.height!='0px'){
			//stretchy(resultList[i])
			$(resultList[i]).style.height=0
			}
			//alert(resultList[i])
	}
	return true
}
function lookupResults(results){
	var showResult =''
	var showExtraResults =''
	reply = eval("(" + results.responseText + ")");
	if(reply.error=='No valid input'){
		showResult='majorError'
	}else if(reply.availability.residential.wireless_tv=='1'){
		if(reply.availability.residential.dsl=='1'){
			showResult='tvrdsl'
		}else if(reply.availability.business.dsl=='1'){
			showResult='tvbdsl'
		}else{
			showResult='tv'
			if(document.lookupForm.phone.value==''){
				showExtraResults = 'phoneTV'
			}
		}
	}else if(reply.availability.residential.dsl=='1'){
			showResult='rdsl'
			if(document.lookupForm.postalcode.value==''){
				showExtraResults = 'postalrdsl'
			}
	}else if(reply.availability.business.dsl=='1'){
			showResult='bdsl'
			if(document.lookupForm.postalcode.value==''){
				showExtraResults = 'postalbdsl'
			}
	}else{
		showResult='nothing'
			if(document.lookupForm.phone.value==''){
				showExtraResults = 'phoneNothing'
			}
			if(document.lookupForm.postalcode.value==''){
				showExtraResults = 'postalNothing'
			}
	}	
	resetResults();
	if(showExtraResults){Element.addClassName(showExtraResults, 'showExtraInfo')}
	stretchy(showResult)
}
function lookup(){
	resetResults()
	postal = document.lookupForm.postalcode.value
	phone = document.lookupForm.phone.value
	$('looking').style.height='40px'
	new Ajax.Request(
		"/lookup.cgi", {
			method: 'GET',
			parameters: ("postalcode=" + postal + "&phone=" + phone),
			onSuccess: function (xmlhttp, json) {
				lookupResults(xmlhttp);
			}
		}
	);
}