/**
 * @overview General purpose utility functions with no dependency on any global variables.
 */


/**
 * Swaps out the source of an image; used mostly for rollover effects.
 * @param {String} id The id of the image element
 * @param {String} name The name of an image object
 * @example	&lt;IMG src="SureFlixPowerU.gif" name="NSureFlixPower" 
 * 	onmouseout="di('NSureFlixPower','ISureFlixPowerU');" 
 * 	onmouseover="di('NSureFlixPower','ISureFlixPowerO');" /&gt;
 *  //<A href=http://alpha.sureflix.com/work/ClientSamples/di.htm target=_blank>sample code</A>
 *  @author Erik 1/1/2001
 */
function di(id,name) {
	if (document.images) {
		document.images[id].src=eval(name+".src");
	}
}
	
/** 
 * Returns the document object inside a frame (in a frameset) with the given id.
 * @example	GetFrameDocument("PPMPlayer").location = "Home.asp";
 * @param	{string} idAsName The id of the frame object, this works in both IE and Firefox
 * @return	{obj} Returns a document element
 */
function GetFrameDocument(idAsName)
{
	return GetFrame(idAsName).document;
}


/**
 * Returns the frame object in a frameset with the given id.
 * @example GetFrame("PPMSideBar").AddToPlaylist(movie,scene,studio,tokens);
 * @param	{string} idAsName The id of the frame object, this works in both IE and Firefox
 * @return	{obj} Returns a window element	
 * @author Erik 10/1/2007
 */
function GetFrame(idAsName) {
	var frame = top.$(idAsName);
	if (frame == null) {
		return null;
	} else {
		return frame.contentWindow;
	}
}


/** 
 * Returns the value of the given parameter in the query string of the current page.
 * @param	{string} qsKey The key to look for in the query string
 * @return	{string} returns the value of the given parameter
 * @example	GetQSParam("Category")   //<A href=http://alpha.sureflix.com/work/ClientSamples/GetQSParam.htm?Category=Comedy&Theme=Standup target=_blank>sample code</A>
 * @author Erik 1/1/2005
 * @
 */
function GetQSParam(qsKey) {
	var qsValue = location.toString().toQueryParams()[qsKey];
	if (qsValue !== undefined && qsValue != "undefined" && qsValue !== null && qsValue != "null") {
		return(qsValue);
	} else {
		return("");
	}
}


/** 
 * Returns the value of a key from a query-string formatted string.
 * @param	{string} qsKey The key to look for in the string
 * @param	{string} qsString A qs formatted string
 * @return	{string} returns the value of the given key
 * @example	GetQSValue("Page.asp?CB=FFFFFF&CF=999999", "CB")   //returns "FFFFFF" <A href=http://alpha.sureflix.com/work/ClientSamples/GetQSValue.htm target=_blank>sample code</A>
 * @author Erik 01/01/2005
 */
function GetQSValue(qsKey, qsString) {
	var re = new RegExp(qsKey + "=([^&]+)", "i");
	matchArray = qsString.match(re);

	if (matchArray === null) {
		return("");
	} else {
		var qsValue = matchArray[1].toString();
		qsValue = qsValue.replace(/\+/, " ");
		return(unescape(qsValue));
	}
}


/**
 * Embeds a flash object in the page. 
 * @param {String} imgID The unique ID of the Flash object element
 * @param {Number} imgW The width of the Flash element
 * @param {Number} imgH The height of the Flash element
 * @param {String} flashPath The path to the Flash file, usually on the cdn
 * @param {String} flashURL Imports "sUrl" as a root level variable to the movie which the movie can use to link to when clicked 
 * @param {String} staticURL Static image display if flash is not installed
 * @param {Boolean} isIFrame True if the movie is being loaded in an IFRAME
 * @example InsertFlash("HomeBanner-FlashTest-Img01", 460, 180, 
 * 	"http://published.sureflix.com/ads/Articles/HomeBanner/HomeBanner-FlashTest-Img04.swf", 
 * 	"StudioList.asp?Studio=Matt+Sterling", 
 * 	"http://published.sureflix.com/ads/Articles/HomeBanner/HomeBanner-FlashTest-Img03.jpg");
 * 	//<A href=http://alpha.sureflix.com/work/ClientSamples/InsertFlash.htm target=_blank>sample code</A>
 * @author Nestor 1/1/2004
 */
function InsertFlash(imgID, imgW, imgH, flashPath, flashURL, staticURL, isIFrame)
{
	Sureflix.UI.InsertFlash(imgID, imgW, imgH, flashPath, flashURL, staticURL, isIFrame);
}
	
