//* Athodyd Global Functions

//*  2003-01-08	FormTAResizer(a1,a2,a3,a4)
//*				onresize="FormTAResizer(document.Form1.S1,document.Form1.S2,document.Form1.S3);"

//*  SiteEditLink(BASEURL,PAGESTRING,SHOWNTEXT)
//*  refreshParent()
//*  refreshIfUpdate()
//*  dr(pval)
//*  amhere()
//*  ParmstringParse(InString,OutArray)
//*  URLParse(testmode)
//*  getCookieVal (offset)
//*  FixCookieDate (date)
//*  GetCookie (name)
//*  SetCookie (name,value,expires,path,domain,secure) 
//*  DeleteCookie (name,path,domain) 
//*  updateFormStatus(theForm)
//*  checkFormAndGo(theForm)
//*  ddDate(zdate,zname)
//*  DDProdPV(ddname,zarray,inval)
//*  thisDDfromArray(ddname,zarray,inval)
//*  alphaDDfromArray(ddname,zarray,inval)
//*  onChangeFormDate(TheField,zzmm,zzdd,zzyy)
//*  CheckUniqueProdPV(PVID,PNUM)
//*  FFwarnInvalid (theField, s)
//*  fixthedate(indate,inyear)
//*  daysInFebruary (year)
//*  CheckDate(TheField)
//*  openWindow(targetPage,windowName,parmstring) 
//*  closeWindow(windowName) 
//*  closeWindowIfUpdate(windowName) 
//*  closeopenWindow(targetPage,windowName,parmstring)
//*  newWparms(x,y,w,h)
//*  stopthief(ie) 

//*******************************************************************************
function FormTAResizer(X1,X2,X3,X4)
{	if(X1>' ')
	{	X1.rows=parseInt(document.body.clientHeight*.0158)-2
		X1.cols=parseInt(document.body.clientWidth*.11);
	}
	if(X2>' ')
	{	X2.rows=parseInt(document.body.clientHeight*.0158)-2
		X2.cols=parseInt(document.body.clientWidth*.09);
	}
	if(X3>' ')
	{	X3.rows=parseInt(document.body.clientHeight*.0158)-2
		X3.cols=parseInt(document.body.clientWidth*.09);
	}
	if(X4>' ')
	{	X4.rows=parseInt(document.body.clientHeight*.0158)-2
		X4.cols=parseInt(document.body.clientWidth*.09);
	}
}
//*******************************************************************************
function SiteEditLink(BASEURL,PAGESTRING,SHOWNTEXT)
{	callstring="'http://"+BASEURL+"/imagebank/tables/"+PAGESTRING+"'"
	zwindow="'editwindow'";
	dr('<a href="javascript:closeopenWindow('+callstring+','+zwindow+',newWparms(350,20,450,550))">');
	dr('<font size="1"><i>'+SHOWNTEXT+'</i></font></a>');
}
//******************************************************************************
function refreshIfUpdate(ACTION)
{	if (ACTION=='Update') refreshParent();}
//******************************************************************************
function refreshParent(){
	window.opener.location.href=window.opener.location.href;
	if (window.opener.progressWindow)
		window.opener.progressWindow.close();
}
//******************************************************************************
function closeWindowIfRemove(ACTION)
{	if (ACTION=='Remove') refreshParent();window.close();}
//******************************************************************************
function closeWindowIfUpdate(ACTION)
{	if (ACTION=='Update') window.close();}
//******************************************************************************
function dr(pval){document.write(pval)}
//******************************************************************************
function amhere() {dr('<br>we have athodyd.js<br>')}
//******************************************************************************
function ParmstringParse(InString,OutArray)					// Parses a generic parm string (n=v&n=v...)
// Incoming: name1=val1&name2=val2&.... and initialized array (second parm)
{	ArTemp=InString.split('&');								// First, breaks into the pairset via '&'
	for (i=0;i<ArTemp.length;i++)							// Then splits the pairset into name,val.
	{	OutArray[i]={name:ArTemp[i].substring(0,ArTemp[i].indexOf('=')),val:ArTemp[i].substring(ArTemp[i].indexOf('=')+1)}
}	}

