var debugFlag = (self.location.search.indexOf("debug=1") > -1) ? 1 : 0;

/***********************************************
	Browser detection
 ***********************************************/	
var isNav4 = (document.layers) ? 1 : 0;
var isIE4Up = (document.all) ? 1 : 0;
var isNav6 = (document.getElementById && !isIE4Up) ? 1 : 0;
var isMac = (navigator.appVersion.indexOf('Mac')!= -1)? 1:0
var isIEMac = (isMac && isIE4Up) ? 1 : 0;
var isSafari = (navigator.appVersion.indexOf('Safari') != -1) ? 1 : 0;

/***********************************************
	general functions
 ***********************************************/	
var thispage = self.location.href;
var homeFlag = ((self.location.href.indexOf("home.asp") > -1) || (self.location.href.indexOf("home_aug") > -1)) ? 1 : 0;

function popSchedule(intDay) {
	var offset = 100;
	var w = screen.width;
	if (w == null || w == "") {
		w = 800;
	} else {
		w -= offset;
	}
	var h = screen.height;
	if (h == null || h == "") {
		h = 600;
	} else {
		h -= offset;
	}	
	
	popWindow("films_vwSchedule.asp?id="+ intDay, w, h, 1, 1, 0);
}

function maxtixPop() {
	//var url = "https://buytiff.torfilmfest.ca/scripts/max/2000/maxweb.exe";
	var url = "http://www.e.bell.ca/filmfest/2005/popup_order.asp"
	var w = 642;
	var h = 506;
	var maxtix = window.open(url,'maxtix','toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=no,copyhistory=no,width='+ w +',height='+ h);
	maxtix.focus();
}

function donatePop() {
	var url = "https://buytiff.torfilmfest.ca/scripts/max/12000/maxweb.exe";
	var maxtix = window.open(url,'maxtix','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,copyhistory=no,width=620,height=500');
	maxtix.focus();
}

function PopUp(url,width,height) {
	popWindow(url, width, height, 0, 0, 0);
}

function popWindow(url, width, height, scrollFlag, resizeFlag, menuFlag) {
	if (scrollFlag) scrollFlag = "yes";
	if (resizeFlag) resizeFlag = "yes";
	if (menuFlag) menuFlag = "yes";
	var tiffpop = window.open(url,'tiffpop','width='+ width +',height='+ height +',toolbar=no,location=no,directories=no,status=no,scrollbars='+ scrollFlag +',resizable='+ resizeFlag +',copyhistory=no,menubar='+ menuFlag);
	tiffpop.focus();
}

function submitForm(formName) {
	document.forms[formName].submit();
}

function resetForm(formName) {
	document.forms[formName].reset();
}

function updateNavStyle() {
	if (document.getElementById && !isIEMac) {
		document.getElementById("nav-container").style.background = "url('med/nav/bg_mainNav.gif')";
	}
}

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}


/***********************************************
	Resize code
 ***********************************************/	
if (!window.saveInnerWidth && (top.location.href.indexOf("films_vwSchedule.asp") == -1)) {
  window.onresize = resizeIt;
  window.saveInnerWidth = getWindowWidth();
  window.saveInnerHeight = getWindowHeight();
}

function resizeIt() {
	if (isNav4) {
		self.location.reload();

	} else {
		positionAdditionalNav();
		if (homeFlag) posme();
	}	
	
}

function getWindowWidth() {
	var pageWidth;
	
	if (navigator.appName.indexOf("Microsoft") != -1) pageWidth = 10;//window.document.body.offsetWidth;
	else pageWidth = window.innerWidth;

	return pageWidth;
	//alert(pageWidth);
}

function getWindowHeight() {
	//var height = window.innerHeight;
	//if (typeof(height) != "number") height = window.document.body.offsetHeight;
}

/***********************************************
	My Film List	
 ***********************************************/	
