/**************************
*
*
*		Event Request Javascript
*		Includes:
*			-Event Reqeust Validation
*			-Event Request Logic
*			-Edit Logic
*			-Endpoint Search
*
*  
**************************/

/***********************
*
*			Event Request Validation
*		  
*
/***********************/
/***********
*	Validate: 
*		Event Request (General)
************/
function validateME() {alert('ok'); }
function ValidateEventForm() {	
	//debugger;
	var text_fields = ['first_name','last_name', 'phone', 'email', 'dept', 'event_name'];
	var validate = true;
	
	// Salutation
	if($('title').value == "null") 
		validate = IndicateError('title_div'); 
	else 
		ClearError('title'); 

	// Checking general fields
	for(var i=0; i<text_fields.length; i++)
	{
		if($(text_fields[i]).value == "")
			validate = IndicateError(text_fields[i]);
		else 
			ClearError(text_fields[i]);
	}

	// Checking Recur/Single
	if( ( document.service.recur_radio[0].checked == false ) && ( document.service.recur_radio[1].checked == false ) ) 
		validate = IndicateError('recurring_select');
	else
		ClearError('recurring_select');
	
	// Checking Recurring Event
	var validateRecurring
	if ( document.service.recur_radio[1].checked == true )
		validateRecurring = ValidateRecurring();	
	// Checking Services
	var validateServices
	validateServices = ValidateServices();	
	
	//Validate Recurring and Services
	if (validateRecurring == false || validateServices == false)
		validate = false;	

/*	
	//Breaks my heart :'(
	//Validate Date
	if(validateDate() == false)
	 	validate = false;
		*/	
		
	// Check Begin / End Time
	if (validateTime() == false)
		validate = false;

	var date = $('event_month').value + "/" + $('event_day').value + "/" + $('event_year').value;
	if(isDate(date) == false)
	{
		return false;
	}

	
	if(!validate) {
		alert("You have empty fields, please fill in the information where indicated.");
		return false;
	}
	return true;	
}

/***********
*	Validate: 
*		Event Request (Recursion)
************/
function ValidateRecurring() {
		//Validate 
		var validate = true;
	
		//Recurring Selection 
		if( $('recur_select_daily').checked || $('recur_select_weekly').checked || $('recur_select_monthly').checked ) {
			validate = true;
		}
		else {
			validate = IndicateError('recurring');		
		}

		
	
		// Daily Checked
		if( $('recur_select_daily').checked ) {
			//Every ___ day(s)
			if( $('every_blank_days').checked ) {
				if( $('daily_recur_days').value == "") 
					validate = IndicateError('daily_recur_days');
				else 
					ClearError('daily_recur_days');
			}

		} //endif: Daily
		else 
			ClearError('daily_recur_days');


		// Weekly checked
		if( $('recur_select_weekly').checked )
		{
			if( $('weekly_occurrence') == "null") 
				validate = IndicateError('recur_weekly_form');
			else 
				ClearError('recur_weekly_form');

			var weekday_checked = false;
			var weekdays = ['recur_week_0', 'recur_week_1', 'recur_week_2', 'recur_week_3', 'recur_week_4', 'recur_week_5', 'recur_week_6'];
			for(var k=0; k<weekdays.length; k++) {
				if( $(weekdays[k]).checked ) { weekday_checked = true; }
			}

			if(!weekday_checked) 
				validate = IndicateError('recur_checkbox_day');

			else 
				ClearError('recur_checkbox_day');
			
		} //endif: Weekly
		
		// Monthly checked
		if( $('recur_select_monthly').checked )
		{
			//Day ___ of every ___ month(s)
			if( $('monthly_type_day').checked ) {
				if( $('monthly_day_recur').value == "")	
					validate = IndicateError('monthly_day_recur');
				else 
					ClearError('monthly_day_recur');
				
				
				if( $('monthly_day_recur2').value == "")
					validate = IndicateError('monthly_day_recur2');
				else 
					ClearError('monthly_day_recur2');
			}
			else 
				ClearError('monthly_day_recur');
			
			
			
			//The __First__ __Sunday__ of every ____ month(s)
			if ( $('monthly_type_single').checked ) {
				// monthly_single_every_months
				if ( $('monthly_single_every_months').value == "") 
					validate = IndicateError('monthly_single_every_months');
				else 
					ClearError('monthly_single_every_months');
			}
		} //endif: Monthly
		
		//Check Dates
		var validateDates
		validateDates = ValidateRecurringDates();
		if (validateDates == false)
			validate = false;
				
	//alert ("Validation Status: " + validate);
	return validate;	
}