//******************************************************************************
function URLParse(testmode)							// Parses the URL string 
// 	Assumes format: http://*/*/fn.ft?p1=v1&p2=v2...pn=vn
//	Identify nulls by *.length=0
//	To be used for getting parms without an SQL query. (sigh. stupid me)
//	Generates: URLTree[0..n],URLFn,URLFt,URLParmSet[0..n].name/val
{	URLTree=document.URL.split('/');						// 1. Parse using '/' into URLTree[0..n]
								if(testmode==true) dr('<br>Tree: ');
								for (i=0;i<URLTree.length;i++){if(testmode==true) dr(', '+i+'='+URLTree[i])}
	URLFn=URLTree[URLTree.length-1];						// 2. URLFn= the last of URLTree   
	URLParmSet=new Array();
	if(URLFn.indexOf('?')!=-1)								// 3. If URLFn has a '?', parse parmstring and reduce URLFn
	{	ParmstringParse(URLFn.substring(URLFn.indexOf('?')+1),URLParmSet); 
		URLFn=URLFn.substring(0,URLFn.indexOf('?'));		}
								if(testmode==true) dr('<br>fn: '+URLFn);		
	if(URLFn.indexOf('.')!=-1)								// 4. If remaining URLFn has a '.', split to URLFn and URLFt
	{	URLFt=URLFn.substring(URLFn.indexOf('.')); 
		URLFn=URLFn.substring(0,URLFn.indexOf('.'));		}
	else URLFt='';
								if(testmode==true) dr('<br>fn: '+URLFn+', ft: '+URLFt+', ft length: '+URLFt.length);	
}
//******************************************************************************
//* Cookies 
//* 
//* Cookie_set
//
//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
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.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
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".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
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 null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  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)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
//*  Form Status *****************************
var FormStatus='Unchanged';		// becomes 'Changed' then 'Checked'
function updateFormStatus(theForm)
{	theForm.FormStatus.value='Changed';	
}
function checkFormAndGo(theForm)
{	if(theForm.FormStatus.value=='Changed')
		{theForm.FormStatus.value='Checked';
		alert('Please review your data and click submit again.');
		return false;	}
	else
		{		//window.close();
		return true;	 }
}
//***************************************
ArMonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
ArMonthLen=new Array(31,28,31,31,30,31,31,30,31,30,31);
ArDay=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
ddcurdate=new Date();
//** ddDate (zdate,zname) *******************************************************************
//* Generates drop-down boxes for a form for setting a date
//*
//* Input: 	zdate: from sql, using: format(<datefieldname>,'mmm d, yyyy')
//*			zname: datefieldname
//*
//* Change Log:
//*	2001-01-12	Switched to all dd's
//*					
//* From SQL to the ddDate call:
//*	1. SQL: 				format(<datefieldname>,'mmm d, yyyy') as t<datefieldname>
//*	2. Js in SQL Region: 	<datefieldname>=new Date("<VB t<datefieldname> eVB>");
//*	3. Js on the form:		document.write('<tr><td>LABEL</td><td>'+ddDate(<datefieldname>,"<datefieldname>")+'</td></tr>')
//*	4. SQL Update:		set <datefieldname>=dateserial(::dd<datefieldname>yy::,::dd<datefieldname>mm::+1,::dd<datefieldname>dd::)
//*
//****************************************************************************************


