function init(){
	if(TransMenu.isSupported()){
		TransMenu.initialize();
	}
}

function swapSrc(obj){
	var cSrc = obj.src;
	if (cSrc.indexOf("_off.") > 0){cSrc = cSrc.replace("_off.","_on.");}
	else if (cSrc.indexOf("_on.") > 0){cSrc = cSrc.replace("_on.","_off.");}
	obj.src = cSrc;
}

function copyForm(formType){
	var theForm = document.cartInformation;
	
	theForm.bill_address1.value = theForm.ship_address1.value;
	theForm.bill_address2.value = theForm.ship_address2.value;
	theForm.bill_city.value = theForm.ship_city.value;
	theForm.bill_companyname.value = theForm.ship_companyname.value;
	//theForm.bill_country.value = theForm.ship_country.value;
	theForm.bill_email.value = theForm.ship_email.value;
	theForm.bill_firstname.value = theForm.ship_firstname.value;
	theForm.bill_lastname.value = theForm.ship_lastname.value;
	theForm.bill_phone.value = theForm.ship_phone.value;
}

function checkLocation(passThat){
	var theForm = document.cartInformation;
	var returnState = true;

	if((theForm.bill_country.value == "X") || (theForm.ship_country.value == "X")){
		alert("Please select a location");
		returnState = false;
	}
	
	if((theForm.bill_country.value == "US") && (theForm.bill_state.value == "X")){
		alert("Please select a state");
		returnState = false;
	}else if((theForm.bill_country.value == "US") && (theForm.bill_zipcode.value == "")){
		alert("Please enter your zipcode");
		returnState = false;
	}
	
	return returnState;
}

function swapCountry(selectedVal){
	var theForm = document.cartInformation;
	
	if(selectedVal == 'US')document.getElementById('billingforus').style.display = "block";
	else document.getElementById('billingforus').style.display = "none";
}

function editInformation(action){
	var theForm = document.reviewCart;
	theForm.action.value = action;
	theForm.submit();
}

function store_addFilter(obj,filter){
	store_filter[filter][1] *= -1;
	if(store_filter[filter][1] > 0){filterState = "on";}else{filterState = "off";}
	obj.src = "/_img/_products/_store/filter_" + store_filter[filter][0] + "_" + filterState + ".gif"
	store_fetch();
}

function store_filterList(){
	var filterList = "";	
	for(var i=0; i<store_filter.length; i++){
		if(store_filter[i][1] > 0) filterList = filterList + store_filter[i][0] + ",";
	}
	return filterList;
}

function store_clearDisplay(){
	document.getElementById('noresults').style.display = "none";
	if(store_products.length){
		for(var i=0; i<store_products.length; i++){
			if(document.getElementById(store_products[i])){
				document.getElementById('storeListing').removeChild(document.getElementById(store_products[i]));
			}
		}
	}
}
	
function store_fetch(){
	document.getElementById('loading').style.display = "block";
	store_clearDisplay();
	params = new Object();
	params.filter = store_filterList();
	params.type = curType;
	params.brand = curBrand;
	params.range = curRange;
	http("POST", "/_cfc/pages/store.cfc?method=listProducts", store_fetch_r, params);
}

var myJson,myData,jsonResults,jsonCols,maxPerPage,maxPages,curType,curBrand,curRange;

function store_fetch_r(r){
	myJson = JSON.parse(r);
	myData = myJson["data"];
	jsonResults = myJson["recordcount"];
	jsonCols = myJson["columnlist"];
	maxPerPage = 8;
	maxPages = Math.ceil(jsonResults/maxPerPage);
	
	if(curType == 'P')
		var typeString = "Phones";
	else if(curType == 'A')
		var typeString = "Accessories";
	else
		var typeString = "All";
	
	var htmlContent = "<span style=\"font-size:13px;\"><b>" + jsonResults + "</b> Products Found (Viewing "+typeString+")</span><br>";
	
	if(maxPages > 1){
		htmlContent = htmlContent + "<span style=\"color:#666;font-size:11px;\">Pages:&nbsp;&nbsp;</span>";
		for(var i=1; i<=maxPages; i++){htmlContent = htmlContent + "<a id=\"pg"+i+"\" style=\"cursor:hand;cursor:pointer;color:#666;font-size:11px;margin-right:5px;\" onclick=\"store_view("+i+")\">"+i+"</a>";}
	}
	
	$('pReturned').innerHTML = htmlContent;
	store_view(1);
}

function store_view_color_page(page){
	if(maxPages > 1){
		for(var i=1; i<=maxPages; i++){
			document.getElementById('pg'+i).style.color='#666666';
			document.getElementById('pg'+i).style.fontWeight='normal';
		}
		document.getElementById('pg'+page).style.color='#6DB73B';
		document.getElementById('pg'+page).style.fontWeight='bold';
	}
}

