


//List.php
//Live search
function GrabSearchResultsInternal()
{
	var searchBox = document.getElementById("liveSearch");
	var searchResults = document.getElementById("searchResults");
	
	//Nothing Entered
	if($('liveSearch').value == "") {
		return 0;
	}
	
	//Too little entered
	if($('liveSearch').value.length < 2) {
		return 0;
	}

	new Ajax.Updater('searchResults', '_ajax/xmlhttp_eventSearch.php', {
					 method: 'post',
					 parameters: {
						 search:   $('liveSearch').value
					 }					 
				});		
	
}


function toggleMe()
{
	$('eventLogDisplay').toggle();
}
function toggleDiv(divName)
{
	$(divName.toString()).toggle();	
}

function toggleDiv_icon(divName)
{

	$(divName.toString()).toggle();	
	
	$(divName.toString() + '_plus').toggle();
	$(divName.toString() + '_minus').toggle();
}

function copyContents()
{
	ShipFirst = form.ShipFirst.value;	
	
}



function resendInvites(type,event_id)
{
	document.getElementById('resendEmailStatus').innerHTML = 'Sending Invitations...';
	var url = "ajax/vc_resend_invites.php";
	var pars = 'type=' + type + '&event_id=' + event_id;
	var myAjax = new Ajax.Updater('resendEmailStatus', url, { method: 'get', parameters: pars });
}


function makeEventRecurring()
{
	Element.toggle('recurring'); 
	Element.toggle('event2_end_date');
	new Effect.Highlight('recurring', {startcolor: '#C7DDED', duration: 2.0});
	new Effect.Highlight('event2_end_date', {startcolor: '#C7DDED', duration: 2.0});
	$('recur_event_checkbox').writeAttribute('value', 'on');	

}


function ResizeTextArea(txtBox)
{
	
	nCols = $(txtBox).cols;
	sVal = $(txtBox).value;
	nVal = sVal.length;
	nRowCnt = 1;
	
	for (i=0;i<nVal;i++)
	{ 
		if (sVal.charAt(i).charCodeAt(0) == 13)  
		{
			alert('return' );
			nRowCnt++;
		}
	}
	/*
	if (nRowCnt < (nVal / nCols)) 
	{ 
		nRowCnt = 1 + (nVal / nCols); 
	}
	*/
	$(txtBox).rows = nRowCnt;
} 

/*
* addEvent
*/

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);


/* Determine view port size */
function viewPortSize(dimension) {
	 var viewportwidth;
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth,
		  viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth,
		   viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	//document.write('<p>Your viewport width is '+viewportwidth+'x'+viewportheight+'</p>');
	
	if (dimension == 'height')
		return viewportheight;
}


/***********************
*
*			Edit Logic
*		  
*
/***********************/
//Add a service to event
function addService(service) {

	var el = document.getElementById(service);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
	el.value = "on";
}
//Removes a Service from an Event
function removeService(type,event_id)
{
	var url = '../ajax/remove_service.php';
	var pars = 'type=' + type + '&event_id=' + event_id;
	var agree = confirm('Are you sure you want to remove this service from this event?');
	if( agree )
	{
		var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars });
		var show = 'show_' + type;
		var remove = 'remove_'+type;
		$(show).style.display = 'none';
		//$(remove).style.display = 'block';
	}		
}


//show the options for a site
function showIssues(id)
{
	if( $('site_issues_'+id).style.visibility == 'visible' )
		$('site_issues_'+id).style.visibility = 'hidden';
	else
		$('site_issues_'+id).style.visibility = 'visible';
}
function updateOtherIssue(site_id)
{
	var text = escape($('other_issue_'+site_id).value);
	var url = '../ajax/update_other_issue.php';
	var pars = 'text=' + text + '&site_id=' + site_id;
	var myAjax = new Ajax.Request(url, { method: 'get',parameters: pars });
}

function toggleSiteIssue(site_count, field )
{
		id = 'vc_issue_' + site_count + '_' + field;
		$(id).value = 0;
		alert(id + " .... " + $(id).value );
}



//Viewing and hiding the edit log.
function showEditLog()
{
	if( $('edit_log').style.visibility == 'visible' )
		$('edit_log').style.visibility = 'hidden';
	else
		$('edit_log').style.visibility = 'visible';
}

//Removes a time from a recurring event, and sets it up as it's own event.
function detachTime(id)
{
	var url = '../ajax/detach_time.php';
	var pars = 'time_id=' + id;
	var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars });
	window.location.reload();
}


