//-----------------------------------------------------------------------------
// collectform01.js
//
// CollectionsOnline! Form Processing Scripts
// Version 1
//
// Authors:	Stephen Detwiler
//-----------------------------------------------------------------------------

var	g_bValidating = false;
var bSendingFile = false;

function makeArray()    {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function NthDay(nth,weekday,month,year) {
    if (nth > 0) return (nth-1)*7 + 1 + (7 + weekday -
DayOfWeek((nth-1)*7 + 1,month,year))%7;
    if (LeapYear(year)) var days = daysofmonthLY[month];
    else                var days = daysofmonth[month];
    return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
}

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d+1;
}

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

function getMS(date) {
	return Date.UTC( y2k(date.getYear()), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds() );
}

function isDST( d )
{
	var thisday = new Date( );
	thisday.setTime(d);
	
	var year = y2k(thisday.getYear());

	var DSTstart = new Date(year,4-1,NthDay(1,1,4,year),2,0,0);
	var DSTend   = new Date(year,10-1,NthDay(-1,1,10,year),2,0,0);

	var todayMS = getMS(thisday);
	var DSTstartMS = getMS(DSTstart);
	var DSTendMS = getMS(DSTend);

	if ( d > DSTstartMS && d < DSTendMS )
		return true;
	else
		return false;
}


function toservertime( ts )
{
	var d = new Date();
	var tzo = d.getTimezoneOffset();	// in minutes

	var serverOffset = 8*60;			// server offset in minutes
	tzo=(serverOffset)-tzo;				// difference between client time and server time
	tzo = tzo*60000;					// convert to ms (*60 seconds *1000 ms)
	ts+=tzo;							// adjust time

	if( isDST( ts ) )
		serverOffset-=60000;			// spring forward (60 secons * 1000ms)

	return ts;
}

// assume processing of 'required' has already happened
function processtimefield( srcform_intime, srcform_outtime, bIsDaylightSavings )
{
	var timestamp=0;
	
	if( srcform_intime.value == "" )
	{
		srcform_outtime.value=0;
	}
	else
	{
		toparse = srcform_intime.value;

		if( toparse.lastIndexOf( "/" ) != toparse.length-5 )
			return false;	
			
		toparse += " 12:00 AM";

		timestamp = Date.parse( toparse );
		timestamp = toservertime( timestamp, bIsDaylightSavings );
		if( timestamp < 0 )
			return false;
		
		timestamp /= 1000;
		srcform_outtime.value = timestamp;
	}
	
	return true;
}

//-----------------------------------------------------------------------------
// CollectError()
//-----------------------------------------------------------------------------
function CollectError( msg )
{
	alert( "CollectionsOnline! Form Processing Error\r\n\r\n" + msg );
	return false;
}


//-----------------------------------------------------------------------------
// function RightsWindow( url )
//-----------------------------------------------------------------------------
function rightsWindow( url )
{
	window.open( url, "rightswindow", "width=350,height=400,location=0,menubar=0,scrollbars=1,toolbar=0",true);
}

//-----------------------------------------------------------------------------
// function SiteValidate( srcform, dtfunct )
//-----------------------------------------------------------------------------
function SiteValidate( srcform, dtfunct )
{
	// call the data table form handler	
	if( dtfunct!=null && dtfunct( srcform ) == false )
		return false;

	var theRights = new String( GetCookie( "rights" ) );
	if( theRights != "null" )
		srcform.rights.value = theRights;
	
	return true;
}

//-----------------------------------------------------------------------------
// function AlbumValidate( srcform, dtfunct )
//-----------------------------------------------------------------------------
function AlbumValidate( srcform, dtfunct )
{
	if( FutureArtsValidate( srcform ) == false )
		return false;
	if( Optional( srcform, "template" ) == false )
		return false;
	if( Optional( srcform, "rights" ) == false )
		return false;
	if( Optional( srcform, "albumsort" ) == false )
		return false;
	if( Optional( srcform, "foldersort" ) == false )
		return false;
	if( Optional( srcform, "redirect" ) == false )
		return false;

	// call the data table form handler	
	if( dtfunct!=null && dtfunct( srcform ) == false )
		return false;

	// Only update rights from cookie if no value already assigned to rights
	if( srcform.rights )
	{
		if( srcform.rights.value=="" )
		{
			var theRights = new String( GetCookie( "rights" ) );
			if( theRights != "null" )
				srcform.rights.value = theRights;
		}
	}

	if( srcform["filelist_un"] )
	{
		// if files were specified, send them to the server
		if( srcform["filelist_un"].value.length > 0 )
		{
			srcform["filelist"].value = escape( srcform["filelist_un"].value );
			SendFiles( srcform );
		}
	}

	return true;
}


