// JavaScript Document

if(!window.$){
	function $(id)
	{
		return document.getElementById(id);
	}
}

/* 
  ------------------------------------------------
  Changes active tab to visible and hides inactive tabs
  Jonathan Dick, REV 20070829
  ------------------------------------------------
*/

function tabSwitch(caller,page)
{
	var tabs = [];
	tabs[0] = 'home-home';
	tabs[1] = 'home-myCalendar';
	tabs[2] = 'home-myRewards';
	tabs[3] = 'home-myFavorites';
	tabs[4] = 'merchant-merchantInfo';
	tabs[5] = 'merchant-merchantReviews';
	tabs[6] = 'ma-myAccount';
	tabs[7] = 'ma-myRewards';
	tabs[8] = 'ma-myCalendar';
	tabs[9] = 'ma-myFavorites';
	tabs[10] = 'product-productInfo';
	tabs[11] = 'product-locations';

	for(var i=0; i<tabs.length; i++)
	{
		try
		{
			if("tab-" + tabs[i] == caller.id)
			{
				document.getElementById("tab-" + tabs[i]).className = 'tab-' + page + '-active';
				document.getElementById("tab-title-" + tabs[i]).src = '/deals-images/tab-' + tabs[i] + '-a.gif';
				document.getElementById("tabs-content-" + tabs[i]).className = 'tabs-content-a';
			}
			else
			{
				document.getElementById("tab-" + tabs[i]).className = 'tab-' + page + '-inactive';
				document.getElementById("tab-title-" + tabs[i]).src = '/deals-images/tab-' + tabs[i] + '-i.gif';
				document.getElementById("tabs-content-" + tabs[i]).className = 'tabs-content-i';
			}
		}
		catch(error)
		{
			//
		}
	}
}
/* 
  ------------------------------------------------
  Changes rollover state of tabs
  Jonathan Dick, REV 20070829
  ------------------------------------------------
*/
function tabOver(caller,page)
{
	if(caller.className == 'tab-' + page + '-inactive')
	{
		document.getElementById(caller.id).className = 'tab-' + page + '-inactive-over';
	}
}

function tabOut(caller,page)
{
	if(caller.className == 'tab-' + page + '-inactive-over')
	{
		document.getElementById(caller.id).className = 'tab-' + page + '-inactive';
	}
}/* 
  ------------------------------------------------
  Changes rollover state of categories
  Jonathan Dick, REV 20070814A
  ------------------------------------------------
*/
function categoryOver(caller)
{
	document.getElementById(caller.id + "-bullet").className = 'category-bullet-active';
	document.getElementById(caller.id).className = 'text-category-active';
}

function categoryOut(caller)
{
	document.getElementById(caller.id + "-bullet").className = 'category-bullet-inactive';
	document.getElementById(caller.id).className = 'text-category-inactive';
}

function mainCategoryOver(caller)
{
	document.getElementById(caller.id).className = 'text-category-active';
}

function mainCategoryOut(caller)
{
	document.getElementById(caller.id).className = 'text-category-inactive';
}
/* 
  ------------------------------------------------
  Changes height of div tags to match eachother
  Jonathan Dick, REV 20070821A
  ------------------------------------------------
*/
function contentHeightMatch(object_id_1,object_id_2)
{
	var object_id_1_height = document.getElementById(object_id_1).clientHeight;
	var object_id_2_height = document.getElementById(object_id_2).clientHeight;
	if(object_id_2 == 'merchant-tabs-content'){
		object_id_1_height += -170;
	}
	if(object_id_2 == 'product-tabs-content'){
		object_id_1_height += -290;
	}
	/* 
	alert("main-content: height = " + document.getElementById(object_id_1).style.height + '\n' + "       clientHeight = " + document.getElementById(object_id_1).clientHeight + '\n' + "       offsetHeight" + document.getElementById(object_id_1).offsetHeight + '\n' + "header-side: height = " + document.getElementById(object_id_2).style.height + '\n' + "       clientHeight = " + document.getElementById(object_id_2).clientHeight + '\n' + "       offsetHeight" + document.getElementById(object_id_2).offsetHeight);
	*/
	if(object_id_1_height > object_id_2_height)
	{
		document.getElementById(object_id_2).style.height = object_id_1_height + "px";
	}
	else
	{		
		document.getElementById(object_id_1).style.height = object_id_2_height + "px";
	}
	/* 
	alert("main-content: height = " + document.getElementById(object_id_1).style.height + '\n' + "       clientHeight = " + document.getElementById(object_id_1).clientHeight + '\n' + "       offsetHeight" + document.getElementById(object_id_1).offsetHeight + '\n' + "header-side: height = " + document.getElementById(object_id_2).style.height + '\n' + "       clientHeight = " + document.getElementById(object_id_2).clientHeight + '\n' + "       offsetHeight" + document.getElementById(object_id_2).offsetHeight);
	*/
}


