
	
/*  Basic Setup Javascript
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * --> Sets up nice IE friendly suckerfish drop downs 
 *
 * --> Hides all elements that have the class 'hide' 
 *
 * --> Enables the Ajax Form Styles 'noI' for forms with no instructions 
 *
 * --> Test for cookies enabled 
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

var require_cookies = true;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Event.observe(window, 'load', function() {


	// Standards-based external links (i.e. target="_blank" fix)
	function externalLinks() 
	{
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
		}
	} 
	externalLinks();
	function externalLinks2() 
	{
		$$('a').each( function(el) {
			if( el.getAttribute("rel") && el.getAttribute("rel")=="external" ) {
				el.target = "_blank";
			}
		});
	} 

	// Hides Borders on Links
	Event.observe( document , 'focus' , function(event) {
		var elt = Event.element(event);
		//alert(elt.tagName);
		if(elt.tagName=="SELECT") return;
		if($(elt).blur) $(elt).blur();
	});


	// The Oh My God! CSS Image Flicker Fix for IE6
	if(document.all) document.execCommand("BackgroundImageCache",false,true);

	
	// Suckerfish
	$$("#sf li").each( function(el) {
		el.onmouseover = function() { this.addClassName('over'); };
		el.onmouseout = function() { this.removeClassName('over'); };
	});

	// IE Hover on List Elements
	$$("li.hover").each( function(el) {
		el.onmouseover = function() { this.addClassName('over'); };
		el.onmouseout = function() { this.removeClassName('over'); };
	});
	$$("form li").each( function(el) {
		el.onmouseover = function() { this.addClassName('focused'); };
		el.onmouseout = function() { this.removeClassName('focused'); };
	});

	// Hide Elements
	document.getElementsByClassName('hide').invoke('hide');
	document.getElementsByClassName('hide').each( function(s) { s.removeClassName('hide'); });


	// Form Instructions
	if(document.getElementsByClassName('instruct').length>0) { $$('body').invoke('removeClassName','noI');	}
	else { $$('body').invoke('addClassName','noI'); }


	// Get all forms and loop through to create a 'Please Wait...' disabled button.
	var Forms = $$('form').each(function( elm , index ) 
	{
		Event.observe( elm , 'submit' , disable_submit.bind(this) , false ); // Listen for all form submission
	});
	function disable_submit(ev)  
	{
		var frm = Event.element(ev);		
		var  button  =  frm.elements['submit'];
		button.value  =  'Please Wait...';
		button.disabled  =  'true';
		return  true;       
	} 


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *
 *		Ajax form handler...
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


	function Hijax() {
	
		var container,url,canvas,data,loading,callback,request;
	
		this.setContainer = function(value) {
			container = value;
		};
		this.setUrl = function(value) {
			url = value;
		};
		this.setCanvas = function(value) {
			canvas = value;
		};
		this.setLoading = function(value) {
			loading = value;
		};
		this.setCallback = function(value) {
			callback = value;
		};
		this.captureData = function() {
			if($( container ).tagName.toLowerCase()=="form") var data = $(container).serialize();
		};

	};
	
	
	// Get all forms with the class 'odc' and loop through
	var ajaxForms = $$('form.ajax').each(function( elm , index ) {
		
		Event.observe( elm , 'submit' , ajax.bind(this) , false ); // Listen for all form submission

	});
	if(!ajaxForms) return;	


	//var validator = new Validation( ajaxForms[0].id ,{useTitles:true,onSubmit:false});

	
	function ajax(ev) { 
			
		//alert(navigator.userAgent);
		//var safari = true;

		// Stop form submission temporarily...
		Event.stop(ev);

		// Form element of submission, could be an input or the form itself...
		var element = Event.element(ev);		
		
		// ... so Get Form Element, var: form
		if( element.tagName != "FORM" ) {
			var form = element.ancestors().find( function(x) { 
				if(x.tagName=="FORM") return true;
			});
		} else {
			var form = element;
		};		

		
		// Set Processing DIV
		var notice = $('notice_'+form.id);
		notice.addClassName('processing').update( 'Processing...' );
		notice.scrollTo();

		
		// Set Form Method to "ajax" so PHP can process correctly (to do: is this necessary?)
		if($('form_method')) $('form_method').value = "ajax";
		if($('form_id')) $('form_id').value = form.id;


		// Do the AJAX thing...
//		new Ajax.Request( 'php/class.form.handler.php' , {
		new Ajax.Request( window.location , {
		
			parameters: form.serialize(true) , // Serialize Form Elements
		
			onFailure: function() 
			{
				alert('Bad things are afoot at the Circle K.');
			},
		
			onSuccess: function(transport) 
			{
				var notice = $('notice_'+form.id);
				notice.removeClassName('processing').update( transport.responseText ).scrollTo();
			}
		
		});


		return;
		// ==================================
				
	}

});




// Drag and Drop Functions (in conjuction with in-page javascript)

function createNewSection(name) {
		if(!name) { var name = $F('new_category'); }
		if(name) {
			var newDiv = Builder.node('div', {id: 'group' + (sections.length + 1), className: 'sortSection' }, [
				Builder.node('h3', { className: 'handle' }, name )
			]);
			sections.push(newDiv.id);
			//$('drag').appendChild(newDiv);
			
			$('drag').insertBefore(newDiv, $('drag').down().next(1) );
			Effect.Appear(newDiv.id);
			destroyLineItemSortables();
			createLineItemSortables();
			createGroupSortable();
		}
}

function createLineItemSortables() {
	for(var i = 0; i < sections.length; i++) {
		Sortable.create(sections[i],{tag:'div',dropOnEmpty: true, containment: sections,only:'lineitem'});
	}
}

function destroyLineItemSortables() {
	for(var i = 0; i < sections.length; i++) {
		Sortable.destroy(sections[i]);
	}
}

function createGroupSortable() {
	Sortable.create('drag',{tag:'div',only:'sortSection',handle:'handle'});
}

// Debug Functions for checking the group and item order

function getGroupOrder() {
	var sections = document.getElementsByClassName('sortSection');
	var alerttext = '';
	var del = 0;
	var delete_message = '';
	sections.each(function(section) {
		var sectionID = section.id;
		var order = Sortable.serialize(sectionID);
		var sequence = Sortable.sequence(section);
		if(del) { del++; delete_message += sectionID + ', '; }
		alerttext += sectionID + ': ' + sequence + '\n';
		if(sectionID=="garbage") { 
			del+=sequence.length; 
			delete_message += sequence;
		}
	});
	alert(alerttext + '\n' + delete_message + ' (' + del + ')' );
	return false;
}



// Cookies

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires ) // in hours
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) ) 
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
			
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Check_Cookie() {
	if( Get_Cookie( 'test' ) ) return true;
	Set_Cookie( 'test', 'it works', '', '/', '', '' );
	if( Get_Cookie( 'test' ) ) return true;
	return false;
}

function Cookie() {
	
	if(!Check_Cookie()) {
		document.write('<style type="text/css"> body, html{height:100%; overflow:hidden} .graph, .map{height:0 !important} select{visibility:hidden} </style>');
		document.write('<div class="overlay"></div><div class="lightbox done"><div class="lbContent">');
		document.write('<h2>Hello.</h2><p>');
		document.write('This website needs <strong>cookies</strong> enabled to perform its best. ');
		document.write('If you don\'t mind, please enable them in your browser, and <a href="#" onclick="window.location.reload()">reload this page</a>.</p>');
		document.write('</div></div>');
	}

}
if(require_cookies) Cookie();




function event_timer() 
{
		now=new Date();
		hour=now.getHours();
		min=now.getMinutes();
		sec=now.getSeconds();
	
		if (min<=9) { min="0"+min; }
		if (sec<=9) { sec="0"+sec; }
		if (hour>12) { hour=hour-12; add="pm"; }
		else { hour=hour; add="am"; }
		if (hour==12) { add="pm"; }
	
		time = ((hour<=9) ? "0"+hour : hour) + ":" + min + ":" + sec + " " + add;
	
		if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }
		else if (document.layers) {
		document.layers.theTime.document.write(time);
		document.layers.theTime.document.close(); }
	
		setTimeout("event_timer()", 1000);
}

