// JavaScript Document
// preload arrow image
if (document.images) {
    img1 = new Image();
    img1.src = "images/add.png";
	img2 = new Image();
    img2.src = "images/attention.png";
	img3 = new Image();
    img3.src = "images/collapse.gif";
	img4 = new Image();
    img4.src = "images/expand.gif";
	img5 = new Image();
    img5.src = "images/success.png";
}
function pleaseWait(table,row,cell) {
	var x=document.getElementById(table).rows[row].cells
	x[cell].innerHTML='Please Wait...';
}
function doNav(theUrl) {
	document.location.href = theUrl;
}
function viewDiv(div) {
	document.getElementById(div).style.display="block";
}
function hideDiv(div) {
	document.getElementById(div).style.display="none";
}
function expandCollapse(div,a,exco) {
	if(exco == 'expand') {
		viewDiv(div);
		document.getElementById(a).href='javascript: expandCollapse(\'viewCategories\',\'expandCollapseLink\',\'collapse\');';
		document.getElementById(a).innerHTML='<img src="images/collapse.gif" width="9" height="9" align="absmiddle" border="0">&nbsp;<font size="-2">Hide All Categories</font>';
	} else if(exco == 'collapse') {
		hideDiv(div);
		document.getElementById(a).href='javascript: expandCollapse(\'viewCategories\',\'expandCollapseLink\',\'expand\');';
		document.getElementById(a).innerHTML='<img src="images/expand.gif" width="9" height="9" align="absmiddle" border="0">&nbsp;<font size="-2">View All Categories</font>';
	}
}
function expandCollapseResources(div,a,exco) {
	if(exco == 'expand') {
		viewDiv(div);
		document.getElementById(a).href='javascript: expandCollapseResources(\'viewResources\',\'resourcesLink\',\'collapse\');';
		document.getElementById(a).innerHTML='<img src="images/collapse.gif" width="9" height="9" align="absmiddle" border="0">&nbsp;<font size="-2">Hide Inactive Resources</font>';
	} else if(exco == 'collapse') {
		hideDiv(div);
		document.getElementById(a).href='javascript: expandCollapseResources(\'viewResources\',\'resourcesLink\',\'expand\');';
		document.getElementById(a).innerHTML='<img src="images/expand.gif" width="9" height="9" align="absmiddle" border="0">&nbsp;<font size="-2">View Inactive Resources</font>';
	}
}
function deleteResourceCategory(id) {
	var remove = confirm('You are about to delete a resource category.  This action cannot be undone.  Any resources within this category will be autamatically moved to the Miscellaneous category.  Are you sure you want to delete this category?');
	if(remove == true) {
		doNav('resources.php?action=process&section=delete_cat&id='+id);	
	}
}
function deleteResource(id) {
	var remove = confirm('You are about to delete a resource.  This action cannot be undone and all files will be lost.  Are you sure you want to delete this resource?');
	if(remove == true) {
		doNav('resources.php?action=process&section=delete&id='+id);	
	}
}

function deleteBanner(id) {
	var remove = confirm('You are about to delete this banner.  This action cannot be undone and this image will be lost.  Are you sure you want to delete this banner?');
	if(remove == true) {
		doNav('banners.php?action=process&section=delete&id='+id);	
	}
}
function changeCursor(tableRow, highLight) {
	if (highLight) {
		tableRow.style.cursor="pointer";
	}
}
function openCategory(id) {
	var img = document.getElementById('img'+id);
	if(img.alt == 'expand') {
		viewDiv('div'+id);
		img.src = 'images/collapse.gif';
		img.alt = 'collapse';
	} else if(img.alt == 'collapse') {
		hideDiv('div'+id);
		img.src = 'images/expand.gif';
		img.alt = 'expand';
	} else {
		alert(img.src);	
	}
}
function deleteWhat(id) {
	var remove = confirm('Are you sure you want to delete this "What We Do" section?');
	if(remove == true) {
		doNav('what.php?action=process&section=delete&id='+id);	
	}
}
function deleteThis(el,text) {
	if(confirm('Are you sure you want to delete this '+text+'?')) {
		doNav($(el).href);
	}
	return false;
}
function toggleCheckbox(el,container) {
	if($(el).checked) {
		$(container).show();
	} else {
		$(container).hide();
	}
}

var error1 = 'We encountered an error while communicating with the server. Please try again.';

