//////////////////////////////////////////////////////////////////////
// add a page load event
//////////////////////////////////////////////////////////////////////
// Add it to window.onload event
//addOnLoad(function() {clearfix("clearthis");});

/*
 * Adds a function into the onload event
 * Usage: addOnLoad(functionName)
 * Or, if you need to pass arguments in the
 * function:
 * addOnLoad(function() {functionName("argument")})
*/
function addOnLoad(newFunction) {
    var currentOnLoad = window.onload;
    if (typeof window.onload != "function") {
        window.onload = newFunction;
    }
    else {
        window.onload = function() {
            currentOnLoad();
            newFunction();
        }
    }
}



//////////////////////////////////////////////////////////////////////
// assign click events to the promoClass on the HP
//////////////////////////////////////////////////////////////////////

function setupPromoEvents() { 
var sp = $$('.promo').each( function(e) {
var inc = e.getElementsByTagName("a");
e.setAttribute('link',inc[0].getAttribute('href'));
e.onclick = function() { location.href = this.getAttribute('link'); }
e.onmouseover = function() { this.className = "promoActive"; }
e.onmouseout = function() { this.className = "promo"; }
});
}

//////////////////////////////////////////////////////////////////////
// show regions on homepageizzle
//////////////////////////////////////////////////////////////////////
var rrTimerID;

function hpRegion(startRegionH,processing) {

var regionCookie = readCookie('region');
if (regionCookie) {
	var rob = document.regionForm.region;
	for (var i = 0; i < rob.length; i++) {
		if (rob[i].value == regionCookie) {
			rob.selectedIndex = i; 
		}
	}
}
	
	
if (document.getElementById('hpRegionContainer').style.display == "block") { 
document.getElementById('rbottom').style.display = "none";
if (processing) { document.getElementById('hpSelectARegion').style.backgroundPosition = "0 -50px"; }
else { document.getElementById('hpSelectARegion').style.backgroundPosition = "0 0"; }
RegionUp(startRegionH,startRegionH); }
else {
document.getElementById('rbottom').style.display = "block";
document.getElementById('hpSelectARegion').style.backgroundPosition = "0 -25px"; 
RegionDown('1',startRegionH); }
}


function RegionUp(h,startRegionH) {
clearTimeout(rrTimerID);
if (h > 1) {
h = h - 20;
if (h < 1) { h = 1; }
document.getElementById('hpRegionContainer').style.height = h;
rrTimerID = setTimeout("RegionUp('" + h + "')", 10); // do it again and again
}
else if (h == 1) { document.getElementById('hpRegionContainer').style.display = "none"; }
}



function RegionDown(h,startRegionH) {
clearTimeout(rrTimerID);
if (h == 0) { h = 1; }
document.getElementById('hpRegionContainer').style.display = "block";
h = parseFloat(h);
if (h < startRegionH) {
h = h + 20;
document.getElementById('hpRegionContainer').style.height = h;
rrTimerID = setTimeout("RegionDown('" + h + "','" + startRegionH + "')", 30); // do it again and again
}
else { document.getElementById('hpRegionContainer').style.height = startRegionH; }
}



//////////////////////////////////////////////////////////////////////
// scroll
//////////////////////////////////////////////////////////////////////
var pl3TimerID;
var times = 0;
var scrollToPos = "-190";
var sp;
var sc;
var curX;
var scc;
var max;

function pl3scroll(d) {
////////////////////////////////////////////////////////////////////////////////////
// the first time you clicky, get the total number of promo divs to know when to stop
if (!sc) {
sp = document.getElementById('promoLevel3Inside').getElementsByTagName("div");
sc = 0;
scc = 0;
for (var i = 0; i < sp.length; i++) { if (sp[i].className == "promo") { sc++; } }
}
////////////////////////////////////////////////////////////////////////////////////
if (d == "prev") { scc = scc - 1; }
else { scc++; }
if (scc < 0) { scc = 0; } 
curX = parseFloat(document.getElementById('promoLevel3Inside').style.left);
if (!curX) { curX = 0; }
max = sc / 4;
if (scc > max) { scc = max; }
//alert('scc:' + scc + ' max:' + max);

if (scc == max) { document.getElementById('FeaturesNext').className = ""; }
else {  document.getElementById('FeaturesNext').className = "active"; }
if (scc > 0) { document.getElementById('FeaturesPrev').className = "active"; }
else {  document.getElementById('FeaturesPrev').className = ""; }

pl3Loop(d);
}