function validateLogin() {
	var theForm = document.frm_filmListLogin;
	var valid = true;
	var errMsg = "";
	
	if (theForm.username == "") {
		valid = false;
		errMsg += "Please enter a username\n";
	}
	
	if (theForm.password == "") {
		valid = false;
		errMsg += "Please enter a password\n";
	}
	
	if (!valid) {
		errMsg = "The following errors were encountered:\n\n"+ errMsg;
		alert(errMsg);		
	} else {
		theForm.submit();
	}
}

function registerMyFilmList(film_id) {
	document.location.replace("films_myFilmList.asp?user=new&myFilmsFunc=add&filmID="+ film_id);
}


/***********************************************
	TIFF Alerts
 ***********************************************/	
function submitTIFFAlert(objForm, alertAction) {
	var theForm = document.frm_tiffAlert;
	theForm.alertType.value = 1;
	if (alertAction == "unsubscribe") theForm.alertType.value = 2;
	theForm.submit();
}
	
function validateTIFFAlert(alertAction) {
	var theForm = document.frm_tiffAlert;
	var valid = true;
	var errMsg = "";
	
	if (theForm.email.value == "" || theForm.email.value == "[email address]") {
		valid = false;
		errMsg = "Please enter your email address.";
		theForm.email.focus();
		
	} else {
		if (!validateEmailAddress(theForm.email.value)) {
			valid = false;
			errMsg = "Please enter a valid email address.";
			theForm.email.focus();
		}
	}
	
	if (!valid) {
		alert(errMsg);
	} else {
		submitTIFFAlert(theForm, alertAction);
	}
}

/***********************************************
	Validation
 ***********************************************/	
function validateEmailAddress(theValue)
{
     var valid = true;
     var a = theValue.indexOf("@");
        
     if ((a < 1) || (a == (theValue.length - 1)))
     {    valid = false;
     }else
     {  
          var emailName = theValue.substring(0, a);
          var emailDomain = theValue.substring(a+1);
                
          valid = valid && validateEmailName(emailName);                
          valid = valid && validateEmailDomain(emailDomain);
     }
     return valid;
}

function validateEmailDomain(theDomain)
{
        var dot = theDomain.indexOf(".");
        
        if ((dot < 1) || (dot == (theDomain.length -1)))
        {       return false;
        }
        
        return validateEmailName(theDomain);
}

function validateEmailName(theName)
{       
        if ((theName.length < 1) || (theName.length > 25))
        {       return false;
        }

        var encoded = escape(theName);
        if ((encoded.indexOf("%") < 0) &&
                (encoded.indexOf("*") < 0) &&
                (encoded.indexOf("+") < 0) &&
                (encoded.indexOf("/") < 0) &&
                (encoded.indexOf("@") < 0))
        {       return true;
        }else
        {       return false;
        }       
}
	
/***********************************************
	functions for controlling on/off graphic
	states
 ***********************************************/	
function setDayNavFlag(name) {
	var boolReturn = false;
	
	if ((typeof(gStrDay) != "undefined" &&  gStrDay != null) && (typeof(gStrNextDay) != "undefined" &&  gStrNextDay != null)) {
		if (name == "mainnav_day_"+ gStrDay || name == "mainnav_day_"+ gStrNextDay) {
			boolReturn = true;
		}
	}
	
	return boolReturn;
}

function doswap(layer,name,onoff){
	var layerName = subNavId + layer;
	if (layer.indexOf("_") != -1) layerName = layer;
	var dayActiveNavFlag = setDayNavFlag(name);  // disables rollover for active day navigation buttons when their content is displayed in iframe
	
	if (!dayActiveNavFlag) {
		if(layer!=''&&layer!='true'&&bw.ns4){
			if (layerName.indexOf("welcomeFrame") == -1) {
				var imageSrc = eval('document.'+ layerName +'.document.images["'+name+'"].src');
				eval('document.'+ layerName +'.document.images["'+name+'"].src = "'+swapRoll(imageSrc,onoff)+'";');
			}	
		}else{
			var imageSrc = document.images[name].src;
			document.images[name].src = swapRoll(imageSrc,onoff);
		}
	}	
}