function ValidateRecurringDates() {
		var validate = true;
		
		var beg_month = Number($('event_month').value);
		var beg_day = 	Number($('event_day').value);
		var beg_year = 	Number($('event_year').value);
		
		var end_month = Number($('recur_event_month').value);
		var end_day = 	Number($('recur_event_day').value);
		var end_year = 	Number($('recur_event_year').value);
		
		//alert("Beg_year " + beg_year + " Beg_month " + beg_month + " Beg_day " + beg_day);
		//alert("end_year " + end_year + " end_month " + end_month + " end_day " + end_day);
		
		
		if (beg_year > end_year) {
			validate = false;
		}
		else if (beg_year == end_year) {
			if (beg_month > end_month) {
				//alert(' year = year; begmonth > endmonth');
				validate = false;
			}
			else if ( beg_month == end_month) {
				if( (beg_day >= end_day) || (beg_day == end_day) ) {
					//alert(beg_day >= end_day);
					//alert(beg_day == end_day);
					//alert(' year = year; month = month; begday > endday || beg_day == end_day');
					validate = false;
				}					
			}			
		}
		
		if (!validate) {
			IndicateError('repeat-end');
			alert("Please re-check End Date");			
		}
		else
			ClearError('repeat-end');
		return validate;

}



function validateTime()
{
		var validate = true;
		
		var event_begin_hour = 		Number($('event_begin_hour').value);
		var event_begin_minute = 	Number($('event_begin_minute').value);
		
		var event_end_hour = 		Number($('event_end_hour').value);
		var event_end_minute = 		Number($('event_end_minute').value);
		
		//alert("Beg_year " + beg_year + " Beg_month " + beg_month + " Beg_day " + beg_day);
		//alert("end_year " + end_year + " end_month " + end_month + " end_day " + end_day);
		
		
		if (event_begin_hour > event_end_hour) {
			validate = false;
		}
		else if (event_begin_hour == event_end_hour) {
			if (event_begin_minute > event_end_minute) {
				//alert(' year = year; begmonth > endmonth');
				validate = false;
			}		
		}
		
		if (!validate) {
			IndicateError('repeat-end');
			IndicateError('begin_time');
			alert("Please re-check Begin and End Times");			
		}
		else {
			ClearError('repeat-end');
			ClearError('begin_time');
		}
		return validate;	

}
/***********
*
*	Validate: 
*		Event Reqeust (Services)
*
************/
// checking services
function ValidateServices() {
	validate = true;
	var services = ['vc', 'vp', 'sv', 'sd'];
	var count = 0;
	for(var i=0; i<services.length; i++)
	{
		if($(services[i]).checked == true)	count++;
	}
	if (count == 0) {
		$('service_select').style.border = "2px solid red";
		validate = false;
	}
	
	//Checking Video Conference
	if ($('vc').checked == true) {
		if( $('vcTos').checked == false) {
			$('vcTosP').style.border = "2px solid red";
			validate = false;
		}
	}
	
	if (validate)
		ClearError('service_select')
	return validate;
}

