




utilityBrowserVer = parseInt(navigator.appVersion);

function imgOn(imgName) {
  	if (utilityBrowserVer >= 3) {
		imgOnString = eval(imgName + "_on.src");
		document.images[imgName].src = imgOnString;
	}
}

function imgOff(imgName) {
	if (utilityBrowserVer >= 3) {
		imgOffString = eval(imgName + "_off.src");
		document.images[imgName].src = imgOffString;
	}
}

function goToLink(address) {
	var linkURL = address.options[address.selectedIndex].value;
	window.top.location.href = linkURL;
	address.selectedIndex=0;
}

function openWindow(address, width, height) {
	var newWindow = window.open(address, 'Popup_Window', 'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	newWindow.focus();
}

function confirmWindow(url, text) {

	if (confirm(text)) {
		window.go = url;
		window.location = url;
	}
}

/**************************************************
DESCRIPTION:
This function opens a help layer with the following parms.
    layerID = The id of the layer to be displayed. (required)
    layerWidth = The width of the layer to be displayed. (required on open)
    imgObj = The name of the image. (required)
    layerAlign = The alignment of the layer to be opened. (optional-"left","center","right")
                 (If not passed, the layer will be aligned to the right of the image.)
**************************************************/
function openCloseLayer(layerID,layerWidth,imgObj,layerAlign) {
	var offsetX = 0;
 	var offsetY = 0;
	if (document.getElementById) {
		if (document.getElementById(layerID).style.display != "none") {
			document.getElementById(layerID).style.display = "none";
		} else {
			if (imgObj != null) {
				//Set the offsets based on the alignment selected.
				if (layerAlign == 'left') {
					offsetX = layerWidth+5;
					offsetY = -8;
				} else if (layerAlign == 'center') {
					offsetX = layerWidth/2;
					offsetY = imgObj.height;
				} else {
					offsetX = offsetX-(imgObj.width+5);
					offsetY = -8;
				}
				//Determine the position of the image.
				el = imgObj;
				var y = el.offsetTop;
				var x = el.offsetLeft;
				var topEl = topEl = el.offsetParent;
				var leftEl = leftEl = el.offsetParent;
				for( ; topEl; topEl = topEl.offsetParent ) y += parseInt(topEl.offsetTop);
				for( ; leftEl; leftEl = leftEl.offsetParent ) x += parseInt(leftEl.offsetLeft);
				//Set the layer position from the top of the page.
				document.getElementById(layerID).style.top = y+offsetY;
				//Set the layer position from the left of the page.
				document.getElementById(layerID).style.left = x-offsetX;
				//Display the layer.
				document.getElementById(layerID).style.display = "block";
			}
		}
	}
}

function setOperation(opType, my_form_name) {
	// Get a pointer to the form for this item
	my_form = document.forms[my_form_name];
	my_form.operation.value = opType;
}

/*
This function opens a new window.
*/
function openNewWindow(URL, winWidth, winHeight, popUpWin, windowName, leftPos, topPos) {
	// Default window width and height, if not passed in.
	var w = 800, h = 600;

	//Check browser is IE.
	if (document.all) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	else {
		w = window.innerWidth;
		h = window.innerHeight;
	}

	//Check if window width was passed. If so, use that width.
	if (winWidth != null) {
		var popW = winWidth;
	}
	else {
		var popW = (w - 100);
	}

	//Check if window height was passed. If so, use that height.
	if (winHeight != null) {
		var popH=winHeight;
	}
	else {
		var popH=(h-100);
	}

	//Check if window should be opened as popup.
	if (popUpWin != null) {
		var noPopUp = 'yes';
		if (popUpWin.toLowerCase() == 'y' || popUpWin.toLowerCase() == 'yes') {
			noPopUp = 'no';
		}
	}
	else {
		var noPopUp = 'yes';
	}

	// Create the window name, if not passed in.
	if (windowName != null) {
		// Use the window name passed in.
		var winName = windowName;
	} else {
		//Generate window name with random number (ie 'win274').
		var winName = 'win'+Math.floor(Math.random()*1000);
	}

	// Set the window positions.
	if ((leftPos == null) || (leftPos == "undefined")) {
		leftPos = (w - popW) / 2;
	}
	if ((topPos == null) || (topPos == "undefined")) {
		topPos = (h - popH) / 2;
	}

	// Open the new window.
	newWindow = window.open(URL,winName,'width='+popW+',height='+popH+',top='+topPos+',left='+leftPos+',menubar='+noPopUp+',location='+noPopUp+',directories='+noPopUp+',fullscreen=no,resizable='+noPopUp+',scrollbars=yes,status=no,titlebar=yes,toolbar='+noPopUp);

	return newWindow;
}

/*
This function opens a window style layer with the following parms.
	 layerId = The id of the layer to be displayed. (required)
	 titleLayerId = The id of the title layer to be positioned relative to the layer.
						 (If not passed or passed as null or empty, the layer will not be controlled.)
	 bgLayerId = The id of a background/additional layer to be displayed/hidden with the main layer.
				    (If not passed or passed as null or empty, the layer will not be controlled.)
	 hideSelects = Boolean specifying whether or not select boxes should be hidden on open/close. (required)
	 layerWidth = The width of the layer to be displayed. (required on open)
	 layerHeight = The height of the layer to be displayed. (required on open)
	 topPosition = The veritcal position of the layer to be opened.
						(If not passed, the layer will be positioned in the middle of the page.)
	 leftPosition = The horizontal position of the layer to be opened.
						(If not passed, the layer will be positioned in the center of the page.)
*/
function openLayerWindow(layerId,titleLayerId,bgLayerId,hideSelects,layerWidth,layerHeight,topPosition,leftPosition) {
	var offsetX = 0;
	var offsetY = 0;
	if (document.getElementById) {
		if (document.getElementById(layerId).style.display != "none") {
			document.getElementById(layerId).style.display = "none";
			if (titleLayerId != null && titleLayerId != "") {
				document.getElementById(titleLayerId).style.display = "none";
			}
			if (bgLayerId != null && bgLayerId != "") {
				document.getElementById(bgLayerId).style.display = "none";
			}
			if (hideSelects) {
				showHiddenSelects();
			}
		} else {
			if (hideSelects) {
				hideSelectObjects();
			}

			var w = 800, h = 600;
			//Check if browser is IE.
			if (document.all) {
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			} else {
				w = window.innerWidth;
				h = window.innerHeight;
			}
			//Check if the layer width was passed. If so, use that width.
			if (layerWidth != null) {
				var popW = layerWidth;
			}
			else {
				var popW = (w - 100);
			}
			//Check if the layer height was passed. If so, use that height.
			if (layerHeight != null) {
				var popH = layerHeight;
			}
			else {
				var popH = (h - 100);
			}

			if (topPosition != null)
				var topPos = topPosition;
			else
				var topPos = (h - popH) / 2;

			if (leftPosition != null)
				var leftPos = leftPosition;
			else
				var leftPos = (w - popW) / 2

			var theLayer = document.getElementById(layerId);
			theLayer.style.top = topPos;
			theLayer.style.left = leftPos;
			theLayer.style.width = layerWidth;
			theLayer.style.height = layerHeight;
			theLayer.style.display = "block";

			if (titleLayerId != null && titleLayerId != "") {
				var titleLayer = document.getElementById(titleLayerId);
				titleLayer.style.top = topPos + 2;
				titleLayer.style.left = leftPos + 5;
				titleLayer.style.display = "block";
			}
			if (bgLayerId != null && bgLayerId != "") {
				var bgLayer = document.getElementById(bgLayerId);
				bgLayer.style.width = w;
				bgLayer.style.height = h;
				bgLayer.style.display = "block";
			}
		}
	}
}

var selectObjectsHidden;
function hideSelectObjects() {
	var selectObjects = document.getElementsByTagName("select");
	var arraySize = selectObjects.length;
	selectObjectsHidden = new Array(arraySize);
	var hiddenCounter = 0;
	for (var i = 0; i < arraySize; i++) {
		if (selectObjects[i].style.visibility == "") {
			selectObjectsHidden[hiddenCounter] = selectObjects[i];
			hiddenCounter++;
			selectObjects[i].style.visibility = "hidden";
		}
	}
}

function showHiddenSelects() {
	if (selectObjectsHidden) {
		var arraySize = selectObjectsHidden.length;
		for (var i = 0; i < arraySize; i++) {
			if (selectObjectsHidden[i].style.visibility == "hidden") {
				selectObjectsHidden[i].style.visibility = "";
			}
		}
	}
}

function goCheckout(){
	 
		  document.CartContentForm.action='https://www.timelife.com/checkout/update_items_in_order.cmd?goShipto=Y';
		  document.CartContentForm.submit();
		  return true;
 }

function popupWin(key){
 //Open a window
 if (key == "shippingRules")
	  window.open("../includes/shipping_rules.jsp?",null,"height=370,width=560,status=yes,toolbar=no,menubar=no,scrollbars=yes,location=no");
 else if(key == "installmentRules")
	  window.open("../includes/installment_rules.jsp?",null,"height=370,width=450,status=yes,toolbar=no,menubar=no,location=no");
 else
	  window.open("../includes/coupon_code.jsp?",null,"height=370,width=450,status=yes,toolbar=no,menubar=no,location=no");
 }
 

function openwindow( link ) {
	window.open(link,null,"height=370,width=560,status=yes,toolbar=no,menubar=no,scrollbars=yes,location=no");
}



function setPageTitle(pageTitle) {
	document.title = pageTitle;
}

function trim(s) {
 var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
 return (m == null) ? "" : m[1];
}

function GetTextWidth(my_form, width){
	var my_width = width -1;

	if (my_form.value.length > my_width) {
		my_form.value = my_form.value.substring(0,my_width);
	}
}

function isEmail(email) {
	email = trim(email);
	if (email.indexOf(" ") >= 0)
		return false;
	var index = email.indexOf("@");
	if (index > 0) {
		var dot = email.indexOf(".", index);
		if ((dot > index + 1) && (email.length > dot + 1))
			return true;
	}
	return false;
}


/*  MaxG: 2004/01/06
This function unescape the encoded string, and replace the plus sign(+) with a space
*/
function encodeReplace(str){
     var result = unescape(str);
     result = stringReplace(result, "+", " ");
     return result;
}

/*
This function replaces all instances of a value in a string with the following parms.
	NOTE: Special characters such as ' and / will need to be passed in as \' and \/.
	string = The value to be searched on. (required)
	match = The value to find in the string. (required)
	replacement = The to replace the match with. (required)
*/
function stringReplace(string, match, replacement) {
	var result = '';
	var index = 0;
	var lastIndex = index;
	while (string.length > lastIndex) {
		index = string.indexOf(match, lastIndex);
		if (index == -1) { break }
			result += string.substring(lastIndex, index) + replacement;
			lastIndex = index + match.length;
	}
	result += string.substring(lastIndex, string.length);
	return result;
}

/*
This function replaces all instances of a value in a string with the following parms.
	NOTE: Special characters such as ' and / will need to be passed in as \' and \/.
	string = The value to be searched on. (required)
	match = The value to find in the string. (required)
	replacement = The to replace the match with. (required)
*/
function stringReplace(string, match, replacement) {
	var result = '';
	var index = 0;
	var lastIndex = index;
	while (string.length > lastIndex) {
		index = string.indexOf(match, lastIndex);
		if (index == -1) { break }
			result += string.substring(lastIndex, index) + replacement;
			lastIndex = index + match.length;
	}
	result += string.substring(lastIndex, string.length);
	return result;
}




/********	Phone Validation.	*********/
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validatePhoneNumber( phoneNumber ){

	if( (phoneNumber==null) || (phoneNumber=="") ) {
		return false
	}
	if( checkInternationalPhone(phoneNumber) == false ) {
		return false
	}
	return true
 }
/********	Phone Validation.	*********/