///////////////////////////////////////////////////////////////////
//	//
//	Javascript functions for tix/SYS On-Line Film Guide	//
//	//
//	copyright (c) 2000 synercom/edi - All Rights Reserved	//
//	//
//	written by Arne F. Hermann	//
//	//
//	//
////////////////////////////////////////////////////////////////////

function tixSYS_body_onload() {
	/**
	 * tixSYS_body_onload(); is a content replacement 
	 *	item - this function is to keep the call valid 
	 *	should it not be replaced. - dt - 20051121
	 */
}

/**
 * This use by pkg mode to move parent window to login via
 *	the addtoplanner routine
 */
function goParent(loc){
        var parentwin = window.opener;
	var grandparentwin = parentwin.opener;
	if(grandparentwin){
	     grandparentwin.location.href=loc;
	     window.close();
	}
	if(parentwin){
	       parentwin.location.href=loc;
		window.close();
        }
}

/**
 * This function used to by addToCart buttons
 */
function ViewPlanner(PkgFill) {
	var time = new Date();
	var sec = ""+time.getHours() + time.getMinutes() + time.getSeconds();
	if (PkgFill == 1) {
	window.location="/cgi-bin/tixSYS/addplannerPKG.pl?"+sec;
	}
	else {
	window.location="/cgi-bin/tixSYS/viewenv.pl?"+sec;
	}
}

/**
 * In use: 20051201 - functions.php
 */
function ClearPlanner(retProg,PkgFill) {
 if (confirm("Click OK to CLEAR your cart of all Ticket Selections\n(you will have to start from scratch). If you don't\nwant to clear your cart, click CANCEL.")) {
	if (PkgFill == 1) {
	var time = new Date();
	var sec = ""+time.getHours() + time.getMinutes() + time.getSeconds();
	window.location="/cgi-bin/tixSYS/clearpkgplanner.pl?"+sec;
	}
	else {
	window.location="/cgi-bin/tixSYS/clearplanner.pl";
	}
	}
}

function AddToCart(FCode) {
	var WinName = window.name;
		document.AddCodeForm.AddCode.value = FCode;
		hAddWin=window.open("addtocart.php?FCode=" + FCode,"AddWindow","width=400,height=200,left=200,top=300,scrollbars=no,toolbar=no,status=no,directories=no,location=no,menubar=no,resizable=no");
}

function AMCTickets(AMCVenue,AMCDate){
	hAddWin=window.open("amcinfo.php?Venue=" + AMCVenue +"&Date="+AMCDate,"AddWindow","width=400,height=200,left=200,top=300,scrollbars=no,toolbar=no,status=no,directories=no,location=no,menubar=no,resizable=no");	
}