function swapRoll(tmp1,onoff){
	if(onoff){
		if(tmp1.indexOf("flyout_")>-1)clearTimer();
		return tmp1.replace("off.gif","on.gif");
	}else{
		if(tmp1.indexOf("flyout_")>-1)setTimer(300);
		return tmp1.replace("on.gif","off.gif");
	}
}


 
/***********************************************
	functions for controlling div layer
	subnav
 ***********************************************/	
//var numSubNavLayers = 8;  // the number of div layers holding subnav menus, used to turn them all off

//Default browsercheck, added to all scripts!
function verifyCompatibleBrowser(){ 
    this.ver=navigator.appVersion; 
	this.agt=navigator.userAgent;
	this.dom=document.getElementById?1:0;
    this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0; 
    this.ie4=(document.all && !this.dom)?1:0; 
    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0; 
	this.is_win95_98=(this.agt.indexOf("win98")>-1)?1:0;
	//this.ns4=(this.ver.indexOf("Mozilla")>-1 && this.ver.indexOf("MSIE")<0)?parseInt(this.ver)==4?1:0:0;
    
	this.is_Mac=(navigator.userAgent.indexOf("Mac") > -1)?1:0;
	
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns5 || this.is_Mac || bw.is_win95_98) 
    return this 
} 
bw=new verifyCompatibleBrowser() 

var layerTimeout=0;

function showLayer(objRefName, layer) {
	resetAllLayers();
	setLayers(objRefName, layer, true);
}

function interpret(val){
	if(val==true&&!bw.ns4) return "visible";
	else if(val==true) return "show";
	else return "hidden";
}

function positionAdditionalNav() {
	var objRef ="mainnav_yearRound";//"thequicklinksanchor";//"mainnav_yearRound";////
	/*
	if (typeof(augustFlag) != "undefined" &&  augustFlag != null) {
		if (augustFlag) objRef = "mainnav_liveFromFestival";
	}
	*/
	setLayers(objRef, "div_additionalNav", true);
}

function blockHomeKeyline() {
	if (!bw.ns4) {
		var objRef = "mainnav_mth_sept";
		setLayers(objRef, "div_keylineBlocker", true);
	}	
}