/***********
*	Validate: 
*		Service Request
************/
function ValidateServiceForm()
{
	var text_fields = ['first_name','last_name', 'phone', 'email', 'dept'];
	var validate=true;

	// Checking general fields
	for(var i=0; i<text_fields.length; i++)
	{
		if($(text_fields[i]).value == "")
		{
			$(text_fields[i]).style.border = "2px solid red";
			validate=false;
		}
		else {
			$(text_fields[i]).style.border = "";
		}
	}

	// Checking dependent/unique fields


	// checking salutation
	if($('title').value == "null") {
		$('title').style.border = "2px solid red";
		validate=false;
	} else {
		$('title').style.border = "";
	}
	


	// checking services
	var services = ['ra','cd', 'ec'];
	var count = 0;
	for(var i=0; i<services.length; i++)
	{
		if($(services[i]).checked == true)	count++;
	}
	if (count == 0) {
		$('service_select').style.border = "2px solid red";
		validate = false;
	}
	
	if(!validate) {
		alert("You have empty fields, please fill in the information where indicated.");
		
		return false;
	}
	return true;

}
/***********
*	Validate: 
*		Helper
************/
function IndicateError(divId) {
	div = $(divId);
	div.style.border = "2px solid red";
	return false;
}

function ClearError(divId) {
	div = $(divId);
	div.style.border = "";
}




/***********************
*
*			Event Request Logid
*		  
*
/***********************/
function Recur_Select(type)
{
	if ($('recur_daily_form').style.display != "none") Effect.Fade('recur_daily_form');
	if ($('recur_weekly_form').style.display != "none") Effect.Fade('recur_weekly_form');
	if ($('recur_monthly_form').style.display != "none") Effect.Fade('recur_monthly_form');

	Effect.Appear(type);
	$(type).style.display = "block";
	Effect.Appear('end_date');	
}
// Hides recurring information
function Single_Event() {
	if ($('recurring').style.display != "none")  
		new Effect.Fade('recurring'); 
	if ($('end_date').style.display != "none")  
		new Effect.Fade('end_date'); 	
	if ( $('recur_daily_form').style.display != "none" ) 
		Effect.Fade('recur_daily_form');
	if ( $('recur_weekly_form').style.display != "none" ) 
		Effect.Fade('recur_weekly_form');
	if ( $('recur_monthly_form').style.display != "none" ) 
		Effect.Fade('recur_monthly_form');
} //end: Single_Event()

//
function Single_Event_E2() {
	if ($('event2_end_date').style.display == "none")
		Effect.Appear('event2_schedule');
		
	$('begin_date').innerHTML = "Date";
	
	//Hide all the recurring info
	Hide_Recur();
	
} //end: Single_Event2()

function Recur_Event_E2()
{
	Effect.Appear('recurring');
	$('recurring').style.display = "block";
	
	Effect.Appear('event2_end_date');
	Effect.Appear('recur_title_e2');
	
}

function Recur_Select_E2(type)
{
	if ($('recur_daily_form').style.display != "none") Effect.Fade('recur_daily_form');
	if ($('recur_weekly_form').style.display != "none") Effect.Fade('recur_weekly_form');
	if ($('recur_monthly_form').style.display != "none") Effect.Fade('recur_monthly_form');

	Effect.Appear(type);
	$(type).style.display = "block";
	
	$('begin_date').innerHTML = "Begin Date";
	
	Effect.Appear('recur_info_e2');
	Effect.Appear('event2_schedule');
	Effect.Appear('event2_end_date');
	Effect.Appear('recur_title_e2');
	
}

function Hide_Recur() {
	if ($('recur_title_e2').style.display != "none")
		new Effect.Fade('recur_title_e2');
	if ($('event2_end_date').style.display != "none")  
		new Effect.Fade('event2_end_date'); 
	if ($('recurring').style.display != "none")  
		new Effect.Fade('recurring'); 
	if ($('recur_info_e2').style.display != "none")
		new Effect.Fade('recur_info_e2');
	
	 new Effect.Fade('recur_daily_form');
	 new Effect.Fade('recur_weekly_form');
	 new Effect.Fade('recur_monthly_form');
	 
	 
	 if ($('end_date').style.display != "none")  
		new Effect.Fade('end_date');
	
}

