function CPValue(s) {
    return (typeof(CP$value) != "undefined" && CP$value[s]) ? CP$value[s] : 0;
}

function PopUpMessage() {
    window.status="";
}

var doRatingsPopup = "false";
var popupMovieId = null;
var inRatingRedirectTest = "true";
var b_popup = (CPValue("*popup*") != 0);
var b_member = (CPValue("*member*") != 0);
var b_show800 = true; // site flag.
var s_titlecount = '30,000'; // constant.
var s_librange = '2-8'; // constant.
var s_maxlib = '8';   // constant.
var s_maxout = (CPValue("*maxout*") == 0) ? '3' : (''+CPValue("*maxout*"));
var s_shippingtime = '1-3'; // days - constant.
var ratedMovies = "";
var ratedRatings = "";


var RENT_BUTTON_SMALL   = 0;
var RENT_BUTTON_MEDIUM  = 10;
var RENT_BUTTON_LARGE   = 20;
var RENT_BUTTON_JUMBO   = 30;

window.name = '_nfk';

var appVers = navigator.appVersion;
var isMacIE = appVers.indexOf("Macintosh") != -1 && appVers.indexOf("MSIE 5") != -1;

/* Given a String, perhaps including units like "px", return the numeric bit */
function stripUnits(valueWithUnits) {
	return parseInt(valueWithUnits);
}

/* Iterate through an object and copy everything to a new one */
function getCopyOfObject(obj) {
	var newObj = new Object();
	for (var prop in obj) {
		newObj[prop] = prop.value;
	}
	return newObj;
}


function ratingsPop(link, movieid) {
    if (doRatingsPopup == "true") {
    	
        showRatingsPop(movieid);
    } else {
        window.location.href = link;
    }
}    

function trackRating (movieId, rating) {
    if (rating >= 3) {
	   ratedMovies = ratedMovies + movieId + ",";
	   ratedRatings = ratedRatings + rating + ",";
	}
}

function xferRating (targetForm) {
	targetForm.movies.value = ratedMovies;
	targetForm.ratings.value = ratedRatings;
}


function openAWindow() {
	window.open(PAGE_ROOT + "FirstRatingPopup?lnkctr=popRate&movieid=" + popupMovieId, "ratepop", "toolbars=0,scrollbars=0,location=0,statusbars=0,menubars=0,resizable=0,width=605,height=295");
}

function showRatingsPop(movieid) {
	popupMovieId = movieid;
	if (window.location.pathname.indexOf("/RateMovies") == -1) {
		
		setTimeout("openAWindow()", 500);
	}
    
}


if ((typeof IMAGE_ROOT) == "undefined") {
    IMAGE_ROOT = " ";
}

// Preload "clicked" rent and save buttons.
var rentButtonPath = IMAGE_ROOT + "pages/rent/";



//commerce popups

var queuePopUpTargetName = "nfQPop";

function rent(imgName, popupUrl) {
    // rbflip(imgName);
    checkForQueueAddCookie(0, popupUrl);
    return true;
}

function checkForQueueAddCookie(cyclesWaited, url) {
    var version = parseFloat(appVers);

    var windowParams = "toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=1,width=585,height=650";
    
    if (navigator.appName == "Netscape" && version >= 4 && version < 5) {
        // Skip cookie checks for Netscape 4.x, it doesn't work with the 204 technique.
        window.setTimeout("window.open('" + url + "', '" + queuePopUpTargetName + "', '" + windowParams + "');", 150);
    } else {
        if (typeof cyclesWaited == "undefined" || cyclesWaited == null) { 
            cyclesWaited = 0;
        }
        var cookies = document.cookie;
        var cookieName = "lastQAddResult";
        var theCookiePosition = cookies.indexOf(cookieName);
        var theCookieValuePostion = theCookiePosition + cookieName.length + 1; // Add one for the equals sign!
        var statusChar = "";
        if (theCookieValuePostion < cookies.length) {
            statusChar = cookies.charAt(theCookieValuePostion);            
        }
        var hasCookie = (theCookiePosition != -1 && (statusChar == "0" || statusChar == "1" || statusChar == "2" || statusChar == "3" || statusChar == "-" )); // Possible add status numbers, "-" covers all negative values.        
        if (hasCookie) { 
            // Since we have *a* cookie, check to see if it's the *right* one.
            var start = url.indexOf("movieid=") + 8;
            var end = url.indexOf("&", start);
            if (end == -1) {
                end = url.length;
            }
            var movieId = url.substring(start, end);
            if (cookies.indexOf(movieId, theCookieValuePostion) == -1) {
                hasCookie = false;
            }
        }
        if (hasCookie) {
            window.open(url, queuePopUpTargetName, windowParams);
        } else if (cyclesWaited < 60) {
            window.setTimeout("checkForQueueAddCookie(" + (cyclesWaited + 1) + ", '" + url + "');", 50);
        }
    }
}