/**
 * Encodes the give string so that it can be passed as a query string parameter
 * @param {String} sStr The original string.
 * @return {String} The URL encoded string.
 * @example var newURL = EditQSParam(oldURL, "Source", URLencode("Category.asp?Category=New"));
 */
function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
}


/**
* Forces a refresh of the browser window
* @author Erik 05/24/2008
*/
function RefreshWindow() {
	d = new Date();
	// We have to add the date stamp to force the browser to load a new page every time instead of using the cache
	window.location.href = window.location.href + "&RefreshDate=" + d.toString();
}



/**
* Returns the viewport's width
* @return {Integer} Width of the viewport
* @author Hernan 11/05/2007
* @author Erik 05/24/2008 changed from getWindowSize() to GetWindowWidth()
*/
function GetWindowWidth() {
  var myWidth = 0;
  
  if(typeof( window.innerWidth ) === 'number') {
    //Non-IE
    myWidth = window.innerWidth;
  } else if(document.documentElement && document.documentElement.clientWidth) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if(document.body && document.body.clientWidth) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}


/**
* Returns the viewport's height
* @return {Integer} Height of the viewport
* @author Hernan 11/05/2007
* @author Erik 05/24/2008 changed from getWindowSize() to GetWindowHeight()
*/
function GetWindowHeight() {
  var myHeight = 0;
  
  if(typeof(window.innerHeight) === 'number') {
    //Non-IE
    myHeight = window.innerHeight;
  } else if(document.documentElement && document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if(document.body && document.body.clientHeight) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}


/**
* Returns the top windows's width
* @return {Integer} Width of the window
* @author Erik 07/29/2008
*/
function GetTopWindowWidth() {
  var myWidth = 0;
  var topWindow = window.top;
  
  if(typeof( topWindow.innerWidth ) === 'number') {
    //Non-IE
    myWidth = topWindow.innerWidth;
  } else if(topWindow.document.documentElement && topWindow.document.documentElement.clientWidth) {
    //IE 6+ in 'standards compliant mode'
    myWidth = topWindow.document.documentElement.clientWidth;
  } else if(topWindow.document.body && topWindow.document.body.clientWidth) {
    //IE 4 compatible
    myWidth = topWindow.document.body.clientWidth;
  }
  return myWidth;
}


/**
* Returns the top windows's height
* @return {Integer} Height of the window
* @author Erik 07/29/2008
*/
function GetTopWindowHeight() {
  var myHeight = 0;
  var topWindow = window.top;

  if(typeof(topWindow.innerHeight) === 'number') {
    //Non-IE
    myHeight = topWindow.innerHeight;
  } else if(topWindow.document.documentElement && topWindow.document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    myHeight = topWindow.document.documentElement.clientHeight;
  } else if(topWindow.document.body && topWindow.document.body.clientHeight) {
    //IE 4 compatible
    myHeight = topWindow.document.body.clientHeight;
  }
  return myHeight;
}


/**
* Returns the horizontal position of the browser window
* @return {Integer} Left position of the browser window
* @author Erik 05/24/2008
*/
function GetWindowLeft() {
	return window.screenLeft ? window.screenLeft : window.screenX;
}


/**
* Returns the horizontal position of the browser window
* @return integer Left position of the browser window
* @author Erik 05/24/2008
*/
function GetWindowTop() {
	return window.screenTop ? window.screenTop : window.screenY;
}


/**
* Resizes the browser window
* @param {Integer} wantedWidth The desired width, or 0 for no change
* @param {Integer} wantedHeight The desired height, or 0 for no change
* @author Erik 05/24/2008
*/
function ResizeWindowTo(wantedWidth, wantedHeight) {
	var currentWidth;
	var currentHeight;
	var diffWidth;
	var diffHeight;
	
	//we use a for loop to avoid an infinite loop
	for (var i=0; i<=20; i++) {
		currentWidth = GetTopWindowWidth();
		currentHeight = GetTopWindowHeight();
		
		//if null or 0 is passed, do not resize in that direction
		if (!wantedWidth) {
			diffWidth = 0;
		} else {
			diffWidth = wantedWidth-currentWidth;
		}
		
		if (!wantedHeight) {
			diffHeight = 0;
		} else {
			diffHeight = wantedHeight-currentHeight;
		}
		
		if (diffWidth == 0 && diffHeight == 0) {
			break;
		}
		
		window.top.resizeBy(diffWidth, diffHeight);
	}
}


function MaximizeWindow() {
	window.moveTo(0, 0);
	window.resizeTo(screen.availWidth, screen.availHeight);
}


function ResizeElement(p_element, p_width, p_height) {
	// Resize the given html element to the given width and height
	// Pass null for the width or height to leave it as is
	// Can be easily combined with FindElement:
	//    ResizeElement(FindElement("MyElement"), 20, 20)
		
	// 07/18/2006 Amy: js error fix, we can't give an element a negative height/width
	if (p_width < 0) {
		p_width = 0;
	}
	if (p_height < 0) {
		p_height = 0;
	}
	// 07/18/2006 Amy: End
		
	if (p_element.style.pixelWidth) {
		if (p_width !== null) {
			p_element.style.pixelWidth = p_width;
		}
		if (p_height !== null) {
			p_element.style.pixelHeight = p_height;
		}
	} else {
		if (p_width !== null) {
			p_element.style.width = p_width;
		}
		if (p_height !== null) {
			p_element.style.height = p_height;
		}
	}
}
	
	
function MoveElement(p_element, p_left, p_top) {
	// Move the given html element to the given coordinates
	// Pass null for the left or top to leave it as is
		
	if (p_element.style.pixelLeft) {
		if (p_left !== null) {
			p_element.style.pixelLeft = p_left;
		}
		if (p_top !== null) {
			p_element.style.pixelTop = p_top;
		}
	} else {
		if (p_left !== null) {
			p_element.style.left = p_left;
		}
		if (p_top !== null) {
			p_element.style.top = p_top;
		}
	}
}
	
	
function GetElementCoord(p_element, p_coord) {
	// Get the requested coordinate of the given html element
	// The coordinate can be "left", "top", "width", "height"
	
	if (p_element.style.pixelLeft) {	
		if (p_coord == "left") {
			return p_element.style.pixelLeft;
		}
		else if (p_coord == "top") {
			return p_element.style.pixelTop;
		}
		else if (p_coord == "width") {
			return p_element.style.pixelWidth;
		}
		else if (p_coord == "height") {
			return p_element.style.pixelHeight;
		}
	} else {
		if (p_coord == "left") {
			return p_element.style.offsetLeft;
		} else if (p_coord == "top") {
			return p_element.style.offsetTop;
		} else if (p_coord == "width") {
			return p_element.style.width;
		} else if (p_coord == "height") {
			return p_element.style.height;
		}
	}
}
	
	
function FindElement(p_elementId) {
	var oItem;
	if (document.all) {
		oItem = document.all[p_elementId];
	} else if (document.getElementById) {
		oItem = document.getElementById(p_elementId);
	} else {
		oItem = null;
	}
		
		
	if (oItem + "" == "undefined") {
		oItem = null;
	}
	return(oItem);
}
	
function EnterValidate(keyStroke) {
	//key = (isNav ? keyStroke.which : window.event.keyCode);
	key = (window.event ? window.event.keyCode: keyStroke.which);
	if (key == 13) {
		Validate();
		return(false);
	}
	else {
		return(true);
	}
}
	
// 07/18/2006 Amy: Moved this function from frame.inc
function FIND(item) {
	return(document.getElementById(item));
}
// 07/18/2006 Amy: End


// 08/21/2006 Amy: Added function
//
//	This function depends on SendBack
function HandleJSErrors() {
	var isBeta = false;
	var url = window.location.href;
	var web = url.substring(url.indexOf("://") + 3, url.indexOf("."));
	web = web.toLowerCase();
		
	if(web.indexOf("work") != -1 || web.indexOf("alpha") != -1 || web.indexOf("beta") != -1 || url.indexOf("localhost") != -1 ){
		isBeta = true;
	}
		
	// we only want to save to the database on live
	if (!isBeta) {
		try {
			window.onerror = function(msg, url, line) {	
				var queryString = "What=SaveJSError&Error=" + msg + "&FileName=ClientCommon.js&URL=" + url + "&LineNumber=" + line;
				//debug
				//window.open("SendBack.asp?" + queryString);
				Sureflix.SendBack(queryString);
				return true;
			}
		} catch(err) {
			throw(err);
		}	
	}
}
// 08/21/2006 Amy: End
	

	
function HideCtr(ctrId)
{
	var ctr = document.getElementById(ctrId);
	if (ctr != null)
	{
		ctr.style.display = 'none';
	}
}
	
//Show element
function ShowCtr(ctrId)
{
	var ctr = document.getElementById(ctrId);
	if (ctr != null)
	{
		ctr.style.display = '';
	}
}

/**
 *	Checks for an empty string or a null value
 *	@param _value: The value to check
 *	@return True if is empty, false if it isn't
 */
function IsEmpty(_value) {
	var thisVal = _value + "";
	thisVal	= thisVal.toLowerCase();

	if (thisVal == "" || thisVal == "null" || thisVal == null || thisVal == "undefined") {
		return true;
	}
	return false;
}