function store_view(page){
	store_clearDisplay();
	store_view_color_page(page);
	
	var startAt = eval((page-1)*maxPerPage);
	var endAt = eval(startAt+maxPerPage);
	
	if(endAt > jsonResults)endAt = jsonResults;
	
	for(var i=startAt; i<endAt; i++){
		var newProduct = document.createElement("div");
		newProduct.setAttribute("id", "p"+myData["id"][i]);
		
		var features = myData["features"][i].split(",");
		var htmlContent = "<table height=\"145\" cellpadding=\"0\" cellspacing=\"7\" border=\"0\"><tr><td valign=\"top\" width=\"49\" height=\"100\" bgcolor=\"#FFFFFF\"><div style=\"padding:5px;border:1px solid #cccccc;background:url(/_img/_products/" + myData["thumbnail"][i]  + ") no-repeat 50% 50%;\"><a href=\"/phones/" + myData["id"][i] + "\"><img src=\"/_img/_global/spacer.gif\" width=\"49\" height=\"100\" border=\"0\"></a></div></td><td valign=\"top\"><a href=\"/phones/" + myData["id"][i] + "\" style=\"font-size:26px;\">" + myData["brand"][i] + " " + myData["model"][i] + "</a><br>";
		if(features.length){for(var x=0; x<features.length; x++)htmlContent = htmlContent + "<img src=\"/_img/_products/_store/filter_small_" + features[x] + ".gif\" alt=\"" + features[x] + "\">";}
		htmlContent = htmlContent + "<br><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr>";
		if(myData["price"][i].length)htmlContent = htmlContent + "<td valign=\"top\" style=\"padding-right:15px;\"><span style=\"font-size:10px;\">Price</span><br><span style=\"font-size:15px;font-weight:bold;\">" + myData["price"][i] + "</span></td>";
		if(myData["pricepre"][i].length)htmlContent = htmlContent + "<td valign=\"top\" style=\"padding-right:15px;\"><span style=\"font-size:10px;\">Prepaid</span><br><span style=\"font-size:15px;font-weight:bold;\">" + myData["pricepre"][i] + "</span></td>";
		if(myData["pricepost"][i].length)htmlContent = htmlContent + "<td valign=\"top\" style=\"padding-right:15px;\"><span style=\"font-size:10px;\">Postpaid</span><br><span style=\"font-size:15px;font-weight:bold;\">" + myData["pricepost"][i] + "</span></td>";
		htmlContent = htmlContent + "</tr></table><br><a href=\"/phones/" + myData["id"][i] + "\"><img src=\"/_img/_global/_buttons/view_details.gif\" width=\"72\" height=\"11\" alt=\"View Details\" border=\"0\"></a></td></tr></table>";
		
		newProduct.innerHTML = htmlContent;
		document.getElementById('storeListing').appendChild(newProduct);
		store_products[i] = "p"+myData["id"][i];
		document.getElementById('p'+myData["id"][i]).style.width="375px";
		if(myData["sale"][i])document.getElementById('p'+myData["id"][i]).style.background="url(/_img/_products/_store/bg_sale_badge_holiday.gif) no-repeat bottom right";
		if(myData["valuepack"][i])document.getElementById('p'+myData["id"][i]).style.background="url(/_img/_products/_store/bg_valuepack.gif) no-repeat bottom right";
		document.getElementById('p'+myData["id"][i]).style.backgroundColor="#ffffee";
		document.getElementById('p'+myData["id"][i]).style.border="5px solid #cccccc";
		document.getElementById('p'+myData["id"][i]).style.marginRight="10px";
		document.getElementById('p'+myData["id"][i]).style.marginBottom="10px";
		document.getElementById('p'+myData["id"][i]).style.cssFloat="left";
		document.getElementById('p'+myData["id"][i]).style.styleFloat="left";
		document.getElementById('p'+myData["id"][i]).style.cssClear="none";
		document.getElementById('p'+myData["id"][i]).style.styleClear="none";
	}
	
	if(!maxPages){document.getElementById('noresults').style.display = "block";}
	document.getElementById('loading').style.display = "none";
}

function store_setType(t){
	curType = t;
	store_fetch();
}

function store_setBrand(b){
	curBrand = b;
	store_fetch();
}

function store_setRange(r){
	curRange = r;
	store_fetch();
}

function liveSupportAgree(){
	$('liveSupportTerms').style.display = "none";
	$('liveSupportChat').style.display = "block";
}

function fp_downloadForm(){
	if($('fp_agreeterms').checked)
		location.href='/_assets/FamilyPlan_ChangeOfResponsibility.pdf';
	else
		alert("You must agree to the terms before downloading");
}

function showIce(s){//Swtich, Width, Height
	var w = 640;
	var h = 407;
	var vPlayer = "<iframe src=\"/_assets/bb_storm_9520.htm\" width=\"640\" height=\"360\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe><a href=\"javascript:showIce(0)\" style=\"color:white;\">Close Video</a>";
	
	if(s){
		document.getElementsByTagName("body")[0].style.overflow = 'hidden';
		document.getElementsByTagName("html")[0].style.overflow = 'hidden';
		
		document.getElementById('greyIce').style.width = f_clientWidth()+'px';
		document.getElementById('greyIce').style.height = f_clientHeight()+'px';
		document.getElementById('greyIce').style.top = f_scrollTop()+'px';
		document.getElementById('greyIce').style.display = 'block';	
		
		var leftP = eval((f_clientWidth()-w)/2);
		var topP = eval(((f_clientHeight()-h)/2)+f_scrollTop());
		
		document.getElementById('greyIceContainter').innerHTML = vPlayer;
		document.getElementById('greyIceContainter').style.overflow = 'hidden';
		document.getElementById('greyIceContainter').style.width = w+'px';
		document.getElementById('greyIceContainter').style.height = h+'px';
		document.getElementById('greyIceContainter').style.left = leftP+'px';
		document.getElementById('greyIceContainter').style.top = topP+'px';
		document.getElementById('greyIceContainter').style.display = 'block';
		
	}else{
		document.getElementsByTagName("body")[0].style.overflow = '';
		document.getElementsByTagName("html")[0].style.overflow = '';
		
		document.getElementById('greyIce').style.overflow='hidden';
		document.getElementById('greyIce').style.width='0px';
		document.getElementById('greyIce').style.height='0px';
		document.getElementById('greyIce').style.display='none';
		
		document.getElementById('greyIceContainter').style.overflow='hidden';
		document.getElementById('greyIceContainter').style.width='0px';
		document.getElementById('greyIceContainter').style.height='0px';
		document.getElementById('greyIceContainter').style.display='none';
	}
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}