function frameHeightMatch(source_object_id,match_object_id_1,match_object_id_2)
{
	var sourceObjectHeight = document.getElementById(source_object_id).clientHeight;
	document.getElementById(match_object_id_1).style.height = sourceObjectHeight + "px";
	document.getElementById(match_object_id_2).style.height = sourceObjectHeight + "px";
}
//Used for AdvertPro Text Ads.
function populateTextAd(mid,merchant_id,brand_name,teaser,desc,full_ad,link_to_url){

		var tracker = document.getElementById('ad_url').href;
		document.getElementById('teaser').id = 'teaser' + mid;
		document.getElementById('ad_url').id = 'ad_url' + mid
		document.getElementById('desc').id = 'desc' + mid;
		document.getElementById('brand').id = 'brand' + mid;
		document.getElementById('container').id = 'container' + mid;
		//write out the ads.
	if(merchant_id == mid)	{
		document.getElementById('availableSpecialsWrapper').style.display = 'block';
		document.write(full_ad);	 	
	}
	else{
		document.getElementById('ad_url' + mid).innerHTML = teaser;
		document.getElementById('ad_url' + mid).href = tracker + link_to_url;
		document.getElementById('teaser' + mid).className = 'advertProTextAd_ON';
		document.getElementById('desc' + mid).innerHTML = desc;
		document.getElementById('desc' + mid).className = 'advertProTextAd_ON';
		document.getElementById('brand' + mid).innerHTML = brand_name;
		document.getElementById('brand' + mid).style.color = '#CF5100';
		document.getElementById('brand' + mid).className = 'advertProTextAd_ON';
	}

}

//Used to populate the search fields in the header

function getSearchValue(searchField){
		defaultFieldValue = document.getElementById(searchField).defaultValue;
		fieldValue = document.getElementById(searchField).value;
		if(fieldValue == ''){
			return defaultFieldValue;
			}
		else{
			return fieldValue;
		}
}


/* SHOW CART METHODS */
function toggle(e) {
	e.checked = !e.checked;
}
function checkToggle(e) {
	var cform = document.cartform;
	if (e.checked) {
		var len = cform.elements.length;
		var allchecked = true;
		for (var i = 0; i < len; i++) {
			var element = cform.elements[i];
			if (element.name == "selectedItem" && !element.checked) {
				allchecked = false;
			}
			cform.selectAll.checked = allchecked;
		}
	} else {
		cform.selectAll.checked = false;
	}
}
function toggleAll(e) {
	var cform = document.cartform;
	var len = cform.elements.length;
	for (var i = 0; i < len; i++) {
		var element = cform.elements[i];
		if (element.name == "selectedItem" && element.checked != e.checked) {
			toggle(element);
		}
	}
}
function removeSelected() {
	var cform = document.cartform;
	cform.removeSelected.value = true;
	cform.submit();
}
function addToList() {
	var cform = document.cartform;
	cform.action = "/new-deals/control//addBulkToShoppingList</@ofbizUrl>";
	cform.submit();
}
/* END SHOW CART */


/* ADVANCED SEARCH  */
function showSubCat(){
	//Find the current parent category.
	var parent = document.getElementById("parentcategory");
	var parentId = parent.options[parent.selectedIndex].value;
	//Find the last parent category
	var lastParent = document.getElementById("pcategory").value;
	if (lastParent.length > 0 && document.getElementById(lastParent)) {
		document.getElementById(lastParent).className="tabletexthidden";
		document.getElementById(lastParent).name="hidden_sub_cat";
	} else {
		document.getElementById("ALL_CATEGORIES").className="tabletexthidden";
	}
	//Set the category_id to the current parent category
	catUpdate(parentId);
	//At this point post the category_id and pcategory are the same.
	document.getElementById("pcategory").value = parentId;
	
	if (document.getElementById(parentId)) {
		document.getElementById(parentId).className="visible";
	} else {
		//Just in case no sub-categories are configured. 
		document.getElementById("ALL_CATEGORIES").className="visible";	
	}

}

function catUpdate(val){
	if (val && val.length > 0) {
		document.advtokeywordsearchform.SEARCH_CATEGORY_ID.value = val;
	} else {
		document.advtokeywordsearchform.SEARCH_CATEGORY_ID.value = document.advcategorysearchform.pcategory.value;
	}
}
/* END ADVANCED SEARCH */