function privPop(whereto) {
    window.open(whereto, 'nf_static_popup', 'toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=1,width=500,height=450,left=1,top=1');
}
	
function addressPop(whereto) {
    window.open(whereto, 'address_popup2', 'toolbars=0,scrollbars=0,location=0,statusbars=0,menubars=0,resizable=1,width=350,height=410,left=1,top=1');
}	

function cobrandPrivPop(whereto) {
	window.open(whereto, 'poppage_cobrand_link', 'toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=1,width=600,height=450,left=400,top=1');
} 


function stateAddress(whereto) {
    window.open(whereto, 'memberaddressedit', 'toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=0,width=425,height=490');
}  

function popupTermsandConditions(whereto) {
    window.open(whereto, "NF_Marquee_Terms_And_Conditions", "height=470,width=400,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no");
}    

function safePop(whereto) {
    window.open(whereto, 'poppop', 'toolbars=0,scrollbars=1,location=0,statusbars=0,menubars=0,resizable=0,width=367,height=450,left=1,top=1');
}

function lhcNavCheck(lhcForm) {
    
    var selIndex = document.forms[lhcForm].page.selectedIndex;
    var selValue = document.forms[lhcForm].page.options[selIndex].value;
    
    if (selValue == 0) {
        // A placeholder option was selected.
        if (selIndex == (document.forms[lhcForm].page.length - 1)) {
            // Final item, the dashed doodah. Stay here.
            document.forms[lhcForm].page.selectedIndex = 0;
        } else {
            // Fallback case for malformed items not at beginning or end.
            document.forms[lhcForm].page.selectedIndex = 0;
        }
    } else if ((selValue == "Recs") && (doRatingsPopup == "true")) {
        showRatingsPop();
    } else if (selIndex == 0) {
        // Title item. Proceed to generic page.
        document.forms[lhcForm].submit();
    } else {
        document.forms[lhcForm].submit();
    }
}

function searchCheck(searchForm) {

    var selValue = document.forms[searchForm].searchTxt.value;
    if (selValue == "") {
        document.location="http://www.cubehelper.com";
    } else {
        document.forms[searchForm].submit();   
    }
}

var dontFocus = "";
function focusAddressFormField() {

    if (document.login_form && (dontFocus == "")) {   
    document.login_form.email.focus();
    }

    else if (document.register_form && (dontFocus == "")) {   
    document.register_form.email.focus();
    }

    else if (document.addressEntry && (dontFocus == "")) {   
    document.addressEntry.fname.focus(); 
    }
  
}

function load_main(whereto) {
    if (! b_popup) {
        window.location.href = whereto;   
    } else if (window.opener) {
      window.opener.top.location.href = whereto; 		
      window.opener.focus();
      self.close();
  } else {
      window.open(whereto, '_nfk')
  }

}

function focusNClearInput(inputObject) {

    var hasBeenFocused = inputObject.getAttribute("hasBeenFocused") != null;
    
    if (!hasBeenFocused) {
        inputObject.setAttribute("hasBeenFocused", "true");
        inputObject.style.color = "#000000";
        inputObject.value = "";
        var thisForm = inputObject.form;
        for (ii = 0;  ii < thisForm.elements.length; ii++) {
            if (thisForm.elements[ii].type == "submit"){
                thisForm.elements[ii].disabled = false;
            }
        }
	
    }                                
}

function swapImage(imageName, newImage) {
    document.images[imageName].src = newImage;
}