/*Submit */
function enableSubmit() {
	new Effect.Highlight('submit_form');
		if ($('submit_form').disabled=='')
		$('submit_form').disabled='disabled';
	else
		$('submit_form').disabled='';

}



/***********
*	Logic: 
*		Services
************/
function showService(service) {

	var el = document.getElementById(service);

	if (el.style.display != 'none') Effect.Fade(service);
	else Effect.Appear(service, {duration: 1.2 });

	
	//Video Conferencing, focus on live search
	if (service == 'service_vc') {
		setTimeout("$('liveSearch').focus();", 1.3);			
	}
}

function showService_checkbox(service, serviceId) {
	//Show the service

	showService(service);
   
	//Check or uncheck
	var el_id = document.getElementById(serviceId);
	
	if (el_id.checked == true)
		el_id.checked = false;
	else
		el_id.checked = true;
}

//Service Request
//Only display selected service
function showService_radio(service) {

	var el = document.getElementById('service_' + service);
	var el_radio = document.getElementById(service);
	
	//Hide everything
	if ($('service_ra').style.display != 'none' && service != 'ra')Effect.Fade('service_ra');
	if ($('service_cd').style.display != 'none' && service != 'cd')Effect.Fade('service_cd');

	if (el.style.display != 'none') Effect.Fade('service_' + service);
	else Effect.Appear('service_' + service);
}



/**************************
*       
*		Endpoint Database 
*		PUBLIC
*		Endpoint Datbaase search on event request page
*
***************************/
//Used to delay firing of AJAX on each and every keystroke, yuck
var count = 0;
function GrabSearchResultsPublic() 
{		
	//Give it a second, Used to delay firing of AJAX on each and every keystroke, yuck
	count = count + 1;
	$("status").innerHTML = "Loading";
	setTimeout("GrabSearchResultsPublic_Send("+count+")", 400);	
}

function GrabSearchResultsPublic_Send(currCount) {
	//Used to delay firing of AJAX on each and every keystroke, yuck
	if (currCount != count) 
		return 0;		
	else
		count = 0;
		
	//Value is too large or too small
	if($('liveSearch').value.length < 2 || $('liveSearch').value.length > 30 ) {
		return 0;
	}
	
	new Ajax.Updater('searchResults', '_ajax/xmlhttp_searchPublic.php', {
					 method: 'get',
					 parameters: {
						 search: $('liveSearch').value,
						 type:   $('liveSearchType').value					 
					 }			 
					 });
	$('searchResults').show();
	clearResultInfo();
	
	//Clear current result
		
}
//Displays search results in LS_results div. 
//Used after user clicks on a specific result
function LS_resultInfo(objType, objId) {	
	new Ajax.Updater('LS_resultInfo', '_ajax/xmlhttp_searchPublicInfo.php', {
			 method: 'get',
	   
			 parameters: {
				 id: objId,
				 type:   objType					 
			 }			 
			 });
	$('searchResults').hide();
	new Effect.Appear('LS_resultInfo');
	
	//Display group information
	setTimeout("$('groupInfo').toggle();", 300);	
} //end LS_display_info()

//Fetches further information on sub obects (eg group -> location -> building)
function showSubInfo(type, id, count) {	
	//var subInfoDiv = document.getElementById(type + id + "SubInfo" + count);
	var divId = type + id + "SubInfo" + count;
	var url = '_ajax/xmlhttp_searchPublicSubInfo.php';
	
	$(divId).toggle();
	new Ajax.Updater(divId, url, {
			 method: 'get',	   
			 parameters: {
				 id: id,
				 type:   type					 
			 }		 
	 });
} // end showSubInfo()


/********************************************************************
/
/
/ 					liveSearch.js 
/ 					Helper
/
/
*********************************************************************/
//Clears the contents of a div
function clearResult() {
	var result = 	document.getElementById('searchResults');
	result.style.display = 'none';
		
	clearResultInfo();
}