//-----------------------------------------------------------------------------
// function FolderValidate( srcform, dtfunct )
//-----------------------------------------------------------------------------
function FolderValidate( srcform, dtfunct )
{
	if( g_dlgOpen == true || bSendingFile )
	{
		alert( "You are currently uploading a file. Please wait until the upload window closes." );
		return false;
	}

	if( FutureArtsValidate( srcform ) == false )
		return false;
//	if( Optional( srcform, "mediasort" ) == false )
//		return false;
	if( Optional( srcform, "redirect" ) == false )
		return false;

	if( Optional( srcform, "convert" ) == false )
		return false;

	if( Optional( srcform, "filelist" ) == false )
		return false;

	if( Optional( srcform, "filelist_un" ) == false )
		return false;

	// call the data table form handler	
	if( dtfunct!=null && dtfunct( srcform ) == false )
		return false;

	if( srcform.rights )
	{
		if( srcform.rights.value=="" )
		{
			var theRights = new String( GetCookie( "rights" ) );
			if( theRights != "null" )
				srcform.rights.value = theRights;
		}
	}
	
	// if files were specified, send them to the server
	if( srcform["filelist_un"].value.length > 0 )
	{
		srcform["filelist"].value = escape( srcform["filelist_un"].value );
		SendFiles( srcform );
	}

	if( document.all&&(document.all["importwindow"] != null) && (srcform["filelist_un"].value.length > 0) )
		importwindow.style.visibility = "visible";

	return true;
}

//-----------------------------------------------------------------------------
// function FolderMultipleValidate( srcform, dtfunct )
//-----------------------------------------------------------------------------
function FolderMultipleValidate( srcform, dtfunct )
{

	if( FutureArtsValidate( srcform ) == false )
		return false;
	if( Optional( srcform, "redirect" ) == false )
		return false;
	if( Optional( srcform, "convert" ) == false )
		return false;
	if( Optional( srcform, "filelist" ) == false )
		return false;

	if( Optional( srcform, "filelist_un" ) == false )
		return false;

	// if files were specified, send them to the server
	if( srcform["filelist_un"].value.length > 0 )
	{
		srcform["filelist"].value = escape( srcform["filelist_un"].value );
		SendFiles( srcform );
	}

	if( (document.all["importwindow"] != null) && (srcform["filelist_un"].value.length > 0) )
		importwindow.style.visibility = "visible";

	// we made it, submit the form
//	srcform.submit();
	
	return true;
}


//-----------------------------------------------------------------------------
// function MediaMultipleValidate( srcform )
//-----------------------------------------------------------------------------
function MediaMultipleValidate( srcform )
{
	if( FutureArtsValidate( srcform ) == false )
		return false;

	if( Optional( srcform, "redirect" ) == false )
		return false;
	if( Optional( srcform, "filelist" ) == false )
		return false;
	if( Optional( srcform, "filelist_un" ) == false )
		return false;

	// if files were specified, send them to the server
	if( srcform["filelist_un"] )
	{
		if( srcform["filelist_un"].value.length > 0 )
		{
			srcform["filelist"].value = escape( srcform["filelist_un"].value );
			SendFiles( srcform );
		}
	}
	if( (document.all["importwindow"] != null) && (srcform["filelist_un"].value.length > 0) )
		importwindow.style.visibility = "visible";

	// we made it, submit the form
//	srcform.submit();
	
	return true;
}


//-----------------------------------------------------------------------------
// function MediaValidate( srcform, dtfunct )
//-----------------------------------------------------------------------------
function MediaValidate( srcform, dtfunct )
{
	if( FutureArtsValidate( srcform ) == false )
		return false;
	if( Optional( srcform, "redirect" ) == false )
		return false;

	// call the data table form handler	
	if( dtfunct!=null && dtfunct( srcform ) == false )
		return false;
	if( srcform.rights )
	{
		var theRights = new String( GetCookie( "rights" ) );
		if( theRights != "null" )
			srcform.rights.value = theRights;
	}

	// we made it, submit the form
//	srcform.submit();
	if( (document.all["importwindow"] != null) && (srcform["filelist_un"].value.length > 0) )
		importwindow.style.visibility = "visible";
	
	return true;
}


