/***************************
* Copyright (c) GrocerySavings.ca
* All Rights Reserved.
*
* Author: Ben Cho
* Website: grocerysavings.ca, localgrocerydeals.com
* CopyRight: 2007
*
* Utility Functions that can be shared
* -- Functions for trimming a string
* -- Functions for fixing javascript problems with "'" ("'" is a string delimiter), the problem occurs when a grocery name 
		contains a "'" and I try to add some onclick code with the use of the grocery name.
		TO solve the problem, i will call convertToJavascriptUse() to convert the grocery name to a javascript usable name,
		the function that uses the grocery name must reverse this procedure by calling convertToHTMLUse()
****************************/

// there is a problem with quotes in parameters in a javascript function
// so we will replace all "'" in the parameters with a "/"
// and when displayin it in html, we will revert it back

function trimString(sInString) 
{
	// sanity check
	if (isNull(sInString))
	{
		return sInString;
	}

	sInString = sInString.replace( /^\s+/g, "" );// strip leading
	return sInString.replace( /\s+$/g, "" );// strip trailing
}

function removeBlankSpaces(inString)
{
	if (!isEmpty(inString))
	{
	  return inString.replace(/\s/g, "");// strip all spaces
	}
}

// used by fifo.js
function convertToHTMLUse(varString)
{
	varString = varString.replace(/#!/g, "'");

//	varString = varString.replace(/\//g, "'");
	return varString;
}

// used by fifo.js
function convertToJavascriptUse(varString)
{
	varString = varString.replace(/\'/g, "#!");

//	varString = varString.replace(/\'/g, "/");	
	return varString;
}

// returns a string with quotes(') replaced by \'
function convertToJsFriendlyString(aStr)
{
	return aStr.replace(/'/g, "\\\'");
}

// returns a string with quotes(') replaced by \'
function convertToSqlFriendlyString(aStr)
{
	aStr = aStr.replace(/'/g, "\'\'");
	aStr = "'" + aStr + "'";
	return aStr;
}

function isEmpty(object)
{
	if (isNull(object))
	{
		return true;
	}
	else if (typeof(object) == "string")
	{
		if (trimString(object) == "")
		{
			return true;
		}
	}

	return false;
}

function isNull(object)
{
	if (object == null)
	{
		return true;
	}
	else if (typeof(object) == "undefined")
	{
		return true;
	}
	else if (object == "undefined")
	{
		return true;
	}
	else if (object == "null")
	{
		return true;
	}

	return false;
}

// relative path to where the grocery images live
var GROCERY_IMAGES_PATH = "images/groceries/";

/*
* if image is not found, it loads the "imageComingSoon.gif" image on the default.asp
*/
function validateImageNameSrc(domElement)
{
	var IMAGE_HEIGHT = 100;
	var IMAGE_WIDTH = 100;

	domElement.src = GROCERY_IMAGES_PATH + "imageComingSoon.gif";
	domElement.height = IMAGE_HEIGHT;
	domElement.width = IMAGE_WIDTH;
}

/*
* if image is not found, it loads the "imageComingSoon.gif" image on the search result pages
*/
function validateImageNameSrcSearchResult(domElement)
{
	var IMAGE_HEIGHT = 80;
	var IMAGE_WIDTH = 80;

	domElement.src = GROCERY_IMAGES_PATH + "imageComingSoon.gif";
	domElement.height = IMAGE_HEIGHT;
	domElement.width = IMAGE_WIDTH;
}

/*
* filter special characters "< > " ' % ; ) ( & + --"
	filter based on grocery names "< > " ; ) ( & --"
*/
function removeBad(strTemp) 
{ 
//    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-\-/g, ""); 
    strTemp = strTemp.replace(/\<|\>|\"|\;|\(|\)|\-\-/g, ""); 
	return strTemp;
} 
	
/*
* put the dom element with the given elementID into focus 
*/
function focusDomElement(elementID)
{
	var domElement = document.getElementById(elementID);

	if (domElement != null)
	{
		domElement.focus();
	}
}

// if user is not logged in then display required login message
// return true if we need to display the required login message
function checkUserRequiredLogin()
{
	var MEMBER_NOT_LOGGED_IN = 1;
	if (document.getElementById("memberID").value == MEMBER_NOT_LOGGED_IN)
	{
		showUserNotLoggedInDialog();
		return true;
	}

	return false;
}

function openNewWindowWithFocus(url)
{
	var oldWin = window.open(url, "myWindow"); 
	oldWin.focus(); 
}

// selects the option with the given value in the drop down list
function selectDropDownListOption(domDropDownList, optionValue)
{
	var domDropDownListOption;
	for (var x=0; x<domDropDownList.length; x++)
	{
		domDropDownListOption = domDropDownList.options[x];
		if (domDropDownListOption.value == optionValue)
		{
			domDropDownList.selectedIndex = x;
			break;
		}
	}
}

// set the default drop down list value given the dom id and value
function selectDropDownListOptionByDomID(domID, optionValue)
{
	var domObject = document.getElementById(domID);
	if (domObject != null)
	{
		selectDropDownListOption(domObject, optionValue);
	}
}

// set the textbox value
function setValueInDomTextbox(domID, value)
{
	var domObject = document.getElementById(domID);
	if (domObject != null)
	{
		domObject.value = value;
	}
}

// set the checkbox value
function setValueInDomCheckbox(domID, cbValue)
{
	var domObject = document.getElementById(domID);
	if (domObject != null)
	{
		if (cbValue == "True")
		{
			domObject.checked = true;
		}
	}
}

/*****************************
	functions for opening only one new window
*****************************/
var flyerWin;
// open a new window for flyer details
function openInSameNewWindowFlyer(url)
{

	// fixed problem when the page is served from server and window is still open
	if (flyerWin == null)
	{
		flyerWin = window.open(url,"flyerWin", "");
	}

	if(flyerWin != null)
	{ 
		try
		{
			// prevent closing the current window if it is the flyerWin
			if (window.top != flyerWin)
			{
				flyerWin.close();
			}
		}
		catch(e){}
	}
	flyerWin = window.open(url,"flyerWin", "");
}

var couponWin;
// open a new window for coupon details
function openInSameNewWindowCoupon(url)
{
	if (couponWin == null)
	{
		couponWin = window.open(url,"couponWin", "");
	}

	if(couponWin != null)
	{ 
		try
		{
			if (window.top != couponWin)
			{
				couponWin.close();
			}
		}
		catch(e){}
	}
	couponWin = window.open(url,"couponWin", "");
}

var outsideWin;
function openInSameNewWindowOutside(url)
{
	if (outsideWin == null)
	{
		outsideWin = window.open(url,"outsideWin", "");
	}

	if(outsideWin != null)
	{ 
		try
		{
			if (window.top != outsideWin)
			{
				outsideWin.close();
			}
		}
		catch(e){}
	}
	outsideWin = window.open(url,"outsideWin", "");
}

// ------------------ query strings ------------------
// if firstQsParameter is true then prepend "?" else "&"
function constructQsParameter(firstQsParameter, key, value)
{
	return (firstQsParameter == true ? "?" : "&") + key + "=" + value;
}