var whitespace = " \t\n\r";

function addslidepopups() {
	if (document.getElementById && document.createTextNode) {
		var atags = document.getElementsByTagName ('a');
		for (var i=0;i<atags.length;i++) {
			if (atags[i].getAttribute('target') && atags[i].getAttribute('target')=='slides') {
				atags[i].onclick = slidepop;
			}
		}
	}
}

function addContactPops() {
	if (document.getElementById && document.createTextNode) {
		var atags = document.getElementsByTagName ('a');
		for (var i=0;i<atags.length;i++) {
			if (atags[i].getAttribute('target') && atags[i].getAttribute('target')=='contact') {
				atags[i].onclick = contactpop;
			}
		}
	}
}

function slidepop() {
	var target=this.href;
	var slidepopwin = window.open(target, "slidepopwin", "width=330, height=400, resizable=yes, status=no, toolbar=no, menubar=no, scrollbars=no");
	slidepopwin.focus();
	return false;
}

function contactpop() {
	var target=this.href;
	var slidepopwin = window.open(target, "slidepopwin", "width=370, height=500, resizable=yes, status=no, toolbar=no, menubar=no, scrollbars=yes");
	slidepopwin.focus();
	return false;
}
 
function fillPageHeight() {
	if (document.getElementById && document.createTextNode) {
		if (document.getElementById("navigation")) {
			objNavigation = document.getElementById("navigation");
			navHeight = objNavigation.scrollHeight
		
			objSidebar = document.getElementById ("sidebar");
			if (objSidebar) {
				if (objSidebar.scrollHeight && objSidebar.scrollHeight != "undefined") {
					sideHeight = objSidebar.scrollHeight;
				} else {
					sideHeight=0;
				}
			} else {
				sideHeight = 0;
			}
		
			if (document.getElementById ("mainContentHole")) {
				objMainContentHole = document.getElementById ("mainContentHole");
			} else if (document.getElementById ("contentHole")) {
				objMainContentHole = document.getElementById ("contentHole");
			}
			
			holeHeight=0;	// default value
			if (objMainContentHole.scrollHeight && objMainContentHole.scrollHeight != "undefined") {	// "undefined" thing for IE mac 5.2
				holeHeight = objMainContentHole.scrollHeight;
			} else if (objMainContentHole.clientHeight) {
				holeHeight = objMainContentHole.clientHeight;
				// alert ("using clientHeight"); // debug
			}
			maxSideColumn = Math.max(sideHeight, navHeight)
			// alert (holeHeight + ", " + navHeight + ", " + sideHeight + ", " + maxSideColumn ); // debug
			if (holeHeight < maxSideColumn) {
				objMainContentHole.style.height = maxSideColumn+"px";
				objMainContentHole.style.minHeight = maxSideColumn+"px";
			}
		}
	}
}

function addToRent(title, runtime, format, notes, mlink, isRecommended) {
	title = escape(title);
	format = escape(format);
	mlink = escape(mlink);
	notes = escape(notes);
	
	searchURL = "?title=" + title;
	searchURL += "&runtime=" + runtime;
	searchURL += "&format=" + format;
	if (isRecommended) {
		searchURL += "&recby=Movie%20Habit";
	} else {
		searchURL += "&recby=%20";
	}
	searchURL += "&notes=" + notes;
	searchURL += "&mlink=" + mlink;
	if (isRecommended) {
		searchURL += "&os=" + 1;
	} else {
		searchURL += "&os=" + 0;
	}

	// set the popup window
	var popurl="/rentlist/adddialog.php" + searchURL;
	winpops=window.open(popurl,"moviehabit","width=400,height=338,scrollbars,resizable")
	winpops.focus();
}

function getCookie (Key) {

	cookieKey = Key
	theCookie = document.cookie 
	
	i = theCookie.indexOf(cookieKey)
	if ( i >= 0 )	
		{
			i += cookieKey.length +1
			j = theCookie.indexOf(";",i-1)
	
			if (j>1) { len = j-i } else { len = 1000 }
			cookieValue = theCookie.substr(i,len)
		}

     if (i <= 0 || cookieValue.length < 1 ) {
		return false
	} else {
		return cookieValue
	}
}

function goRent () {
	cookieKey = "uemail"
	cookieValue = getCookie (cookieKey);
	if (cookieValue) {
		location.href = "/rentlist/showlist.php";
	} else {
		location.href = "/rentlist/login.shtml";
	}
	return false;
}

function isEmail (s)
{
	if (isEmpty(s))
		return false;

    // is s whitespace?
    if (isWhitespace(s))
    	return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@"))
    	return false;
    else
    	i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    {
		i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != "."))
    	return false;
    else
    	return true;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}

function isWhitespace(s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1)
        	return false;
    }
    return true;
}

function validateEmail(strEmail) {
	if (!isEmail(strEmail)) {
		alert ("Your e-mail address appears to be incorrectly formatted. Please check it and try again.")
		return false;
	}
	return true;
}

/* This adds the function to the body tag to make sure the nav is not longer 
	than the content */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(fillPageHeight);
addLoadEvent(addslidepopups);
addLoadEvent(addContactPops);
 
document.domain = "moviehabit.com";