function pl3Loop(d) {
clearTimeout(pl3TimerID);
stopPos = (scrollToPos * scc); // with of promo div X current next/prev number
if (curX != stopPos) {
if (d == "next") { curX = curX - 10; } // subtract 4 to go forward
else { curX = curX + 10; if (curX > 0) { curX = 0; } } // add 4 to go backwards
document.getElementById('promoLevel3Inside').style.left = curX; // set current left pos
pl3TimerID = setTimeout("pl3Loop('" + d + "')", 0); // do it again and again
}
//else { 
//	alert(stopPos); alert(curX); // says im done!
//	 }
}

//////////////////////////////////////////////////////////////////////
// cookie functions
//////////////////////////////////////////////////////////////////////
function createCookie(name,value,days,domain)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	wholecookie = name+"="+value+expires+"; path=/;";
	if (domain) { wholecookie = wholecookie + "domain=" + domain + ";"; }
	document.cookie = wholecookie;
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name,domain)
{
	createCookie(name,"",-1,domain);
}

//////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// find x and y positions of an element
//////////////////////////////////////////////////////////////////////
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
//////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// scrollPageTo(endPosition)
//////////////////////////////////////////////////////////////////////
var scrolldelay;
function scrollPageTo(end) {
		clearTimeout(scrolldelay); // clear the repeater
		var currentPos = (document.all)?document.body.scrollTop:window.pageYOffset; 
		if (currentPos > end) {
    	window.scrollBy(0,-10); // horizontal and vertical scroll increments
    	scrolldelay = setTimeout('scrollPageTo(\'' + end + '\')',10); 
		}
		//alert(end);
}



//////////////////////////////////////////////////////////////////////
// upperFirst(whattomakeuppercasefirst)
// makes things all lowercase then uppercases the first letter of
// each word
//////////////////////////////////////////////////////////////////////
function upperFirst(u){
 u = u.toLowerCase();
 u = u.split(' ');
 for (i=0;i<u.length;i++)
   u[i] = u[i].replace(/^(.)/,u[i].charAt(0).toUpperCase());
 return u.join(' ');
}
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// make me a window
//////////////////////////////////////////////////////////////////////

