//<!--
d = new Date();
day = d.getDate();
month = d.getMonth()+1;
year = d.getFullYear();
full = month + "." + day + "." + year;
//--------------------------------------------------
function writeYear() {
	var today = new Date();
	year = today.getFullYear();
	document.write(year); 
}
//--------------------------------------------------
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.0
	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 && document.getElementById) x=document.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_displayStatusMsg(msgStr) { //v1.0
	status=msgStr;
	document.MM_returnValue = true;
}
//--------------------------------------------------
	function cleartextfield(fldtext){
		if (fldtext.defaultValue == fldtext.value) {
			fldtext.value = '';
		}
	}
//--------------------------------------------------
	function validateZipCode(fldId){
		var fld = document.getElementById(fldId);
		var zipcode =/^\d{5}$/;
		var valid = zipcode.exec(fld.value);		
		if(valid){			
			return true;
		}else{
			alert("Invalid zipcode entered. Please re-enter the zipcode");
			fld.value = "";
			fld.focus();
			return false;
		}
	}
//--------------------------------------------------
	function printpage() {
		window.print();  
	}
//--------------------------------------------------

function previewWindow(url) {
	window.open(url, "_blank", "menubar=0,location=0,toolbar=0,status=0,titlebar=0,scrollbars=1,resizable=1");
}

function clearSiteSearchField(field) {
	if (field.value == "Search") field.value = "";
}
function clearText(thefield) {
  if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
  if (thefield.value=="") { thefield.value = thefield.defaultValue }
}
function searchSite(frm) {
	if (frm.query.value == "Search" || frm.query.value == "") return false;
	return true;
}

function popupSearch() {
	if($('popupcontent')) {
		$('popupcontent').innerHTML = $('searchbox').innerHTML;
		popup('popup', 'searchnav', 250, 65, 200, 60);
	}
}

function maskPhoneNumber(inputobj, errobj) {
	var resPhone = "";
	var clnPhone = "";
	var fparts = inputobj.value.split("");
	var idx;
	for (idx = 0; idx < fparts.length; idx++) {
		if ("1234567890".indexOf(fparts[idx]) != -1) {
			clnPhone += fparts[idx];
		}
	}

	if (clnPhone.length == 10) {
		var clnPhoneParts = clnPhone.split("");
		for (idx = 0; idx < clnPhoneParts.length; idx++) {
			if (idx == 0) {
				resPhone += "(";
			} else if (idx == 3) {
				resPhone += ") ";
			} else if (idx == 6) {
				resPhone += "-";
			}
			resPhone += clnPhoneParts[idx];
		}
		inputobj.value = resPhone;
		errobj.style.display = "none";
		return true;
	} else {
		errobj.style.display = "block";
		return false;
	}
}

//Custom hbLink tag. Check if the real _hbLink function
//is defined befare attempting to call it. We do this
//because the hbx.js may take awhile to load and the user
//may be clicking on things before hbx is fully loaded.
function _hbLink_(lid) {
	if (typeof(_hbLink) != "undefined") {
		_hbLink(lid);
	}
}
//-->


function goToMyAccount(relativetoid,url) {
	cartCall(
		{
			url: url,
			relativetoid: relativetoid,
			popupcallback: function(jsonobj) {
				popup(jsonobj.popupid, relativetoid, 248, 119, 0, 110);
			}
		}
	);
}
function getElementPosition(elemID,popupWidth,popupHeight){
	var ele = $(elemID);
	
	position = Position.page(ele);
	
	// determine scrolling offsets
	// this determines if IE is in strict mode or not (DOCTYPE exists)
	var iebody=(document.compatMode && document.compatMode != "BackCompat")
				? document.documentElement : document.body;
	var offsetX = document.all? iebody.scrollLeft : window.pageXOffset;
	var offsetY = document.all? iebody.scrollTop : window.pageYOffset;
	// end 
	
	var direction = '';
	
	var width = ele.getWidth();
	var height = ele.getHeight();
		
	if((position[1]-popupHeight)> 0) {
		
		offsetTop = position[1] - popupHeight;
		direction = 'bottom';
	} else {
		
		offsetTop = position[1]+height;
		direction = 'top';
	}
	if((position[0]-popupWidth)> 0) {
		
		offsetLeft = position[0] - popupWidth;
		direction += '-right';
	} else {
		offsetLeft = position[0]+width;
		direction += '-left';
	}
	
	// adjust for scrolling offsets
	offsetTop += offsetY;
	offsetLeft += offsetX;
	
	return {left:offsetLeft,top:offsetTop,dir:direction};
}