function AddToPkg(FCode,PageFrom) {
		document.AddCodeForm.AddCode.value = FCode;
		hAddWin=window.open("addtoplannerpkg.php?FCode=" + FCode + "?PageFrom="+PageFrom,"AddWindow","width=400,height=200,left=200,top=300,scrollbars=no,toolbar=no,status=no,directories=no,location=no,menubar=no,resizable=no");
}
function UpdtPkgCode(FCode) {
	//Get the selected quantity for this FCode
	var QQ = 0;
	var time = new Date();
	var sec = ""+time.getHours() + time.getMinutes() + time.getSeconds();
	QQ = eval("document.PkgPlanner." + FCode + ".selectedIndex");
	window.location="/cgi-bin/tixSYS/sendpkgorder.pl?UPD=" + FCode + ";Q="+QQ+";Dt="+sec;
}
function UpdatePkg() {
	if (confirm("Click OK to update your Package Ticket Order. If you have not finished selecting shows, click CANCEL. After Finalizing, you will receive an Order Confirmation page which will show all details of your order. Note that you will receive vouchers for any ticket entitlements for which you did not select tickets.")) {
	window.location="/cgi-bin/tixSYS/updatepkgorder.pl";
	}
	return;
}
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//	Function to correct for 2.x Mac date bug.
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0)	// Except on the Mac - ahead of its time
	date.setTime (date.getTime() - skew);
}
//	Function to return the value of the cookie specified by "name".
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
	return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break;
	}
	return "NOTSET";
}
//	Function to create or update a cookie.
function SetCookie (name,value,expires,path,domain,secure) {
	path="/";
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}
//	Function to delete a cookie. (Sets expiration date to start of epoch)
function DeleteCookie (name,path,domain) {
	path="/";
	if (GetCookie(name)) {
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function ConfirmUpdate (SysActive) {
	// removed from viff
	//if (SysActive != "1") {
	//alert("We're sorry but the On-line Ticketing Order Update is temporarily OFF-LINE.	You can keep adding shows to your Ticket Planner but will not be able to update your order until we're back on-line.	Please try updating your order later.	Also, please note that you will not be able to update orders between 8:00pm and 9:00pm.");
	//return;
	//}
	if (confirm("Click OK to go to the checkout page where you can\nselect quantities to purchase for the shows you've\nselected.	If you have not finished selecting shows,\nclick CANCEL.")) {
	window.location = "/cgi-bin/tixSYS/orderupdt.pl";
	}
	return;
}
function ConfirmCart (SysActive) {
	// removed from viff
	//if (SysActive != "1") {
	//alert("We're sorry but the On-line Ticketing Order Update is temporarily OFF-LINE.	You can keep adding shows to your Ticket Planner but will not be able to update your order until we're back on-line.	Please try updating your order later.	Also, please note that you will not be able to update orders between 8:00pm and 9:00pm.");
	//return;
	//}
	if (confirm("Click OK to go to the Order Form. You will need to provide your registration\ninformation, and then you will be able to select quantities to purchase for the\nshows you've selected.	If you have not finished selecting shows, click CANCEL.")) {
	window.location = "https://secure.nhs.org/tixSYS/2005/filmguide/TicketOrderForm.php";
	}
	return;
}
function UpdateTotals(FCode,SChgType,SChgAmt,MaxTix) {

var Amount = 0;
var Quantity = 0;
var DAmount = "";
var HChgAmount = 0;
var pr="";
var qq = "";
var Elem = "";
var numelems = document.OrderForm.length;
for (var ii=0; ii < numelems; ii=ii+1) {
	Elem = document.OrderForm.elements[ii];
	FCode = Elem.name;
	if (FCode.substr(0,1) == "Q"){
		pr = eval("document.OrderForm.Price" + FCode.substr(1) + ".value");
		qq = Elem.selectedIndex;
		Amount += qq * pr;
		if (FCode.substr(1,1) != "i") {
			//eval qty for NON-ITEMS (i.e. tickets) only
			Quantity += parseFloat(qq);
		}
		if (Quantity > MaxTix) {
			alert("You can only select a maximum of " + MaxTix + " tickets on a single ticket order.	\nThe Qty for this show will be reset to zero.")
			Quantity = Quantity - Elem.selectedIndex;
			Elem.selectedIndex = 0;
		}
	}
}
//Fix the format of Amount...
if (Amount > 0) {
	// Add in will-call handling charge
	switch (SChgType) {
	case 'P':	//Percentage of Order Total
	Amount = parseFloat(Amount) + (Amount * parseFloat(SChgAmt));
	break;
	case 'F':	//Flat Rate per Order
	Amount = parseFloat(Amount) + parseFloat(SChgAmt);
	HChgAmount = parseFloat(SChgAmt);
	break;
	case 'T':	//Flat Rate per Ticket
	Amount = parseFloat(Amount) + (parseFloat(SChgAmt) * Quantity);
	HChgAmount = parseFloat(SChgAmt) * Quantity;
	break;
}

if (document.getElementById('MailOrder').checked) {
// Hamptons Specific for $1 mail charge
	//Amount = Amount + 1;	
	//document.OrderForm.MailOrderChg.value = "1.00";
}

//Format to [nnn]n.dd
Amount = (Math.ceil(Amount * 100))/100;
DAmount = "A"+Amount;
DAmount = DAmount.substr(1);
if (DAmount.indexOf(".") < 1) {
	DAmount = DAmount + ".00";
}
else {
	DAmount = DAmount + "00";
	DAmount = DAmount.substr(0,DAmount.indexOf(".")+3);
}

HChgAmount = "A"+HChgAmount;
HChgAmount = HChgAmount.substr(1);
if (HChgAmount.indexOf(".") < 1) {
	HChgAmount = HChgAmount + ".00";
}
else {
	HChgAmount = HChgAmount + "00";
	HChgAmount = HChgAmount.substr(0,HChgAmount.indexOf(".")+3);
}

}
document.OrderForm.OrderAmount.value = DAmount;
document.OrderForm.HChgAmount.value = HChgAmount;
document.OrderForm.NumberTix.value = Quantity;
}

//php document related functions
function swapImgRestore() { //v3.0
	var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.p) d.p=new Array();
	var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function findObj(n, d) { //v4.0
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function swapImage() { //v3.0
	var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.p) d.p=new Array();
	var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function swapImgRestore() { //v3.0
	var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function findObj(n, d) { //v3.0
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document); return x;
}
function swapImage() { //v3.0
	var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function WinOpen(height,width,url)
{
 window.open(
	url,
	"RegHelp",
	"toolbar=no,location=no,resizable=yes,scrollbars=yes,status=no,height="+height+",width="+width);
}
function WinOpenFullAttr(height,width,url,popname,fullattr)
{
 window.open(
	url,
	popname,
	fullattr + "," + "height=" + height + ",width=" + width);
}
// Form Processing Javascript
function disableForm(theform) {
	if (document.all || document.getElementById) {
	for (i = 0; i < theform.length; i++) {
	var tempobj = theform.elements[i];
	if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
	tempobj.disabled = true;
	}
	//setTimeout('alert("Your form has been submitted.	Notice how the submit and reset buttons were disabled upon submission.")', 2000);
	return true;
	}
	else {
	alert("Your order is now being processed. This may take a minute or two, so please be patient. Do NOT click the submit button again or you will get an error.");
	//return false;
	}
}
function phpFilmNotePopup(FC) {
	var H = screen.height - 300;
	H = 470;	
	var W = 720;
	var AtX = 25;
	var AtY = 25;
	FilmNotePopup=window.open("filmnote.php?FCode=" + FC,"popup","width=" + W + ",height=" + H +",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=no,status=yes,directories=no,location=no,resizable=yes,menubar=no");
}
function phpProgNotePopup(FC) {
	var H = screen.height - 300;
	H = 500;	
	var W = 720;
	var AtX = 25;
	var AtY = 25;
	FilmNotePopup=window.open("prognote.php?notepg=1&ProgCode=" + FC,"popup","width=" + W + ",height=" + H +",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=no,status=yes,directories=no,location=no,resizable=yes,menubar=no");
}
function phpEventNotePopup(FC) {
	var H = screen.height - 300;
	H = 500;	
	var W = 720;
	var AtX = 25;
	var AtY = 25;
	FilmNotePopup=window.open("eventnote.php?notepg=1&EventNumber=" + FC,"popup","width=" + W + ",height=" + H +",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=no,status=yes,directories=no,location=no,resizable=yes,menubar=no");
	//if( FilmNotePopup ) { alert('went up'); }
	//if( !FilmNotePopup ) { alert('not up'); }
}
function phpTrailerPopup(FC) {
	var H = screen.height - 300;
	H = 500;	
	var W = 720;
	var AtX = 25;
	var AtY = 25;
	FilmNotePopup=window.open("trailer.php?EventNumber=" + FC,"popup","width=" + W + ",height=" + H +",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=no,status=yes,directories=no,location=no,resizable=yes,menubar=no");
}
function phpFeatureLookupPopup(FC) {
	var H = screen.height - 300;
	H = 470;
	var W = 720;
	var AtX = 25;
	var AtY = 25;
	FilmNotePopup=window.open("featurelookup.php?ProgCode=" + FC,"popup","width=" + W + ",height=" + H +",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=no,status=yes,directories=no,location=no,resizable=yes,menubar=no");
}
function timeGridAddTix(FCode) {
	var qq = "";
	var Elem = "";
	var OkUpdate = 1;
	var numelems = document.forms['gridform'].elements.length;
	for (var i=0; i < numelems; i=i+1) {
	Elem = document.forms['gridform'].elements[i].name;
	}
	qq = eval("document.forms['gridform']."+ FCode + ".selectedIndex");
	if (qq == 0) {
	alert("You have not selected a quantity!	Please try again.")
	OkUpdate = 0;
	}
	if (OkUpdate) {
	window.location = "/cgi-bin/tixSYS/updtplanner.pl?ADD+" + FCode + qq;
	}
return;
}
function ShowHide(id, visibility) {
	divs = document.getElementsByTagName("div");
	divs[id].style.visibility = visibility;
}
function IMDBFilmPopup(theFilmURL){
	var H = screen.height - 200;
	var W = screen.width - 200;
	var AtX = 50;
	var AtY = 50;
	FilmNotePopup=window.open("http://us.imdb.com/title/tt" + theFilmURL,"imdb","width=" + W + ",height=" + H + ",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=yes,status=yes,directories=yes,location=yes,resizable=yes,menubar=yes");
}
function IMDBDirectorPopup(theDirNum){
	var H = screen.height - 200;
	var W = screen.width - 200;
	var AtX = 50;
	var AtY = 50;
	FilmNotePopup=window.open("http://us.imdb.com/name/nm" + theDirNum,"imdb","width=" + W + ",height=" + H + ",left=" + AtX + ",top=" + AtY + ",scrollbars=yes,toolbar=yes,status=yes,directories=yes,location=yes,resizable=yes,menubar=yes");
}
function validateQuickForm(theform) {
	var result = false;
	var numElements = theform.elements.length;
	var broken = true;
	var numEntries = 0;
	var brokenEntries = 0;
	for (i = 0; i < numElements; i++){
	var contentLength = theform.elements[i].value.length;
	if(contentLength > 0){
	numEntries++;
	if(contentLength < 5){
	brokenEntries++;
	}
	}
	}
	if((brokenEntries > 0) || (numEntries == 0)){
	alert("There appears to be a problem with your form.\n	You must enter at least ONE Film Code\n and Film Codes must all be 5 characters in length.");
	result = false;
	}
	else{
	result = true;
	}
	return result;
}
function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
	if (fromString == "") {
	return inputString;
	}
	if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
	while (temp.indexOf(fromString) != -1) {
	var toTheLeft = temp.substring(0, temp.indexOf(fromString));
	var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
	temp = toTheLeft + toString + toTheRight;
	}
	} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
	var midStrings = new Array("~", "`", "_", "^", "#");
	var midStringLen = 1;
	var midString = "";
	// Find a string that doesn't exist in the inputString to be used
	// as an "inbetween" string
	while (midString == "") {
	for (var i=0; i < midStrings.length; i++) {
	var tempMidString = "";
	for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
	if (fromString.indexOf(tempMidString) == -1) {
	midString = tempMidString;
	i = midStrings.length + 1;
	}
	}
	} // Keep on going until we build an "inbetween" string that doesn't exist
	// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
	while (temp.indexOf(fromString) != -1) {
	var toTheLeft = temp.substring(0, temp.indexOf(fromString));
	var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
	temp = toTheLeft + midString + toTheRight;
	}
	// Next, replace the "inbetween" string with the "toString"
	while (temp.indexOf(midString) != -1) {
	var toTheLeft = temp.substring(0, temp.indexOf(midString));
	var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
	temp = toTheLeft + toString + toTheRight;
	}
	} // Ends the check to see if the string being replaced is part of the replacement string or not
	return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

/* GRID */
function movegrid(){ // centers grid in window.
	var gridWidth = document.getElementById('venuebar').style.width;
	gridWidth = replaceSubstring(gridWidth, "px", "") 
	var winW = 630, winH = 460;
	if (parseInt(navigator.appVersion)>3) {
	if (navigator.appName=="Netscape") {
	winW = window.innerWidth;
	winH = window.innerHeight;
	}
	if (navigator.appName.indexOf("Microsoft")!=-1) {
	winW = document.body.offsetWidth;
	winH = document.body.offsetHeight;
	}
	}
	var leftoffset = (winW - gridWidth)/2 - 35;
	var allDivs = document.all.tags('div');
	var leftpx;
	for(var i = 0; i < allDivs.length; i++){
	leftpx = allDivs[i].style.left;
	if(leftpx.length > 0){
	leftpx = leftpx.substring(0,leftpx.indexOf('px'));
	var newleft = parseInt(leftpx) + leftoffset;
	allDivs[i].style.left = newleft;
	}
	}
}
var speed, currentpos=curpos1=0, alt=1, curpos2=-1
function initializeScroll(){
	if (window.parent.scrollspeed!=0){
	speed=window.parent.scrollspeed
	scrollwindow();
	}
}
function scrollwindow(){
	temp=(document.all)? document.body.scrollLeft : window.pageXOffset;
	alt=(alt==0)? 1 : 0;
	if(alt==0){
	curpos1=temp;
	}
	else{
	curpos2=temp;
	}
	window.scrollBy(speed,0);	
}


/* MM IMAGE SWAPPING */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* IMAGE SWAPPING */
function changeImage(imageName, newImage) {
	document.images[imageName].src = newImage;
}

/* COOKIES */
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	begin = dc.indexOf(prefix);
	if (begin != 0) return null;
	} else {begin += 2;}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {end = dc.length;}
	return unescape(dc.substring(begin + prefix.length, end));
}

/* MESSAGES */
function tixSYS_displayTestMessages() {
	tm = document.getElementById("test_messages");
	tm.onclick = function () {
		tm.style.display = 'none';
	}
	setTimeout('tm.style.display = "none";', 15000);
}
function tixSYS_displayUserMessages() {
	um = document.getElementById("userMessages");
	um.onclick = function () {
		um.style.display = 'none';
	}
	setTimeout('um.style.display = "none";', 15000);
}

// tribeca amex terms
function AMEXTermsPopup() {
    window.open("AMEXPopup.php",'AMEXPopup','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=470,height=520,top=100');
}

