var HP_URL_STAGING = 'hpstaging.Wibiti.com/';
var HP_URL_LOCAL = 'localhost/sa3hp2/';
var HP_URL_FIFTYEIGHT = 'fiftyeight/sa3hp2/';
//LIVE SITE URL is set from location.host

var HP_IMG_SUB_LIVE = 'thumbs/hpmain/';
var HP_IMG_SUB_STAGING = 'thumbs/hptest/';

var IMG_DEFAULT = 'pics/noPhoto-60x40.jpg';
var HPURL_LIST = '';
var HP_SORT_STATE = '';
var DDL_CITY_OPTION;
var DDL_AREA_OPTION;
var HP_CAT_ID = '';
var NAVIGATE_FROM = true;
var CAT_LINK_CLICK = false;
var VENDID = 0;
var STATE = '';

//------------------------
//for search service only
//------------------------
var ADS_ID_SERVICE = 0;
var CAT_ID_SERVICE = 0;
var VEND_ID_SERVICE = 0;
var REGION_ID_SERVICE = 0;
var GMAP_ZOOM_LEVEL = 3;
//------------------------

//-----------------------
//for yoChicago only
//-----------------------
var UBER_CAT_SERVICE = 0;
var SET_AUTO_RESULTS = 1;

function getHost()
{
	switch(location.host)
	{
		case 'localhost' :
			//return 'localhost/sa3hp2/';
			return 'Wibiti.com/';
			break;
			
		case 'fiftyeight' :
			return 'hpstaging.Wibiti.com/';
			break;
		
		default :
			return 'Wibiti.com/';
			break;
	}
}

function getServiceHost()
{
	switch(location.host)
	{
		case 'localhost' :
			return 'localhost/sa3hp2search/';
			break;
			
		case 'hpstaging.Wibiti.com' :
			return 'hpstaging.Wibiti.com/search/';
			break;
			
		default :
			return 'wibiti.com/search/';
			break;
			
		//default :
		//	return 'search.wibiti.com/';
		//	break;

	}
}

function buildImagePath(imgID)
{
	
	if (imgID == '0')
	{
		imagePath = 'http://Wibiti.com/pics/noPhoto-60x40.jpg';
	}
	else
	{
		//if ((location.host == 'localhost') || (location.host == 'hpstaging.Wibiti.com') || (location.host == 'fiftyeight'))
		//{
			//imagePath = 'http://' + HP_URL_STAGING + HP_IMG_SUB_STAGING + imgID.substr(3,3) + '/' + imgID + '.jpg';
			imagePath = 'http://Wibiti.com/' + HP_IMG_SUB_LIVE + imgID.substr(3,3) + '/' + imgID + '.jpg';
		//}
		//else
		//{
		//	imagePath = 'http://' + location.host + '/' + HP_IMG_SUB_LIVE + imgID.substr(3,3) + '/' + imgID + '.jpg';
		//}

	}
	
	return imagePath;
}

function getVendID()
{
	if (QueryString("v") == null) return VENDID
	else return QueryString("v")
}

function IsValidZip(zip)
{
	zip = zip+'';
	if ((zip != '') && (zip.length == 5) && (IsNumeric(zip)))
		{return true;}
	else
		{return false;}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function QueryString(key)
{ 
	var query = window.location.search.substring(1); 
	var pairs = query.split("&"); 
	
	QueryString.keys = new Array(); 
	QueryString.values = new Array();
	
	for (var i=0;i<pairs.length;i++) 
	{ 
		var pos = pairs[i].indexOf('='); 
		if (pos >= 0) 
		{ 
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1); 
			QueryString.keys[QueryString.keys.length] = argname; 
			QueryString.values[QueryString.values.length] = value;   
		} 
	} 

	var value = null;

	for (var i=0;i<QueryString.keys.length;i++) 
	{ 
		if (QueryString.keys[i]==key) 
		{ 
			value = QueryString.values[i]; 
			break; 
		} 
	} 
	return value; 
}

function NewWindow(pageURL, pageWidth, pageHeight, pageScrollbars, pageResizable, pageLeft, pageTop) {
//pop-up new window
//check for already opened pop-up window
	try {
		var winWidth = pageWidth;
		var winHeight = pageHeight;
		var winScrollbars = pageScrollbars;
		var winResizable = pageResizable;
		var winMenubar = 'no';
		var winToolbar = 'no';
		var winStatus = 'no';
		var winLeft = pageLeft;
		var winTop = pageTop;
		
		if (!winLeft && !winTop) {
			//if left and top undefined, try centering pop-up in the middle of the page
			if (!pageWidth && !pageHeight) { 
				//if pageWidth and pageHeight undefined
				winLeft = Math.round(screen.availWidth / 5);
				winTop = Math.round(screen.availHeight / 5);
			} else {
				//center pop-up
				winLeft = Math.round((screen.availWidth - pageWidth) / 5);
				winTop = Math.round((screen.availHeight - pageHeight) / 5);
			}
		}
			
		var winProps = 'width=' + winWidth;
		winProps = winProps + ',height=' + winHeight;
		winProps = winProps + ',scrollbars=' + winScrollbars;
		winProps = winProps + ',resizable=' + winResizable;
		winProps = winProps + ',menubar=' + winMenubar;
		winProps = winProps + ',toolbar=' + winToolbar;
		winProps = winProps + ',status=' + winStatus;
		winProps = winProps + ',left=' + winLeft;
		winProps = winProps + ',top=' + winTop;

		if (!window.newwin) { 
			newwin = window.open(pageURL, '', winProps); 
		} else { 
			if (!newwin.closed) { 
				newwin.location.href = pageURL;
				newwin.focus();
			} else { 
				newwin = window.open(pageURL, '', winProps); 
			}
		}
	}
	catch (e) {}
}