function closePopup(popupid,lpos) {
	if($('aal_broadband_notification_begin')) {
		$('aal_broadband_notification_begin').hide()
	}
	if($('aal_broadband_notification_continue')) {
		$('aal_broadband_notification_continue').hide()
	}
	
	if($('aal_wireless_notification_begin')) {
		$('aal_wireless_notification_begin').hide()
	}
	if($('aal_wireless_notification_continue')) {
		$('aal_wireless_notification_continue').hide()
	}
	
	if($('aal_familyplan_notification_begin')) {
		$('aal_familyplan_notification_begin').hide()
	}
	if($('aal_familyplan_notification_continue')) {
		$('aal_familyplan_notification_continue').hide()
	}
	
	if($('cant_mix_with_upgrade_phone_continue')) {
		$('cant_mix_with_upgrade_phone_continue').hide()
	}
	if($('cant_mix_with_upgrade_phone_begin')) {
		$('cant_mix_with_upgrade_phone_begin').hide()
	}
	
	if($('cant_mix_with_upgrade_modem_continue')) {
		$('cant_mix_with_upgrade_modem_continue').hide()
	}
	if($('cant_mix_with_upgrade_modem_begin')) {
		$('cant_mix_with_upgrade_modem_begin').hide()
	}
	
	if($('cant_mix_with_change_plan_continue')) {
		$('cant_mix_with_change_plan_continue').hide()
	}
	if($('cant_mix_with_change_plan_begin')) {
		$('cant_mix_with_change_plan_begin').hide()
	}
	
	if($('cant_mix_with_new_feature_continue')) {
		$('cant_mix_with_new_feature_continue').hide()
	}
	if($('cant_mix_with_new_feature_begin')) {
		$('cant_mix_with_new_feature_begin').hide()
	}
	if(popupid == 'LOSSelectorNav') {
		document.location.href = '/myaccount';
	}
	var popup = $(popupid);
	popup.hide();

	
	// tell HBX
	_hbSet('lid','close');
	
	if(lpos != null)
		_hbSet('lpos',lpos);
		
	_hbSend();
}

function popup(popupid, relativetoid, popupType) {
	
	var popup = $(popupid);
	var relativeobj = $(relativetoid);
	var w = popup.getWidth();
	var h = popup.getHeight();

	var relativeto = getElementPosition(relativetoid,w,h);

	var left = relativeto.left;
	var top = relativeto.top;
	var dir = relativeto.dir;
	
	popup.style.display = "none";
	popup.style.left = left+'px';
	//FIXME : remove the hardcoded 200px
	popup.style.top = (top-200)+'px';

	//Use scriptaculous to animate the popup.
	new Effect.Grow(popupid, {direction: dir, duration: '0.5'});
	
	// NOW CALL THE HITBOX CODE if necessary
	if(popupType != null)
		callHitboxPageView(popupType);
	// END HHITBOX
}
function callHitboxPageView(name,forceName){
	var hbxPopupInfo = $H({'changezip':['change_zipentry','/popup'],
							'startover':['startover_confirmation','/popup'],
							'incomplete_checkout':['checkout_infompletepackage','/popup'],
							'changezip_conflict':['addtocart_conflict_voiceplan_with_voiceplan','/popup'],
							'changezip_invalid':['','/popup'],
							'addtocart_zipentry':['addtocart_zipentry','/popup'],
							'popup_script':['','/popup']
						});
	if(typeof hbxPopupInfo[name] != 'undefined') {
			var useName = (forceName != null) ? forceName : hbxPopupInfo[name][0];
			_hbPageView(useName,hbxPopupInfo[name][1]);
		}
}

var Leap = Class.create();
Leap.prototype = {
	initialize: function(){},
	enableLogger: true,
	ajaxRequest: function(url,options
//		params,successHandler,failureHandler
		){
		new Ajax.Request(url,{
			method: 'post',parameters: options.parameters,
			onSuccess: function(transport) {
				var response = LeapJS.getAjaxResponse(transport);
				if(!response.success && response.session_timeout == true){
					document.location.href = response.redirect_to;
					return;
				}
				options.onSuccess(response);
			},
			onFailure: function(transport) {
				var response = LeapJS.getAjaxResponse(transport);
				options.onFailure(response);
			},
			onLoading: function() {
				if(options.onLoading != undefined) {
					options.onLoading();
				}
			}
		});
	},
	getAjaxResponse: function(transport) {
		try {
			var response = eval('(' + transport.responseText + ')');
		} catch (err) {
			var response = {};
	  		response.success = false;
	  		response.errorMessages = '<div class="errorsList">Invalid Server Response</div>';
		}
		
		if (!response.success
			&& typeof response.exception != 'undefined'
			&& typeof response.popup != 'undefined'
			&& typeof response.popupID != 'undefined'
		) {
			var div = document.createElement('div');
			div.setAttribute('id','exception_container_' + response.popupID);
			div.innerHTML = response.popup;
			document.body.appendChild(div);
			popup(response.popupID,'logo-section');
		}
		
		// give it back
		response.responseText = transport.responseText;
		return response;
	},
	log: function(whatToLog) {
		if( this.enableLogger && (typeof console != 'undefined'))
			 console.log(whatToLog);
	}
};

var LeapJS = new Leap();
//-------------
//-->