//-----------------------------------------------------------------------------
// function SiteModify( srcform, dtfunct )
//-----------------------------------------------------------------------------
function SiteModify( srcform, dtfunct )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( SiteValidate( srcform, dtfunct ) == false )
	{
		CollectError( "Failed to modify the site" );
		g_bValidating = false;
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------------
// function AlbumAdd( srcform, dtfunct )
//-----------------------------------------------------------------------------
function AlbumAdd( srcform, dtfunct )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( AlbumValidate( srcform, dtfunct ) == false )
	{
		CollectError( "Failed to add the album" );
		g_bValidating = false;
		return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// function AlbumModify( srcform, dtfunct )
//-----------------------------------------------------------------------------
function AlbumModify( srcform, dtfunct )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( AlbumValidate( srcform, dtfunct ) == false )
	{
		CollectError( "Failed to modify the album" );
		g_bValidating = false;
		return false;
	}
	return true;
}

//-----------------------------------------------------------------------------
// AlbumDelete( srcform )
//-----------------------------------------------------------------------------
function AlbumDelete( srcform )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( FutureArtsValidate( srcform ) == false )
	{
		CollectError( "Failed to delete album" );
		g_bValidating = false;
		return false;
	}

	return true;	//srcform.submit();
}


//-----------------------------------------------------------------------------
// function FolderAdd( srcform, dtfunct )
//-----------------------------------------------------------------------------
function FolderAdd( srcform, dtfunct )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( FolderValidate( srcform, dtfunct ) == false )
	{
		CollectError( "Failed to add the folder" );
		g_bValidating = false;
		return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// function FolderMultipleAdd( srcform )
//-----------------------------------------------------------------------------
function FolderMultipleAdd( srcform )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( FolderMultipleValidate( srcform ) == false )
	{
		CollectError( "Failed to add the folders" );
		g_bValidating = false;

		return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// function FolderModify( srcform, dtfunct )
//-----------------------------------------------------------------------------
function FolderModify( srcform, dtfunct )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( FolderValidate( srcform, dtfunct ) == false )
	{
		CollectError( "Failed to modify the folder" );
		g_bValidating = false;

		return false;
	}

	return true;

}


//-----------------------------------------------------------------------------
// FolderDelete( srcform )
//-----------------------------------------------------------------------------
function FolderDelete( srcform )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( FutureArtsValidate( srcform ) == false )
	{
		CollectError( "Failed to delete folder" );
		g_bValidating = false;

		return false;
	}

	return true;//srcform.submit();
}


//-----------------------------------------------------------------------------
// MediaMultipleAdd( srcform )
//-----------------------------------------------------------------------------
function MediaMultipleAdd( srcform )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( MediaMultipleValidate( srcform ) == false )
	{
		CollectError( "Failed to add media" );
		g_bValidating = false;

		return false;
	}
	return true;
}



//-----------------------------------------------------------------------------
// MediaModify( srcform )
//-----------------------------------------------------------------------------
function MediaModify( srcform, dtfunct )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( MediaValidate( srcform, dtfunct ) == false )
	{
		CollectError( "Failed to modify media" );
		g_bValidating = false;
		return false;
	}

	return true;
}


//-----------------------------------------------------------------------------
// MediaDelete( srcform )
//-----------------------------------------------------------------------------
function MediaDelete( srcform )
{
	if( g_bValidating )
		return false;
	
	g_bValidating = true;

	if( FutureArtsValidate( srcform ) == false )
	{
		CollectError( "Failed to delete media" );
		g_bValidating = false;
		return false;
	}

	return true;//srcform.submit();
}

//-----------------------------------------------------------------------------
// JumpCheck( )
// used to validate that you enter a locator when using the jump to site
// if no locator specified, it uses the value in the redirect form field
//-----------------------------------------------------------------------------
function JumpCheck(srcform)
{

/*	if( srcform.loc.value == "" )
	{
		if( srcform.redirect )
			location.href = srcform.redirect.value;
		return false;
	}*/
	
	return true;
}