//Clears the contents of the 'LS_resultInfo' div
function clearResultInfo() {
	var resultInfo = 	document.getElementById('LS_resultInfo');
	resultInfo.style.display = 'none';
}
function clearAll() {
	//Clear Result Information
	clearResultInfo();
	//Clear search box
	$('liveSearch').value = '';
	$('liveSearch').focus();
}


//Hides the first dive, shows the second
//Eventually we'll integrate scriptaculous into this
//returns true if now visible, false is now hidden
function alternateDiv(showDivId) {
	var showDiv = document.getElementById(showDivId);
	
	if (showDiv.style.display == 'none') {
		showDiv.style.display = 'block';
		return true;
	}
	
	else if (showDiv.style.display != 'none') {
		showDiv.style.display = 'none';
		return false;
	}

} //end swapDiv()

function alternateLocation(showDivId, iconId, plus, minus) {
	
	var div = document.getElementById(showDivId);
	var icon = document.getElementById(iconId);
	var icon_src = '';
	(div.style.display == "none") ? icon_src=minus : icon_src=plus;
	//document['plus'].src= icon_src;
	//icon.src = icon_src;
	//alert(icon_src);
	
	 
	$(showDivId).toggle();
	//alternateDiv(showDivId);
} //end alternateLocation()


	

/*******************
*
*			VC Sites
*
*********************/

//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 addLocationSite(locationId, locationName, locationIp, groupName)
{
	//Make Selected Sites visible
	$('ls_selected_sites').display = 'block';
	
	//Increase Site Count
	var totalSites = incSiteCount();


	if (totalSites == 1)
		$('vc_sites_div').update();
	
	var removeLink = '<span style="padding-left: 10px; "><a href="javascript:removeSite(\''+totalSites+'\')">[remove]</a></span>';
		//alert(removeLink);
	
	
	//Get information on this site
	$('vc_sites_div').insert('<div id="vc_site_'+totalSites+'" class="vc_site">');
	$('vc_sites_div').insert('<table cellpadding="2" cellspacing="0" id="vc_siteTable_'+totalSites+'"></table></div>');
	//$('vc_site_'+totalSites).insert(removeLink);
	
	
	//Now actually add the fields
	var siteTable = $('vc_siteTable_'+totalSites);	
	
	//Add the header
	var row = siteTable.insertRow(-1);
	var siteCell = row.insertCell(-1);
	siteCell.innerHTML = '<h4 style="display: inline">Site ' + totalSites + ' Information</h4>' + removeLink;
	siteCell.colSpan = 2;

	//Add the location
	row = siteTable.insertRow(-1);
	var labelCell = row.insertCell(-1);
	labelCell.innerHTML = 'Group:'
	labelCell.style.paddingLeft = '10px';
	var inputCell = row.insertCell(-1);
	inputCell.innerHTML = groupName + '<input type="hidden" name="vc[sites]['+totalSites+'][location]" value="'+groupName+'">';
	
	//Add the IP address
	row = siteTable.insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = 'IP:'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = locationIp +'<input type="hidden" name="vc[sites]['+totalSites+'][ip_address]" value="'+locationIp+'">';
	inputCell.innerHTML += '<input type="hidden" name="vc[sites]['+totalSites+'][location_id]" value="'+locationId+'">';
	
	//Remove add Link
	$('location'+locationId).innerHTML ='Added';
	
	//Reset Results
	//clearAll();
	$('LS_resultInfo').update('<b>Site Added</b>. Please search for additional sites. ');
	$('liveSearch').value = '';
	$('liveSearch').focus();
}