//Adds input fields for a user to add a site to the list of video conference endpoints.
//This is a temporary solution, in favor of someone selecting the sites later.
function addSite_edit()
{
	//Get our current number of total sites
	var totalSites = $('total_sites').value;
	totalSites++;

	//Increase the count in the hidden field.
	$('total_sites').value = totalSites;

	//Now actually add the fields
	//Add the header
	var row = $('vc_sites').insertRow(-1);
	var siteCell = row.insertCell(-1);
	siteCell.innerHTML = '<h4>Site ' + totalSites + ' Information</h4>';
	siteCell.colSpan = 2;

	//Add the location
	row = $('vc_sites').insertRow(-1);
	var labelCell = row.insertCell(-1);
	labelCell.innerHTML = '<div class="vc_site_label">Location:</div>'
	labelCell.style.paddingLeft = '10px';
	var inputCell = row.insertCell(-1);
	inputCell.innerHTML = '<div class="vc_site_input"><input type="text" name="vc[sites]['+totalSites+'][location]"></div>';

	//Add the contact name
	row = $('vc_sites').insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = '<div class="vc_site_label">Contact Name:</div>'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = '<div class="vc_site_input"><input type="text" name="vc[sites]['+totalSites+'][contact_name]"></div>';

	//Add the phone number
	row = $('vc_sites').insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = '<div class="vc_site_label">Phone Number:</div>'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = '<div class="vc_site_input"><input type="text" name="vc[sites]['+totalSites+'][phone_number]"></div>';

	//Add the email address
	row = $('vc_sites').insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = '<div class="vc_site_label">Email Address:</div>'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = '<div class="vc_site_input"><input type="text" name="vc[sites]['+totalSites+'][email_address]"></div>';

	//Add the IP address
	row = $('vc_sites').insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = '<div class="vc_site_label">IP Address:</div>'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = '<div class="vc_site_input"><input type="text" name="vc[sites]['+totalSites+'][ip_address]"></div>';

}



/*
Billing
*/
function billing_populate_cart(event_id, event_name, event_date, count) {
	/*
		item_ids = One or more SKU (item ID) for this order
		quantities = (Optional) The quantity for each respective item_id.  Default is 1.
		event_date = The date of the event as it should appear in the work instructions.
		event_name = The name of the event as it should appear in the work instructions.
	*/
	
	/*
	* Extract Billing Information
	*/
	var pars ='event_name=' +  escape(event_name);
	pars += '&event_date=' + escape(event_date);
	
	
	//Traverse billing id's
	for (var i = 0; i< count; i++)
	{
		//Parse item ID from name attribute ( name="billing[ID]" )
		jsid = 'billingid' + i;
		id = $(jsid).name;
		id = id.slice(8, name.length - 1);
		
		//Quantity
		qty = $(jsid).value
		
		if (qty > 0) {
			pars += '&item_ids=' + escape(id);
			pars += '&quantities=' + escape(qty);
		}
	}
	

	//document.write("<b>pars is </b>=>"+pars+"<br>");

	/*
	* Create new Bill (~office)
	*/		
	//Insert into billing
	var url = 'http://at.ufl.edu/~office/video/event.cgi?' + pars;
	var myAjax = new Ajax.Request(url, { 
		method: 'get', 
		onSuccess: function(response) { 
			//Bill Created
			window.billing_id = response.responseText.toString(); // Ajax.Response#responseText -> String			
  		},
		onFailure: function(response) {
			alert("Fail " + response.responseText);	
			return;
		}
  }); //end: Ajax.Request 
	


	
	/*
	*  Store Bill Number
	*/		
	//billing_id = '22804   ';
	//Store Billing ID
		var pars_billing = 'event_id=' + escape(event_id);
		pars_billing += '&billing_id=' + window.billing_id.toString();
		var url_billing = '_ajax/xmlhttp_billing.php?' + pars_billing; 
		
		//alert("pars_billing " + pars_billing);
		//alert("url_billing " + url_billing);
		
		new Ajax.Request(url_billing, { 
					method: 'get', 
					onSuccess: function(response) { 
							var url_order = 'http://at.ufl.edu/~office/billing/order/?act=edt&id='+window.billing_id+'&ret=?act=tbl&sts=c&flt=&ctr=&ord=id';	
							$('billing_createOrder').update('<a href="'+url_order+'" target ="_blank"> View Order: '+window.billing_id+'</a>');
							alert ("Order Number " + response.responseText + " Created ");
							
							//Hide Billing Information
							$('billing_primary').hide();
							$('billing_secondary').hide();
					},
					onFailure: function(response) {
						alert("Fail " + response.responseText);	
						return;
					}
		}); //end: Ajax.Request	
}//end: billing_populate_cart()


function billing_unlink(event_id) {
	
		/*
	*  Store Bill Number
	*/		
	//billing_id = '22804   ';
	//Store Billing ID
		var pars_billing = 'event_id=' + escape(event_id);
		pars_billing += '&billing_id=0';
		var url_billing = '_ajax/xmlhttp_billing.php?' + pars_billing; 
		
		new Ajax.Request(url_billing, { 
					method: 'get', 
					onSuccess: function(response) { 
							$('billing_createOrder').update('<a href="javascript:location.reload(true)">Refresh this page</a>');
							alert ("Order Cleared");
					},
					onFailure: function(response) {
						alert("Fail " + response.responseText);	
						return;
					}
		}); //end: Ajax.Request	
	
}