function ddDate(zdate,zname){
	zo='';
	if (!isNaN(zdate)){	zmm=zdate.getMonth();	zdd=zdate.getDate();	zyy=zdate.getFullYear();}
		else {		zmm=-1;		zdd="";	zyy=ddcurdate.getFullYear();}
	rezname='"'+zname+'"';
//	document.write('<br>zdate: ',zdate,', ',zmm,'/',zdd,'/',zyy);
//** Month **
	zo='<select name="'+zname+'mm" onChange="onChangeFormDate(this.form.'+zname+',this.form.'+zname+'mm.value,'+zname+'dd.value,'+zname+'yy.value)"><option value="-1">&nbsp;</option>';
	for (i=0;i<12;i++){
		if(zmm==i)zo=zo+'<option value="'+i+'" selected>'+ArMonth[i]+'</option>';
			else zo=zo+'<option value="'+i+'" >'+ArMonth[i]+'</option>';		}
	zo=zo+'</select>&nbsp;';
//** Day **
	zo=zo+'<select name="'+zname+'dd" onChange="onChangeFormDate(this.form.'+zname+',this.form.'+zname+'mm.value,'+zname+'dd.value,'+zname+'yy.value)"><option value="-1">&nbsp;</option>';
	for (i=1;i<32;i++){
		if(zdd==i)zo=zo+'<option value="'+i+'" selected>'+i+'</option>';
		else zo=zo+'<option value="'+i+'" >'+i+'</option>';	}
	zo=zo+'</select>';	
//** Year **	
	zo=zo+', <select name="'+zname+'yy" onChange="onChangeFormDate(this.form.'+zname+',this.form.'+zname+'mm.value,'+zname+'dd.value,'+zname+'yy.value)"><option value="-1">&nbsp;</option>';
	for (year=2025; year>1900; year--) {
		if (year == zyy) zo += '<OPTION VALUE="' + year + '" SELECTED>' + year + '</OPTION>';
			else              zo += '<OPTION VALUE="' + year + '">'          + year + '</OPTION>';    	}
	zo=zo+'</select>';
//** Hidden Field **
	zzXzmm=parseInt(zmm)+1;
	if(!isNaN(zdate))zo=zo+'<input type="hidden" name="'+zname+'" value="'+zzXzmm+'/'+zdd+'/'+zyy+'">';
	else zo=zo+'<input type="hidden" name="'+zname+'">';
return zo;
}
//****************************************************************************************
function DDProdPV(ddname,zarray,inval)
{	document.write('<select name="'+ddname+'" onChange="CheckUniqueProdPV(this.form.pvid,this.form.printnum)">');
	document.write('<option value="0"></option>');
	for (i=0;i<zarray.length;i++)
	{	if(zarray[i].id==inval) selflag=' selected '; else selflag=' ';
		document.write('<option value="'+zarray[i].id+'" '+selflag+'>'+zarray[i].title+'</option>');	}
	document.write('</select>');
}
function thisDDfromArray(ddname,zarray,inval)
{	document.write('<select name="'+ddname+'" >');
	if (inval==0) selflag=' selected '; else selflag=' ';
	document.write('<option value="0" '+selflag+'></option>');
	for (i=0;i<zarray.length;i++)
	{	if(zarray[i].id==inval) selflag=' selected '; else selflag=' ';
		document.write('<option value="'+zarray[i].id+'" '+selflag+'>'+zarray[i].title+'</option>');	}
	document.write('</select>');
}
function MakeDDfromArray(ddname,zarray,inval)
{	document.write('<select name="'+ddname+'" >');
	for (i=0;i<zarray.length;i++)
	{	if(zarray[i].id==inval) selflag=' selected '; else selflag=' ';
		document.write('<option value="'+zarray[i].id+'" '+selflag+'>'+zarray[i].title+'</option>');	}
	document.write('</select>');
}
function alphaDDfromArray(ddname,zarray,inval)
{	document.write('<select name="'+ddname+'" >');
	if (inval=='.') selflag=' selected '; else selflag=' ';
	document.write('<option  '+selflag+'>.');
	for (i=0;i<zarray.length;i++)
	{	if(zarray[i].title==inval) selflag=' selected '; else selflag=' ';
		document.write('<option  '+selflag+'>'+zarray[i].title);	}
	document.write('</select>');
}

//****************************************************************************************

function onChangeFormDate(TheField,zzmm,zzdd,zzyy)
{	zXzmm=parseInt(zzmm)+1;	
	if(zzmm==-1) TheField.value='';
	else if(zzdd==-1) TheField.value=zXzmm+'/15/'+zzyy;
	else TheField.value=zXzmm+'/'+zzdd+'/'+zzyy;
}
function CheckUniqueProdPV(PVID,PNUM)
{	pset=PVID.value.toString()+PNUM.value.toString();
	for (i=0;i<pvcheck.length;i++)
	{	if(pvcheck[i]==pset) 
		{   PNUM.focus()
    			PNUM.select()
    			alert('This print already exists.')
    			return false
	}	}
	return true;
}
//****************************************************************************************
function FFwarnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}
//****************************************************************************************
function fixthedate(indate,inyear)				// eg: fixthedate("**=FP_FieldVal(fp_rs,"lastcheck")**",**=d2year**)
{	if(indate=='&nbsp;')	outdate='.'
	else	{	tdate=new Date(indate);	outdate=new Date(inyear,tdate.getMonth(),tdate.getDate(),tdate.getHours(),tdate.getMinutes(),tdate.getSeconds());	}
	return outdate;
}
//****************************************************************************************

daysInMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);

function daysInFebruary (year)
// Validate February Day per Year algorithm
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}
function CheckDate(TheField)								// A very loose date validation
{	msg='Please enter a valid date.';
	if(TheField.value.length==0) return true;
	TheDate=TheField.value;
	xdate=new Date();
	if(!isNaN(TheDate)&&TheDate.indexOf("/")==-1)			// If only one number, it is a year. 
	{	yy=parseInt(TheDate);								// Using 50 to distinguish 2000 from 1900.
		if(yy<50)		yy=yy+2000;
		else	if(yy<100)	yy=yy+1900;
		TheField.value='1/1/'+yy;
		return true;									// Return the year-only.
	}
	ArTemp=TheDate.split('/');
	if(ArTemp.length==0)		return FFwarnInvalid (TheField, msg);	// Well, the isNaN gets this; nolo.
	if(ArTemp.length>3)		return FFwarnInvalid (TheField, msg);	// Too many slashes.
	for (i=0;i<ArTemp.length;i++) 							//  Loop through checking
	{	if(isNaN(ArTemp[i])) 	return FFwarnInvalid (TheField, msg);	//	that they're numbers
		if(ArTemp[i]==0)		return FFwarnInvalid (TheField, msg);	//	and not 0.
	}
	if(ArTemp.length==2)										// If 2, and second len >2, second is year, else, is month.
	{	if(ArTemp[1].length>2)	{	yy=parseInt(ArTemp[1]);	mm=parseInt(ArTemp[0]);	dd=1;}
		else					{	yy=xdate.getYear();		mm=parseInt(ArTemp[0]);	dd=parseInt(ArTemp[1]);}
	}
	else						{	yy=parseInt(ArTemp[2]);	mm=parseInt(ArTemp[0]);	dd=parseInt(ArTemp[1]);}
	if(mm>12)				return FFwarnInvalid (TheField, msg);	// Validate month.
	if(yy<100)
	{	if(yy<50)yy=2000+yy;								// Century 50 split.
		else yy=1900+yy;
	}
	if(dd>daysInMonth[mm]) 					return FFwarnInvalid (TheField, msg);	// Days in month
	if ((mm == 2) && (dd > daysInFebruary(yy))) 	return FFwarnInvalid (TheField, msg);	// February.
	TheField.value=mm+'/'+dd+'/'+yy;
	return true;									// Whew.
}
//****************************************************************************************
// Popup Handling
//***************************************************************
var ishere=1;
var windowHandle = null; 
var windowHandle_closed = false;  

function openWindow(targetPage,windowName,parmstring) 
{	windowHandle = window.open(targetPage,windowName,parmstring);
	if (windowHandle_closed) 
	{         windowHandle_closed = false;     
}	}  
function closeWindow(windowName) 
{     if (windowHandle != null) 
	{	if (!windowHandle_closed) 
		{	windowHandle_closed = true;
		windowHandle.close();         
}     } 	}
function closeopenWindow(targetPage,windowName,parmstring)
{	closeWindow(windowName);
	openWindow(targetPage,windowName,parmstring);
}
function closeopenWindowClean(targetPage,windowName)
{	closeWindow(windowName);
	windowHandel=window.open(targetPage,windowName)
	if (windowHandle_closed)
	{	windowHandle_closed=false;
}	}
function newWparms(x,y,w,h)
{	rval='screenX='+x+',screenY='+y+',left='+x+',top='+y+',width='+w+',height='+h+',resizable=yes,scrollbars=yes,location=yes,status=yes';
	return rval;	}
function newWparmsOld(x,y,w,h)
{	rval='screenX='+x+',screenY='+y+',left='+x+',top='+y+',width='+w+',height='+h+',resizable=yes,scrollbars=yes,location=yes,status=yes';
	return rval;	}
//***************************************************************
<!-- from: http://www.boogiejack.com/howx027.html -->
function stopthief(ie) 
{	var warning = "Right clicking this page is not allowed.";
	if (navigator.appName == 'Netscape' && ie.which == 3) {
	alert(warning);
	return false;
	}
	else
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
		alert(warning);
		return false;
	}
return true;
}
