//********************************************************************
//*	       Name:	common_scripts.js
//*	Description:	JavaScript functions for cyberskin.com
//*	    Created:	03/29/2006
//*	    Updated:	04/20/2006
//********************************************************************

//********************************************************************
//* init()
//* Great window.onload hack from http://dean.edwards.name/
//* Takes care of funky screen remnants due to resizing DOM elements
//********************************************************************
function init() {
       // quit if this function has already been called
       if (arguments.callee.done) return;

       // flag this function so we don't do the same thing twice
       arguments.callee.done = true;

		//* Call the fix content height function
       fnFixContentHeight();
	   
		//==========================================================================================
		// if supported, initialize TransMenus
		//==========================================================================================
		// Check isSupported() so that menus aren't accidentally sent to non-supporting browsers.
		// This is better than server-side checking because it will also catch browsers which would
		// normally support the menus but have javascript disabled.
		//
		// If supported, call initialize() and then hook whatever image rollover code you need to do
		// to the .onactivate and .ondeactivate events for each menu.
		//==========================================================================================
		if (TransMenu.isSupported()) {
			TransMenu.initialize();
		}
   }

   /* for Mozilla */
   if (document.addEventListener) {
       document.addEventListener("DOMContentLoaded", init, null);
   }

   /* for Internet Explorer */
   /*@cc_on @*/
   /*@if (@_win32)
       document.write("<script defer src=_includes/ie_onload.js><"+"/script>");
   /*@end @*/

   /* for other browsers */
   window.onload = init;

//********************************************************************
//* fnFixContentHeight()
//* Compare the content column height with the optimal size.
//* If the content is shorter then it is automatically adjusted so it doesn't look like shit.
//********************************************************************
function fnFixContentHeight() {
	var objContainer = document.getElementById("Container");
	var objContent = document.getElementById('Copy');
	var intContainer;
	var intContent;
	var myHeight = 0;

	if (typeof(window.innerWidth) == '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;
	}
	
	// Get the height of the content column
	if (objContent.offsetHeight) {
		intContent = objContent.offsetHeight;
	}
	else if (objContent.style.pixelHeight) {
		intContent = objContent.style.pixelHeight;
	}
	
	// Get the height of the container
	if (objContainer.offsetHeight) {
		intContainer = objContainer.offsetHeight;
	}
	else if (objContainer.style.pixelHeight) {
		intContainer = objContainer.style.pixelHeight;
	}
	
	// Compare the two column heights.
	// If the content is shorter then fill out to match the sub nav
	if (intContainer < myHeight) {
		objContent.style.height = (myHeight - 330) + 'px';
	}
}

function fnWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

//********************************************************************
//* fnPopupWindow(strFileName)
//* Opens a new popup window
//* strFileName = The name of the page to display in the window
//* intWinWidth = The desired width of the popup window
//* intWinHeight = The desired height of the popup window
//********************************************************************
function fnPopupWindow(strFileName, intWinWidth, intWinHeight) {
	var winWidth = intWinWidth;
	var winHeight = intWinHeight;
	var scrWidth = (screen.width - winWidth)/2;
	var scrHeight = (screen.height - winHeight)/2;
	winPopup = window.open(strFileName, 'winPopup', 'width=' + winWidth + ', height=' + winHeight + ', top=' + scrHeight + ', left=' + scrWidth + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes');
	if (window.focus) {
		winPopup.focus();
	}
	return false;
}

//********************************************************************
//* fnFormFocus(objElement, blnEvent)
//* Changes the background color of a form element when it is in focus
//* objElement = The form element to change
//* blnEvent = Whether the element is in or out of focus
//********************************************************************
function fnFormFocus(objElement, blnEvent) {
	if (blnEvent == 0) {
		objElement.style.backgroundColor = '#FFFFFF';
	}
	else {
		objElement.style.backgroundColor = '#FFFFCC';
	}
}

//********************************************************************
//* fnCheckEmail(strEmail)
//* Check if the string is a valid e-mail address. Returns true or false.
//* strEmail = The string to validate
//********************************************************************
function fnCheckEmail(strEmail) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(strEmail)) {
		return true;
	}
	else {
		return false;
	}
}

//********************************************************************
//* fnCenterEnlargeLink()
//* Sets the div width to center the link to enlarge an item image 
//* based on that image's width
//********************************************************************
function fnCenterEnlargeLink() {
	var objItemImage = document.getElementById('ItemImage');
	var objEnlargeImage = document.getElementById('EnlargeImage');
	var intImageWidth;
	
	// Get the width of the item image
	if (objItemImage.offsetWidth) {
		intImageWidth = objItemImage.offsetWidth;
	}
	else if (objItemImage.style.pixelWidth) {
		intImageWidth = objItemImage.style.pixelWidth;
	}
	
	// Set the enlarge link width
	objEnlargeImage.style.width = intImageWidth + 'px';
}

//********************************************************************
//* fnEnlargeImage(strImageName)
//* Opens a larger version of a product image in a popup window
//* strImageName = The image to display
//********************************************************************
function fnEnlargeImage(strImageName) {
	var winWidth = 600;
	var winHeight = 420;
	var scrWidth = (screen.width - winWidth)/2;
	var scrHeight = (screen.height - winHeight)/2;
	winPopup = window.open('_includes/item_enlarged.asp?ID=' + strImageName, 'ItemEnlarged', 'width=' + winWidth + ', height=' + winHeight + ', top=' + scrHeight + ', left=' + scrWidth + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes');
	if (window.focus) {
		winPopup.focus();
	}
	return false;
}