//-----------------------------------------------------------------------------
// collectform01.js
//
// CollectionsOnline! Form Processing Scripts
// Version 1
//
// Authors:	Stephen Detwiler
//-----------------------------------------------------------------------------

var	g_bValidating = false;
var bSendingFile = false;

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

function isDST ( ts )
{
	var thisday = new Date( );
	thisday.setTime( ts );
	
	var year = y2k(thisday.getYear());

	var lsm = new Date;
	var lso = new Date;
	var day;

	// If earlier than 2007
	if( year < 2007 )
	{
		lsm.setMonth(2);		// March
		lsm.setDate(31);		// 31st
		
		var day = lsm.getDay();	// day of week of 31st
		lsm.setDate(31-day);	// last Sunday

		lso.setMonth(9);		// October
		lso.setDate(31);		// 31st

		day = lso.getDay();		// day of week of 31st
		lso.setDate(31-day);	// last Sunday
	}
	// If equal or after 2007
	else
	{
		lsm.setMonth(2);			// March
		lsm.setDate(1);				// First Day of March
		
		day = lsm.getDay();			// day of week of 1st (6-Sat, 0-Sunday)

		if( day )
			lsm.setDate(15-day);	// Process first day M-Sa
		else
			lsm.setDate(8);			// Process first day Su
	
		lso.setMonth(10);			// November
		lso.setDate(1);
		
		day = lso.getDay();
		if( day )
			lso.setDate(8-day);		// Process first day M-Sa
		else
			lso.setDate(1);			// Process first day Su
	}

	if( thisday < lsm || thisday >= lso ) 
		return false;

	return true;
}

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 ) )
		ts-=3600000;					// spring forward 60 mins (3600 seconds * 1000ms)

	return ts;
}

// assume processing of 'required' has already happened
function processtimefield( srcform_intime, srcform_outtime )
{
	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 );
		if( timestamp < 0 )
			return false;
		
		var thists = new Date( );
		thists.setTime( timestamp );
		hour = thists.getHours();
		if( hour==23 ) {
		timestamp+=3600000;
		}
		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;
}