/* STANDARD SEARCH */
function chkSearch(obj, defaultKeywordText, defaultLocationText) {
	if(obj.SEARCH_STRING.value == defaultKeywordText) {
		obj.SEARCH_STRING.value = "";
	}
	if(obj.SEARCH_LOCATION.value == defaultLocationText) {
		obj.SEARCH_LOCATION.value = "";
	}
	if (obj.SEARCH_STRING.value.length > 0) {
		obj.SEARCH_CATEGORY_ID.value = 'ALL_CATEGORIES';
	}
}
/* END STANDARD SEARCH */

function openWindow(url,windowName,w,h,properties,centerWindow,getHandle)
{
        var extra = '';
        if(centerWindow == true)
        {
                var winl = (screen.width - w) / 2;
                var wint = (screen.height - h) / 2;
                extra = ',top=' + wint + ',left=' + winl;
        }

        windowHandle = window.open(url,windowName,'width='+w+',height='+h+','+properties + extra);
        if(parseInt(navigator.appVersion) >= 4)
        {
                windowHandle.window.focus();
        }
        if(getHandle == true)
        {
                return windowHandle;    
        }
}

function notify(productId)
{
window.open( "/deals/control/viewRestockNotification?productId="+productId, "emailWindow", "status = 1, height = 300, width = 375, resizable = 0" )
}

//Newsleter Subscribe
function subscribe(obj, redirect, zipCode){
	/* window.open( "http://marketing.citydeals.com/box.php?funcml=add&nlbox[1]=1&email="+obj.value, "emailWindow", "status = 1, height = 400, width = 400, resizable = 0" )*/
	
	
	var windowName = "emailWindow";
	var w = 400;
	var h = 400;
	var extra = '';
	var properties = "status = 1, resizable = 0";
	var url = null;
	
	if(redirect == true){
		if(zipCode != null && zipCode != "Enter Zip Here" && zipCode != ""){
			url = "/deals/control/requirezipcode?email=" + obj.value + "&zipCode=" +zipCode+ "&refer=emailEnroll";
		} else {
			url = "http://marketing.citydeals.com/box.php?funcml=add&nlbox[1]=1&email="+obj.value;
		}
		
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		extra = ',top=' + wint + ',left=' + winl;
		
		window.open(url,windowName,'width='+w+',height='+h+','+properties + extra);
		document.getElementById('my-offers-hidden').className = "hiddenZip";
	} else {
		document.getElementById('my-offers-hidden').className = "visibleZip";
	}
}

function openWindow(url,windowName,w,h,properties,centerWindow,getHandle)
{
	var extra = '';
	if(centerWindow == true)
	{
		var winl = (screen.width - w) / 2;
		var wint = (screen.height - h) / 2;
		extra = ',top=' + wint + ',left=' + winl;
	}
	
	windowHandle = window.open(url,windowName,'width='+w+',height='+h+','+properties + extra);
	if(parseInt(navigator.appVersion) >= 4)
	{
		windowHandle.window.focus();
	}
	if(getHandle == true)
	{
		return windowHandle;	
	}
}

function showFromMessage(distance, city, state, zip, event) {
	//The tip method is in the wz_tooltip.js
	var message = "The distance shown, "+distance+" miles, is measured from the center of ";
	if (zip) {
		message = message + city +', '+state+' '+zip; 
	} else if (city) {
		message = message + city +', '+state; 
	}
	message = message +'.<br> These distances are linear (as the crow flies). The actual driving distance may vary.';
	Tip(message, EVENT, event, DELAY, 500, BGCOLOR, '#FFFFFF', CLOSEBTN, true, CLOSEBTNTEXT, 'close', CLOSEBTNCOLORS, ['', '#ffffff', '', ''], CENTERMOUSE, true, ABOVE, true, TITLEBGCOLOR, '#6ca4d8', PADDING, 5, STICKY, true);
}

//Trim functions used by the Merchant Pages
function LTrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}


//pad digits with leading zeros for odometer request counters
function PadDigits(n, totalDigits) 
{ 
    n = n.toString(); 
    var pd = ''; 
    if (totalDigits > n.length) 
    { 
        for (i=0; i < (totalDigits-n.length); i++) 
        { 
            pd += '0'; 
        } 
    } 
    return pd + n.toString(); 
}

function num2Image(num)
{
   numstring = PadDigits(num,4);
   for(i=0; i< 4; i++)	
   {
      document.write('<img src="/deals-images/digits/'+numstring.charAt(i)+'.gif" height="12" align="top">')
   }
} 