function NF_preloadImages() {
  var d=document; if(d.images){ if(!d.NF_p) d.NF_p=new Array();
    var i,j=d.NF_p.length,a=NF_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.NF_p[j]=new Image; d.NF_p[j++].src=a[i];}}
}

function changeWindowTitle(title) {
    if (CP$value["*member*"]) {
        var t = title;
        document.title = t;
    }
}

function oopPop() {
    window.open("/Popup?id=5170",
                'oopPop',
                'toolbars=0,scrollbars=0,location=0,statusbars=0,menubars=0,resizable=0,width=350,height=250');
}

function ooppop() {
	oopPop();
}

button1 = new Image();
button1.src = IMAGE_ROOT + "buttons/one_moment_please.gif";
button2 = new Image();
button2.src = IMAGE_ROOT + "buttons/one_moment_please_small.gif";
var theForm;
var requestSubmitted = false;  

function disableButton(btn,form,buttonType) {
   if (!requestSubmitted){
        if (buttonType != null) {
           var buttonName = buttonType;
           btn.src = buttonName.src; // image swap happens here
        } else {
           var submitMessage = "  Please Wait...  ";
           btn.value = submitMessage;
        }
        theForm = form;
        btn.disabled = true;
        requestSubmitted = true;
        setTimeout("submitIt()", 250);
   } else {
        return false;
   }
}
function submitIt() {
    theForm.submit();
    return false;
}
function checkandsubmit(submitForm) {
    if(requestSubmitted == true) {
        return false;
    } else {
	    requestSubmitted = true;
	    submitForm.submit();
	    return false;
    }
}

function goToOnce(url) {
    if (requestSubmitted == true) {
		return false;
    } else {
	    requestSubmitted = true;
	    window.location.href=url;
	    return false;
    }
}

function popupTimer() {
    setTimeout("self.close()",120000);
}

// Dynamic Help toolkit

var answers = new Array();
var seeMoreLinks = new Array();
var shownAnswer = -1;
var isSafari = navigator.userAgent.indexOf("Safari") > -1 || navigator.userAgent.indexOf("AppleWebKit") > -1;
var isOpera = navigator.userAgent.indexOf("Opera") > -1;
function showAnswer(whichOne, totalShown, presentationId, faqId) {
	showInlineAnswer(whichOne, totalShown, presentationId, faqId, "ShowHelpAnswer");
}

function showSelfHelpAnswer(whichOne, totalShown, presentationId, faqId) {
	showInlineAnswer(whichOne, totalShown, presentationId, faqId, "ShowSelfHelpAnswer");
}

function showInlineAnswer(whichOne, totalShown, presentationId, faqId, url) {
    if (shownAnswer != -1) {
        var oldquestion = document.getElementById("faq" + shownAnswer + "q");
        var oldanswer = document.getElementById("faq" + shownAnswer + "a");    
        oldquestion.style.backgroundColor = "#FFFFEE";
        oldquestion.style.fontWeight = "normal";
        oldanswer.style.backgroundColor   = "#FFFFEE";
        oldanswer.innerHTML = seeMoreLinks[shownAnswer];
    }
    
    var question = document.getElementById("faq" + whichOne + "q");
    var answer = document.getElementById("faq" + whichOne + "a");
    question.style.backgroundColor = "#FEFEC0";
    question.style.fontWeight = "bold";
    answer.style.backgroundColor   = "#FEFEC0";
    answer.innerHTML = answers[whichOne];
    
    var base = document.getElementById("dynhelpsbbase");
    if (base && typeof base != "unknown") {
	    if (whichOne+1 == totalShown) {
	    	base.style.backgroundColor = "#FEFEC0";
	    } else {
	    	base.style.backgroundColor = "#FFFFEE";
	    }
	}
    
    shownAnswer = whichOne;
    
    if (!isSafari) {
        window.location.href = "http://" + window.location.hostname + "/" + url + "?dpid=" + presentationId + "&qid=" + faqId + "&faqPos=" + (whichOne + 1) + "&faqTotal=" + totalShown + "&ncok=y";
    }
}