function formatnum(valuein,dollar) 
{ 

	valuein = "" + valuein;
	
	if (valuein.substring(0,1) == '$')
	{
		return valuein;
	}
	else
	{
		valuein = "" + Math.round( parseFloat(valuein) * 100 ) / 100;
		valueout = valuein.substring(valuein.length,valuein.length + 3) 
		valuein = valuein.substring(0, valuein.length)
		
		while (valuein.length>3) 
		{ 
			valueout = "," + valuein.substring(valuein.length - 3, valuein.length) + valueout 
			valuein = valuein.substring(0, valuein.length-3)
		}
		
		valueout = valuein + valueout
		
		if (dollar == 1)
			valueout = '$' + valueout;

		return valueout;
	}
}

function KeyDownHandler() {
// process only the Enter key
	if (event.keyCode == 13) {
		// cancel the default submit
        event.returnValue=false;
        event.cancel = true;
        
        // submit the form by programmatically clicking the specified button
        btn.click();
    }
}

function redirectToNavigation(hpURL,rownum)
{		
	var vendQS = '';
	var qs = QueryString("qs");
	
	if (QueryString("v") != null) vendQS = '&v=' + QueryString("v");
	if (qs == null) qs = 0;
	
	ddlRangeMin = document.getElementById('ddlRangeMin');
	ddlRangeMax = document.getElementById('ddlRangeMax');
	ddlDays = document.getElementById('ddlDays');
	
	ddlSubs = document.getElementById('ddlSubs');
	ddlArea = document.getElementById('ddlArea');
	ddlCity = document.getElementById('ddlCity');
	
	chkOpens =  document.getElementById('chkOpens');
	chkAdjZips = document.getElementById('chkAdjZips');
	
	var min = ddlRangeMin.value;
	var max = ddlRangeMax.value;
	var url = '';
	
	if (min == '')
	{
		min = 0;
		max = 0;
	}
	
	url	 = '?hpURL=' + hpURL + vendQS
	url += '&c=' + ddlSubs.value + '&s=' + QueryString("s")
	url += '&r=' + QueryString("r") + '&z=' + QueryString("z")
	url += '&a=' + ddlArea.value + '&n=' + ddlCity.value
	url += '&min=' + min + '&max=' + max
	url += '&oh=' + chkOpens.checked + '&az=' + chkAdjZips.checked
	url += '&d=' + ddlDays.value
	
	if (QueryString("recentHpURL") != null) url += '&recentHpURL=' + QueryString("recentHpURL")
	
	if (QueryString("from") == 'hp') url += '&from=hp'
	
	url += '&qs=' + qs
	url += HP_SORT_STATE

	
	sethpURLListCookie(rownum);
	location.href = 'Navigate.aspx' + url
}

function openHomePage(hpURL)
{
	NewWindow('http://' + getHost() + hpURL + '&popup=true',800,600,'yes','yes');
}

function buildhpURLList(hpURL)
{

	if (HPURL_LIST == '')
	{
		HPURL_LIST += hpURL.substring(1,5);
	}
	else
	{
		HPURL_LIST += '|' + hpURL.substring(1,5);
	}
}

function resetHpList()
{
	HPURL_LIST = '';
	tempTable = document.getElementById('tblHomePages')
	
	for (r=0; r<tempTable.rows.length-1; r++)
	{
		buildhpURLList(tempTable.rows[r+1].id.substring(3));
	}

}

function sethpURLListCookie(rownum)
{
	//cookie used on navigation page
	if (getCookie("hpList") != null)
	{
		deleteCookie("hpList");
	}
	
	var x = rownum //selected HP position
	var y = (HPURL_LIST.length + 1)/5 //total number of HPs
	var z = 550 //total allowed 
	var a = 0 //beginning position of range
	var b = 0 //end position of range
	
	
	//we need to rebuild HPURL_LIST if there are 
	//more than 550 homepages in selected list
	if (y > z)
	{
		if (x - (z / 2) <= 0)
		{
			a = 0
			b = z
		}
		else
		{
			if (x + (z / 2) > y)
			{
				a = y - z
				b = y			
			}
			else 
			{
				a = x - (z / 2)
				b = x + (z / 2)
			}
		}
		
		aryCookie = HPURL_LIST.split('|')
		
		//reset HP_URL_LIST
		HPURL_LIST = aryCookie[a]
		for (r=a+1; r<b; r++) HPURL_LIST += '|' + aryCookie[r]
	}
	
	setCookie("hpList",HPURL_LIST);
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
	//alert('setCookie');
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function setRentTerm(value)
{
	switch (value)
	{
		case "1" : return 'day'; break;
		case "7" : return 'wk'; break;
		case "30" : return 'mo'; break;
		default : return '0'; break;
	}
}

function setSortState(col,dir)
{
	HP_SORT_STATE = '&col=' + col + '&dir=' + dir;
}

function resetSortState()
{
	HP_SORT_STATE = '';
}


function Replace(argvalue, x, y) 
{ 
    while (argvalue.indexOf(x) != -1) 
	{ 
    	var leading = argvalue.substring(0, argvalue.indexOf(x)); 
    	var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, argvalue.length); 
    	argvalue = leading + y + trailing; 
	}
 
	return argvalue; 
} 

// Get current year for copyright notice. - KM 1/4/2005
var todaysDate = new Date();
var currentYear = todaysDate.getYear();

function writeCurYear(){
	document.write(currentYear);
}