function makewindow(url,name,wi,hi,scroll) {  
if (!scroll) { scroll = 0; }
mywin = window.open(url,name,'width=' + wi + ',height=' + hi + ',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll); }

//////////////////////////////////////////////////////////////////////
// autotab
//////////////////////////////////////////////////////////////////////
var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
				}
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//////////////////////////////////////////////////////////////////////
// define regions
//////////////////////////////////////////////////////////////////////

var timerID; // for shutoff function
var regions = new Array(16) // 16 total counting 0
regions[0]="Cincinnati";
regions[1]="Dayton";
regions[2]="Mason";
regions[3]="Beavercreek";
regions[4]="Franklin";
regions[5]="Huber Heights";
regions[6]="Kettering";
regions[7]="Miamisburg";
regions[8]="Middletown";
regions[9]="Monroe";
regions[10]="Oakwood";
regions[11]="Springboro";
regions[12]="Spring Valley";
regions[13]="Trenton";
regions[14]="Vandalia";
regions[15]="West Carrollton";





//////////////////////////////////////////////////////////////////////
// prev next buttons for intro page
//////////////////////////////////////////////////////////////////////
var regionState = 0; // starting region 
var totalRegions = 2; // total region uls divided by 2
var increment = "214"; // total pixels to move to the left per 2 uls

function region(direction) {
	if (direction == "prev") {
		if (regionState > 0) { regionState = (regionState - 1); }
	}
	else if (direction == "next") {
		if (regionState < (totalRegions - 1)) { regionState = (regionState + 1); }
	}
	
	var location = (increment * regionState);
	document.getElementById('regionContainerInner').style.left = "-" + location + "px";
	
	if (regionState == 0) { document.getElementById('regionPrev').className = "prevRegionDisabled"; }
	else { document.getElementById('regionPrev').className = "prevRegion"; }
	
	if (regionState < (totalRegions - 1)) { document.getElementById('regionNext').className = "nextRegion"; }
	else { document.getElementById('regionNext').className = "nextRegionDisabled"; }
}


//////////////////////////////////////////////////////////////////////
// select region button
//////////////////////////////////////////////////////////////////////
var regionVisible = "no";

function initRegion() {

/*document.getElementById('selectARegionButton').onclick = function() {
	turnOffErrorFloat();
	if (regionVisible == "no") {
	document.getElementById('mainContainerInner').style.top = '-699px';
	document.getElementById('selectARegionButton').style.backgroundPosition = "0 -21px";
	regionVisible = "yes";
	elbo();
	}	
	else {
	document.getElementById('mainContainerInner').style.top = '-233px';
	document.getElementById('selectARegionButton').style.backgroundPosition = "0 0";
	regionVisible = "no";
	elbo();	
	}
}

document.getElementById('regionClose').onclick = function() {
	document.getElementById('mainContainerInner').style.top = '-233px';
	document.getElementById('selectARegionButton').style.backgroundPosition = "0 0";
	regionVisible = "no";
	elbo();	
}

*/


// setup click events for regions
var x=0;
var regionCookie = readCookie('region');

if (regionCookie) {
regionCookie = regionCookie.replace('%20',' ');
if (regionCookie == '') { regionCookie = "Cincinnati"; }
if (regionCookie != "Cincinnati") { document.getElementById('introSiteId').innerHTML = ": " + regionCookie; }
} 
//for (x=0; x<16; x++) { 
//var obj = regions[x];
//if (regionCookie == '') { regionCookie = "Cincinnati"; }
//if (regionCookie != "Cincinnati") { document.getElementById('introSiteId').innerHTML = ": " + regionCookie; }
//document.getElementById(obj).onclick = changeRegion;
//if (regionCookie == obj) { document.getElementById(obj).className = 'active'; }
//}

}

//////////////////////////////////////////////////////////////////////
// show region elbo
//////////////////////////////////////////////////////////////////////
function elbo() {
//////////////////////////////////////////////////////////////////////
// on resize, move elbo
//////////////////////////////////////////////////////////////////////
window.onresize = elbo;
//////////////////////////////////////////////////////////////////////

if (regionVisible == "yes") {
var sabX = findPosX(document.getElementById('selectARegionButton'));
var sabY = findPosY(document.getElementById('selectARegionButton'));
document.getElementById('introElbo').style.top = (sabY - 12) + "px";
document.getElementById('introElbo').style.left = (sabX - 90) + "px";
document.getElementById('introElbo').style.display = "block";
}
else {
document.getElementById('introElbo').style.display = "none";
}
}




//////////////////////////////////////////////////////////////////////
// change the region based on click
//////////////////////////////////////////////////////////////////////
function changeRegion(r) {
// turn off all 
var x=0;

for (x=0; x<16; x++) { 
var obj = regions[x];
document.getElementById(obj).className = ''; }
eraseCookie('region');
createCookie('region', this.innerHTML,90,'cincinnatibell.com');
document.getElementById('introSiteId').innerHTML = ": " + this.innerHTML;
this.className = "active";
clearTimeout(timerID);
timerID = setTimeout("shutoff()", 1500);
new Effect.Highlight(this);
}
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// if you clicky the region, wait 2 seconds to run this to close region
//////////////////////////////////////////////////////////////////////
function shutoff() {
document.getElementById('mainContainerInner').style.top = '-233px';
document.getElementById('selectARegionButton').style.backgroundPosition = "0 0";
regionVisible = "no";
elbo();
clearTimeout(timerID);
}
//////////////////////////////////////////////////////////////////////

function setupRegionDropDown() {
var regionCookie = readCookie('region');
if (regionCookie) {
	var rob = document.regionForm.region;
	for (var i = 0; i < rob.length; i++) {
		if (rob[i].value == regionCookie) {
			rob.selectedIndex = i; 
		}
	}
}
}


//////////////////////////////////////////////////////////////////////
// change the region based dropdown
//////////////////////////////////////////////////////////////////////
function changeRegionDD(r) {
// turn off all 
eraseCookie('region');
var cookieDomain = '';
if (location.hostname.indexOf("cincinnatibell.com") > -1) { cookieDomain = "cincinnatibell.com"; }
createCookie('region', r.value,90,cookieDomain);
clearTimeout(timerID);
// squish the button, then make it rotatey
//hpRegion('80','yes');
new Effect.BlindUp('regionNav', { duration: .2});
// wait then reload
if ((r.value == "Mason") || (r.value == "Morrow") || (r.value == "SouthLebanon")) { timerID = setTimeout("location.href = '/consumer/bundles/';", 1200); }
else { timerID = setTimeout("location.href = location.href;", 1200); }
}