// Global Dropdown Menu
var openMenu = null;
var canMenuBeClosed = true;
var menuTop =  null;
var menuBottom = null;
var menuLeft = null;
var menuRight = null;
var wWidth = null;

function showMenu(id, objPos){
	if(document.getElementById){

			document.onmousemove = null;
			setElementProperty('profiles', 'display', 'none');
			openMenu = id;
			canMenuBeClosed = false
	
			var x = 0;
			var y = 0;
			
			x = getElementLeft(objPos);
			y = getElementBottom(objPos);
			
			
			wWidth = document.body.offsetWidth;
			document.onmousemove = getMousePosition;
			
			setElementProperty(id, 'display', 'block');
			setElementProperty(id, 'left', x + "px");
			setElementProperty(id, 'top', y + "px");
		
	}
}
function hideMenu(id){
	setElementProperty(id, 'display', 'none');
	document.onmousemove = null;
	window.status = "";
	openMenu = null;
	menuTop = null;
	menuBottom = null;
	menuLeft = null;
	menuRight = null;
	wWidth = null;
}
function getMousePosition(event){
	var x, y;
	if(window.event){
		x = window.event.clientX;
		y = window.event.clientY;
		if (document.documentElement && document.documentElement.scrollTop){
			y+=document.documentElement.scrollTop;
		} else if(self.pageYOffset){
			// mainly for safari. It doesn't handle body.scrolltop well.
			// The above will catch most everything. The bottom will catch all explorers.
		} else {
			y+=document.body.scrollTop;
		}
	} else {
		x = event.pageX;
		y = event.pageY;
	}
	if(openMenu != null){
		var testInside = isInside(x, y, openMenu);
		if((wWidth != document.body.offsetWidth) || (!testInside && canMenuBeClosed == true)){
			hideMenu(openMenu);
		}
		if(testInside){
			canMenuBeClosed = true;
		}
	}
}

function isInside(xMouse, yMouse, id){

	menuLeft = menuLeft == null ? getElementLeft(id) : menuLeft;
	menuRight = menuRight == null ? getElementRight(id) : menuRight;
	menuTop = menuTop == null ? getElementTop(id) : menuTop;
	menuBottom = menuBottom == null ? getElementBottom(id) : menuBottom;
		
	if( (id != null) && (xMouse >= menuLeft - 30) && (xMouse <= menuRight + 30) && (yMouse >= menuTop - 30) && (yMouse <= menuBottom + 30) ){
		return true;
	} else {
		return false;
	}
}

function getElementLeft(id) {
	var x = 0;
	var menuDiv = typeElement(id);
	while (menuDiv != null) {
		x+= menuDiv.offsetLeft;
		menuDiv = menuDiv.offsetParent;
	}
	return parseInt(x);
}

function getElementWidth(id){
	var menuDiv = typeElement(id);
	return parseInt(menuDiv.offsetWidth);
}

function getElementRight(id){
	return getElementLeft(id) + getElementWidth(id);
}

function getElementTop(id) {
	var y = 0;
	var menuDiv = typeElement(id);
	while (menuDiv != null) {
		y+= menuDiv.offsetTop;
		menuDiv = menuDiv.offsetParent;
	}
	return parseInt(y);
}

function getElementHeight(id){
	var menuDiv = typeElement(id);
	return parseInt(menuDiv.offsetHeight);
}

function getElementBottom(id){
	return getElementTop(id) + getElementHeight(id);
}

function setElementProperty(id, id_property, id_value){
	var menuDiv = typeElement(id);
	if((menuDiv != null) && (menuDiv.style != null)){
		menuDiv = menuDiv.style;
		menuDiv[ id_property ] = id_value;
	}
}
function typeElement(id) {
	var type = null;
	if(typeof(id) == "object"){
		type = id;
	} else {
		type = document.getElementById(id);
	}
	return (type);
}
function callOnWindowLoad(func){
	if(!window.onloadFunctions){
		onloadFunctions = [];
	}
	onloadFunctions[onloadFunctions.length] = func;
}
window.onload = function(){
	if(window.onloadFunctions){
		for(var i = 0; i < onloadFunctions.length; i++){
			onloadFunctions[i]();
		}
	}
};

// Simple but effective textarea character limiter
function textAreaLimiter(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
}
	