function setLayers(objRefName, layerName, value){
	var theValue = interpret(value);
	var layerTopOffset = 0;
	var layerLeftOffset = 6; 
	var NS4_offset = "";
	if (objRefName == "btn_quickLinks") layerLeftOffset = -63;	
		
	if (layerName == "div_keylineBlocker") {
		// set offsets for graphic to block keyline above iframe
		layerTopOffset = 13;
		layerLeftOffset = 0;
		
	} else if (layerName == "div_additionalNav") {
		// set offsets for order and quicklinks buttons
		if (homeFlag) {
			// offsets for home page
			layerTopOffset = 0;
			layerLeftOffset = 0;
			if (typeof(augustFlag) != "undefined" &&  augustFlag != null) {
				if (augustFlag) {
					layerTopOffset = 0;
					layerLeftOffset = -52;
				}
			}
			
		} else {
			// offsets for interior pages
			layerTopOffset = 0;
			layerLeftOffset = 10;
			if (typeof(augustFlag) != "undefined" &&  augustFlag != null) {
				if (augustFlag) {
					layerTopOffset = -1;
					layerLeftOffset = 100;
				}
			}
			
		}
	} else if (layerName == "div_flyout"+ numSubNavLayers) {
		// the last div layer will always be the quicklinks flyout (not to be confused with
		// the div layer that holds the order and quicklinks buttons)
		// it's referencing is done according to the TIFFG year round  or Live from festival button
		// in order to accomodate NS4
		if (homeFlag) {
			layerTopOffset = 0;
			layerLeftOffset = 20;
		} else {
			layerTopOffset = 0;
			layerLeftOffset = 5;
		}
	
	}
	
	// a few exceptions
	// additional offset for quicklinks menu
	if (objRefName == "quicklinks") {
		if (homeFlag) {
			layerLeftOffset -= 57;
		} else {
			layerLeftOffset += 38;
		}	
		
		//objRefName = "mainnav_liveFromFestival";
		objRefName = "mainnav_yearRound";//"thequicklinksanchor";//"mainnav_yearRound";
	}
	
	// reduce offset for festival info
	if (objRefName == "mainnav_festivalInfo") layerLeftOffset -= 5;
	
	// set x,y coordinates for layerName
	//var layerTop = (objRefName != "") ? findPosY(document.images[objRefName]) + layerTopOffset : "";
	var layerTop = layerTopOffset;
	//alert(objRefName)
	if (layerName == "div_keylineBlocker") layerTop = (objRefName != "") ? findPosY(document.images[objRefName]) + layerTopOffset : "";
	var layerLeft = (objRefName != "") ? findPosX(document.images[objRefName]) + layerLeftOffset : "";
	//alert("done")
	if (bw.ie4 || bw.ie5 || bw.ie6) {
	
		if (layerLeft != "") {
			eval(layerName).style.xpos = layerLeft;
			eval(layerName).style.left = eval(layerName).style.xpos;
		}
		
		if (layerTop != "") {
			eval(layerName).style.ypos = layerTop;
			eval(layerName).style.top = eval(layerName).style.ypos;
		}	
		eval(layerName).style.visibility = theValue;
		//alert(layerName +" -> "+ eval(layerName).style.left +", "+ eval(layerName).style.top +" ~~ "+ layerLeft +", "+ layerTop);
		
	} else if (bw.ns5) {
		if (layerLeft != "") {
			if (navigator.userAgent.indexOf("Safari")!=-1){
				document.getElementById(layerName).style.left = layerLeft;
			}else{
				document.getElementById(layerName).style.xpos = layerLeft;
				document.getElementById(layerName).style.left = document.getElementById(layerName).style.xpos;
			}
		}
		
		if (layerTop != "") {
			if (navigator.userAgent.indexOf("Safari")!=-1){
				document.getElementById(layerName).style.top = layerTop;
			}else{
				document.getElementById(layerName).style.ypos = layerTop;
				document.getElementById(layerName).style.top = document.getElementById(layerName).style.ypos;
			}
		}	
		document.getElementById(layerName).style.visibility = theValue;	
	
	} else {
		if (layerLeft != "") {
			document.layers[layerName].xpos = layerLeft;
			document.layers[layerName].left = document.layers[layerName].xpos;
		}
		
		if (layerTop != "") {
			document.layers[layerName].ypos = layerTop;
			document.layers[layerName].top = document.layers[layerName].ypos;
		}	
		if (NS4_offset != "" && NS4_offset > 0) {
			var tmp = document.layers[layerName].top
			document.layers[layerName].ypos = tmp + NS4_offset;
			document.layers[layerName].top = document.layers[layerName].ypos;
		}
		document.layers[layerName].visibility = theValue;
	}	
}

// the following is used to nudge all layers slightly left to accomodate NS4
var subNavId = "div_flyout";
/*
window.onLoad = function() {
	if (!(bw.ie4 || bw.ie5 || bw.ie6 || bw.ns5)) {
		var ns4LeftOffset = 8;
		for (i = 1; i <= numSubNavLayers; i++) {
			document.layers[subNavId + i].xpos = document.layers[subNavId + i].left + ns4LeftOffset;
			document.layers[subNavId + i].left = document.layers[subNavId + i].xpos;
		}
	}	
}
*/
function clearTimer() {
	window.clearInterval(layerTimeout);
}

function setTimer(interval,what) {
	layerTimeout = window.setTimeout('resetAllLayers()',interval);
}

function resetAllLayers() {
	for (i=1; i<=numSubNavLayers; i++) setLayers("", subNavId + i,false);
}	

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{ //alert(obj)
	var curtop = 0;
	var printstring = '';
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	//window.status = printstring;
	return curtop;
}