function addCustomRegInfo(i) {
	var newi = i;
	new Ajax.Request('modules/workshops.php?action=ajax&section=customRegInfo&i='+i, {
		method:'get',
		onSuccess:function(transport) {
			var r = transport.responseText;
			$('customRegInfoContainer').insert(r);
		},
		onFailure:function() { alert(error1); }
	});
	newi++;
	$('customRegInfoA').onclick = function() { return addCustomRegInfo(newi); };
	return false;
}
function removeCustomRegInfo(i) {
	if(confirm('Are you sure you want to remove this custom registration field?')) {
		$('customRegInfo'+i).remove();
	}
	return false;
}
function selectCustomRegInputType(i) {
	var chosen = $F('inputType'+i);
	new Ajax.Request('modules/workshops.php?action=ajax&section=customRegInfoInputType&chosen='+chosen+'&i='+i, {
		method:'get',
		onSuccess:function(transport) {
			var r = transport.responseText;
			$('customRegInfoTypeContainer'+i).update(r);
		},
		onFailure:function() { alert(error1); }
	});
	return false;
}
function addOption(x,i) {
	var newx = x;
	new Ajax.Request('modules/workshops.php?action=ajax&section=addOption&x='+x+'&i='+i, {
		method:'get',
		onSuccess:function(transport) {
			var r = transport.responseText;
			$('optionsContainer'+i).insert(r);
		},
		onFailure:function() { alert(error1); }
	});
	newx++;
	$('optionsA'+i).onclick = function() { return addOption(newx,i); };
	return false;
}
function removeOption(x,i) {
	if(confirm('Are you sure you want to remove this option?')) {
		$('option'+i+'_'+x).remove();
	}
	return false;
}
function addInfoBox(i) {
	var newi = i;
	new Ajax.Request('modules/workshops.php?action=ajax&section=addInfoBox&i='+i, {
		method:'get',
		onSuccess:function(transport) {
			var r = transport.responseText;
			$('infoBoxContainer').insert(r);
			initTiny();
		},
		onFailure:function() { alert(error1); }
	});
	newi++;
	$('infoBoxA').onclick = function() { return addInfoBox(newi); };
	return false;
}
function removeInfoBox(i) {
	if(confirm('Are you sure you want to remove this info box?')) {
		$('infoBox'+i).remove();
	}
	return false;
}
function addAppointmentTime(i) {
	var newi = i;
	new Ajax.Request('modules/workshops.php?action=ajax&section=addAppointmentTime&i='+i, {
		method:'get',
		onSuccess:function(transport) {
			var r = transport.responseText;
			$('appointmentContainer').insert(r);
		},
		onFailure:function() { alert(error1); }
	});
	newi++;
	$('appointmentTimesA').onclick = function() { return addAppointmentTime(newi); };
	return false;
}
function removeAppointmentTime(i) {
	if(confirm('Are you sure you want to remove this appointment time?')) {
		$('appointmentTime'+i).remove();
	}
	return false;
}
function addCouponCode(i) {
	var newi = i;
	new Ajax.Request('modules/workshops.php?action=ajax&section=addCouponCode&i='+i, {
		method:'get',
		onSuccess:function(transport) {
			var r = transport.responseText;
			$('couponCodesContainer').insert(r);
		},
		onFailure:function() { alert(error1); }
	});
	newi++;
	$('couponCodeA').onclick = function() { return addCouponCode(newi); };
	return false;
}
function removeCouponCode(i) {
	if(confirm('Are you sure you want to remove this coupon code?')) {
		$('couponCode'+i).remove();
	}
	return false;
}
function showInfoBox(id) {
	var children = $('infoBoxDetails').childElements();
	children.each(function(element,index) {
		$(element).hide();
	});
	var list = $('infoBoxList').down('ul').childElements();
	list.each(function(el,index) {
		$(el).removeClassName('selected');
	});
	$('infoBox'+id).show();
	$('infoBoxTitle'+id).addClassName('selected');
	return false;
}
function addAnotherAttendee(el,i) {
	var newi = i;
	$('extraGuestsContainer').insert('<div id="attendee'+i+'"><input type="text" name="attendees['+i+']" style="width:200px;" />&nbsp;<a href="#" onclick="return removeAttendee(\''+i+'\');" class="adminlinks red">Remove</a></div>');
	newi++;
	$(el).onclick = function() { return addAnotherAttendee(el,newi); };
	adjustRegistrationPrice();
	return false;
}
function removeAttendee(i) {
	$('attendee'+i).remove();
	adjustRegistrationPrice();
	return false;
}
function getRegistrationPrice() {
	var payment = 0;
	if($('fullPayment').checked) {
		payment = $F('fullPayment');
		payment = payment*1;
		var guests = $('extraGuestsContainer').childElements();
		var guestcount = guests.size();
		if(guestcount > 0) {
			payment = payment + (guestcount * $F('extra_guest_price'));
		}
	} else if($('depositPayment').checked) {
		payment = $F('depositPayment');
		payment = payment*1;
	}
	return payment.toFixed(2);
}
function adjustRegistrationPrice() {
	var payment = getRegistrationPrice();
	var discount = getCouponCodeDiscount();
	payment = payment-(discount*1);
	$('totalContainer').down().update('$'+payment.toFixed(2));
	$('amount_1').value = payment.toFixed(2);
}
function registerForWorkshop() {
	$('registerForm').request({
		onSuccess:function(transport) {
			var r = transport.responseText;
			if(r == '') {
				$('registerForm').action = 'https://www.paypal.com/cgi-bin/webscr';
				$('registerForm').submit();
			} else {
				doNav(r);
			}
		},
		onFailue:function() { alert(error1); }
	});
	return false;
}
function finalizeRegistration(element) {
	var action = $(element).href;
	var value = prompt('Please enter a transaction ID number to finalize this registration.')
	if(value) {
		doNav(action+'&tx_id='+value);
	}
	return false;
}
var searching = false;
function checkCouponCode() {
	if(searching) {
		searching.transport.abort();
		searching = false;
	}
	$('couponCodeResult').update('<img src="images/processing.gif" border="0" alt="Processing..." align="absmiddle" />');
	new Ajax.Request('ajax_coupons.php',{
		method:'post',
		postBody:'coupon_code='+$F('couponCode')+'&id='+$F('workshopId')+'&amount='+getRegistrationPrice(),
		onCreate:function(request) {
			searching = request;
		},
		onSuccess:function(transport) {
			var result = transport.responseText.evalJSON();
			if(!result.status) {
				$('couponCodeResult').update('<span style="color:#cc0000;">'+result.msg+'</span>');
				$('couponCodeDiscount').value = '0';
			} else {
				$('couponCodeResult').update('<span style="color:green">'+result.msg+'</span>');
				$('couponCodeDiscount').value = result.discount;
			}
			adjustRegistrationPrice();
			searching = false;
		},
		onFailure:function() { alert('We encountered an error while searching for this coupon code. Please try again.'); searching=false; }
	});
}
function getCouponCodeDiscount() {
	return $F('couponCodeDiscount');
}
function removeUploadedImage(image) {
	if(confirm('Are you sure you want to remove this image?')) {
		new Ajax.Request('modules/workshops.php?action=ajax&section=removeImage&image='+image,{
			method:'get',
			onComplete:function() {
				$('currentImageContainer').remove();
			}
		});
	}
	return false;
}
/* uploader functions */
var fileID;
var finishUpload = false;
function onFileSelect(obj) {
	for(var item in obj.fileList) {
		$('fileContainer').value = obj.fileList[item].name;
		fileID = obj.fileList[item].id;
		$('fileNameContainer').update('Filename: '+obj.fileList[item].name+' (<span id="uploadProgressContainer">0</span>%) <a href="#" onclick="return cancelFileUpload(\''+fileID+'\');">cancel</a><div id="progressBar"><div id="innerProgressBar"></div></div>');
		uploader.disable();
		var accountid = $('accountid').value;
		$('fileName').value = '/home/karnanas/dashboard_files/'+accountid+'/'+obj.fileList[item].name;
		uploader.upload(fileID,'/ajax_dashboard.php?accountid='+accountid);
	}
}
function onUploadStart() {

}
function onUploadProgress(obj) {
	var total = obj.bytesTotal;
	var loaded = obj.bytesLoaded;
	var percent = loaded/total;
	var width = percent*200;
	$('uploadProgressContainer').update((percent*100).round());
	$('innerProgressBar').setStyle({width:width.floor()+'px'});
}
function onUploadComplete(obj) {
	var basename = $F('fileContainer');
	$('uploadProgressContainer').update('100');
	$('innerProgressBar').setStyle({width:'200px'});
	$('fileContainer').value = '';
	setTimeout(function() {
		if(finishUpload) {
			$('fileForm').submit();
			$('pleaseWait1').update('Completing process. Please wait');
		} else {
			$('fileNameContainer').update('Filename: '+basename+' <a href="#" onclick="return removeFileUpload(\''+$F('fileName')+'\');">remove</a>');
			$('uploaderContainer').hide();
		}
	},500);
}
function cancelFileUpload(id) {
	uploader.cancel(id);
	uploader.removeFile(id);
	uploader.enable();
	$('fileContainer').value = '';
	$('fileNameContainer').update('');
	return false;
}
function removeFileUpload(file) {
	new Ajax.Request('ajax_dashboard.php?action=removeFileUpload&file='+file,{
		onSuccess:function(transport) {
			var response = transport.responseText;
			if(response != '') {
				alert(response);
			} else {
				$('fileContainer').value = '';
				$('fileNameContainer').update('');
				$('fileName').value = '';
				$('uploaderContainer').show();
			}
		},
		onFailure:function() { alert(com_error); }
	});
	return false;
}
function checkForFile() {
	if($F('fileContainer') != '') {
		finishUpload = true;
		$('pleaseWait1').update('<div class="required">Please wait while the file finishes uploading.</div>').show();
		return false;
	}
}
function submitForm(id) {
	$(id).submit();
	return false;
}