//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()
{
	
	//Make Selected Sites visible
	$('ls_selected_sites').display = 'block';
	
	//Get our current number of total sites
	var totalSites = incSiteCount();
	
	if (totalSites == 1)
		$('vc_sites_div').update();
	
	//Removal
	var removeLink = '<span style="padding-left: 10px"><a href="javascript:removeSite(\''+totalSites+'\')">[remove]</a></span>';

	//Now actually add the fields
	$('vc_sites_div').insert('<div id="vc_site_'+totalSites+'" class="vc_site">');
	$('vc_sites_div').insert('<table cellpadding="2" cellspacing="0" id="vc_siteTable_'+totalSites+'"></table></div>');
	var siteTable = $('vc_siteTable_'+totalSites);

	//Add the header
	var row = siteTable.insertRow(-1);
	var siteCell = row.insertCell(-1);
	siteCell.innerHTML = '<h4 style="display: inline">Site ' + totalSites + ' Information</h4>' + removeLink;
	siteCell.colSpan = 2;

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

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

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

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



/*
	
	//Add the verified checkbox
	row = $('vc_sites').insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = 'Verified:'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = '<input type="checkbox" name="vc[sites]['+totalSites+'][verified]">';

	*/
}

//Add or Remove the IPVR
function IPVR()
{	
	if ($('vc_record').checked)
	{
		addSiteIPVR();
	}
	else
	{
		ipvr = $('vc_IPVR').value;
		removeSite(ipvr);
	}
			
}

//Adds the IPVCR (IP Video Recorder) to the video conference
function addSiteIPVR()
{
	/*
	*	Group: IPVCR 
	*	IP: 128.227.156.84
	*	Location ID (epdb): 200
	*/
	//Increase Site Count
	var totalSites = incSiteCount();


	if (totalSites == 1)
		$('vc_sites_div').update();
	
	
	//Get information on this site
	$('vc_sites_div').insert('<div id="IPVR"><div id="vc_site_'+totalSites+'" class="vc_site">');
	$('vc_sites_div').insert('<table cellpadding="2" cellspacing="0" id="vc_siteTable_'+totalSites+'"></table></div></div>');
	
		//Removal
	var removeLink = '';//'<span style="padding-left: 10px"><a href="javascript:removeSite(\''+totalSites+'\')">[remove]</a></span>';
	
	//Now actually add the fields
	var siteTable = $('vc_siteTable_'+totalSites);	
	
	//Add the header
	var row = siteTable.insertRow(-1);
	var siteCell = row.insertCell(-1);
	siteCell.innerHTML = '<h4 style="display: inline">Site ' + totalSites + ' Information</h4>' + removeLink;
	siteCell.colSpan = 2;

	//Add the location
	row = siteTable.insertRow(-1);
	var labelCell = row.insertCell(-1);
	labelCell.innerHTML = 'Group:'
	labelCell.style.paddingLeft = '10px';
	var inputCell = row.insertCell(-1);
	inputCell.innerHTML = 'IP Video Recorder<input type="hidden" name="vc[sites]['+totalSites+'][location]" value="IP Video Recorder">';
	
	//Add the IP address
	row = siteTable.insertRow(-1);
	labelCell = row.insertCell(-1);
	labelCell.innerHTML = 'IP:'
	labelCell.style.paddingLeft = '10px';
	inputCell = row.insertCell(-1);
	inputCell.innerHTML = '128.227.156.84 <input type="hidden" name="vc[sites]['+totalSites+'][ip_address]" value="128.227.156.84">';
	inputCell.innerHTML += '<input type="hidden" name="vc[sites]['+totalSites+'][location_id]" value="200">';
	inputCell.innerHTML += '<input type="hidden" id="vc_IPVR" value="'+totalSites+'">';
}


function incSiteCount()
{

	//Get our current number of total sites
	var totalSites = $('total_sites').value;
	totalSites++;
	//Increase the count in the hidden field.
	$('total_sites').value = totalSites;
	
	return totalSites;
	
}

function removeSite(site_id)
{
	//Dec Site Count
	$('vc_siteTable_'+site_id).remove();	
}

/**************************
*
*
*		Helper
*
*
**************************/

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */


// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// 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 DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}



//A handy function, just like php's print_r
function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}