
function popup(url,width,height){
    nw = window.open(url,"newwindow","width="+width+",height="+height+",status=0,toolbar=0,menubar=0,scrollbars=1,resizable=1,location=0");
    nw.focus();
}

function checkForm(){
if (document.subscribeForm.elements['Email Address'].value.length > 0){
 return true;
 }
else{
 alert('Please enter your email address.');
 document.subscribeForm.elements['Email Address'].focus();
 return false;
 }
}

function filterNonNumeric(field) {
	var result = new String();
	var numbers = "0123456789";
	var chars = field.value.split(""); // create array
	for (i = 0; i < chars.length; i++) {
	if (numbers.indexOf(chars[i]) != -1) result += chars[i];
	}
	if (field.value != result) field.value = result;
}

function displaynameFilter(field) {
	var result = new String();
	var vals = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var chars = field.value.split(""); // create array
	for (i = 0; i < chars.length; i++) {
	if (vals.indexOf(chars[i]) != -1) result += chars[i];
	}
	if (field.value != result) field.value = result;
}

function emailLookup(val) {
	var fldval = $(val).value;
	var pars = 'v='+fldval;
	var URL = '/ola/ajax/emaillookup/';

	if (!fldval == '') {
    	var myAjax = new Ajax.Request(
		URL,
		{
			asynchronous: true,
			method: 'post',
			parameters: pars,
			onLoading: showLookup,
			onSuccess: showEmailSearchResults
		});
	}
}

function setscrollwidth(holderid, tNum, tWidth, border, margins){
    holder = id2elem(holderid);
    hWidth = ((tNum+1)*tWidth)+(2*border)+(2*margins);
    holder.style.width = hWidth+'px';
}

function tabswitch(set,tNum,total){
    clearTabs(set,total);
    tab = id2elem(set+'_t'+tNum);

    tab.className = 'on';
    section = id2elem(set+'_s'+tNum);
    section.style.display = "block";
}
function roll(sname,rname){
    sname = id2elem(sname);
    sname.src = rname.src;
}
function id2elem(id) {
	if (typeof(id) != 'string') {
		return id;
	}
	if (document.getElementById) {
		id = document.getElementById(id);
	} else if (document.all) {
		id=document.all[id];
	} else {
		id = null;
	}
	return id;
}
function textCounter(field,cntfield,maxlimit) {
	var val = field.value;
  // Adjust newlines so can do correct character counting for MySQL. MySQL counts a newline as 2 characters.
  if (val.indexOf('\r\n')!=-1)
    ; // this is IE on windows. Puts both characters for a newline, just what MySQL does. No need to alter
  else if (val.indexOf('\r')!=-1)
    val = val.replace ( /\r/g, "\r\n" );        // this is IE on a Mac. Need to add the line feed
  else if (val.indexOf('\n')!=-1)
    val = val.replace ( /\n/g, "\r\n" );        // this is Firefox on any platform. Need to add carriage return
  else
    ;                                           // no newlines in the textarea
	if (val.length > maxlimit) { // if too long...trim it!
		field.value = val.substring(0, maxlimit);
		textCounter(field,cntfield,maxlimit);
	}
	// otherwise, update 'characters left' counter
	else
		cntfield.value = maxlimit - val.length;
}

function clearForm(f) {
	$(':input',f).each(function(){
		var type = this.type;
		var tag = this.tagName.toLowerCase();

		if (type == 'text' || type == 'password' || type == 'textarea')
			this.value = "";

		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;

		else if (tag == 'select')
			this.selectedIndex = 0;
	})
}
function getFormData(f) {
	$(':input',f).each(function(){
		var type = this.type;
		var tag = this.tagName.toLowerCase();

		if (type == 'text' || type == 'password' || type == 'textarea')
			this.value = "";

		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;

		else if (tag == 'select')
			this.selectedIndex = 0;
	})
}
function getBranch(id) {
	var cUrl = def_path +'ajax/getbranchclasses';
    var pars = 'id=' + id;

	$("#branchloading").css('visibility','visible');

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "json",
	   success: showBranch
	 });
}
function getSectionSafetys(id) {

	var cUrl = def_path +'ajax/getsectionsafetys';
  var pars = 'id=' + id;

	$("#safetyloading").css('visibility','visible');

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: showSafetys
	 });
}
function getSectionStrategys(id) {

	var cUrl = def_path +'ajax/getsectionstrategys';
    var pars = 'id=' + id;

	$("#catsloading").css('visibility','visible');

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: showStrategys
	 });
}
function getStrategyDetails (safetyid, id) {

	var cUrl = def_path +'ajax/getstrategydetails';
    var pars = 'id=' + id + '&safetyid=' + safetyid;

	$("#detailloading").css('visibility','visible');

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: showStrategyDetails
	 });
}
function getLevel3Details (id) {

	var cUrl = def_path +'ajax/getlevel3details';
    var pars = 'id=' + id;

	$("#detailloading").css('visibility','visible');

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: showLevel3Details
	 });
}
function showSafetys (rStr) {

	$('#safetyloading').css('visibility','hidden');
	$('#safetydropdown').html(rStr);
	$('#safetydropdown').fadeIn("fast");
	$('#catsloading').hide("fast");
	$('#catsdropdown').hide("fast");
	$('#strategyform').hide("fast");

}
function showStrategys (rStr) {

	$('#catsloading').css('visibility','hidden');
	$('#catsdropdown').html(rStr);
	$('#catsdropdown').fadeIn("fast");
	$('#strategyform').hide("fast");
	$('#level3form').hide("fast");

}
function showStrategyDetails (rStr) {

	$('#strategyloading').css('visibility','hidden');
	$('#strategyform').html(rStr);
	$('#strategyform').fadeIn("fast");
	$('#level3form').hide("fast");

}
function showLevel3Details (rStr) {

	$('#level3loading').css('visibility','hidden');
	$('#level3form').html(rStr);
	$('#level3form').fadeIn("fast");

}
function updateSafetyDetails (id) {
	tinyMCE.triggerSave(true,true);
  var details = tinyMCE.get('safetydetails').getContent()
	var cUrl 		= def_path +'ajax/updatesafetydetails';
  var pars 		= 'id=' + id + '&details=' + details;

	$("#safetyloading").css('visibility','visible');
	$('#safetydropdown').hide("fast");
	$('#catsloading').hide("fast");
	$('#catsdropdown').hide("fast");
	$('#strategyform').hide("fast");
	$('#safetyDetailsResponse').hide('fast');

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: afterSafetyDetails
	 });
}
function afterSafetyDetails (rStr) {

	$('#safetyloading').css('visibility','hidden');
	$('#safetyDetailsResponse').html(rStr);
	$('#safetydropdown').fadeIn("fast");
	$('#catsdropdown').fadeIn("fast");
	$('#strategyform').fadeIn("fast");
	$('#safetyDetailsResponse').fadeIn('fast');

}
function updateStrategyDetails (id) {

	tinyMCE.triggerSave(true,true);
  var details = tinyMCE.get('strategydetails').getContent()
	var cUrl 		= def_path +'ajax/updatestrategydetails';
  var pars 		= 'id=' + id + '&details=' + details;

	$("#safetyloading").css('visibility','visible');
	$('#safetydropdown').hide("fast");
	$('#catsloading').hide("fast");
	$('#catsdropdown').hide("fast");
	$('#strategyform').hide("fast");

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: afterStrategyDetails
	 });
}
function afterStrategyDetails (rStr) {

	$('#safetyloading').css('visibility','hidden');
	//$('#sectionid').val('');
	$('#strategyDetailsResponse').html(rStr);
	$('#safetydropdown').fadeIn("fast");
	$('#catsdropdown').fadeIn("fast");
	$('#strategyform').fadeIn("fast");
	$('#strategyDetailsResponse').fadeIn('fast');

}
function updateLevel3Details (id) {

	tinyMCE.triggerSave(true,true);
  var details = tinyMCE.get('level3details').getContent()
	var cUrl 		= def_path +'ajax/updatelevel3details';
  var pars 		= 'id=' + id + '&details=' + details;

	$("#safetyloading").css('visibility','visible');
	$('#safetydropdown').hide("fast");
	$('#catsloading').hide("fast");
	$('#catsdropdown').hide("fast");
	$('#strategyform').hide("fast");
	$('#level3form').hide("fast");

	var myAjax = $.ajax({
	   type: "POST",
	   url: cUrl,
	   data: pars,
	   dataType: "text",
	   success: afterLevel3Details
	 });
}
function afterLevel3Details (rStr) {

	$('#safetyloading').css('visibility','hidden');
	$('#level3DetailsResponse').html(rStr);
	$('#safetydropdown').fadeIn("fast");
	$('#catsdropdown').fadeIn("fast");
	$('#strategyform').fadeIn("fast");
	$('#level3dropdown').fadeIn("fast");
	$('#level3form').fadeIn("fast");
	$('#level3DetailsResponse').fadeIn('fast');

}
function showLoading () {
	$('#branchloading').css('visibility','visible');
}
function showClassLoading () {
	$('#classloading').css('display','block');
}
function showClass(cid, md) {
	if (md == 'edit') {

		$('#classesList').css('display','none');
		$('#classstatus').css('display','none');
		$('#formtitle').html('UPDATE CLASS INFORMATION');
		$('#submitBtn').val('SAVE CHANGES');

		var cUrl = def_path +'ajax/getclass';
	    var pars = 'id=' + cid;
		var myAjax = $.ajax({
		   type: "POST",
		   url: cUrl,
		   data: pars,
		   dataType: "text",
		   success: function(rStr) {
			var json = eval("(" + rStr + ")");

				$.each( json, function(k,v){

					if (k == 'isactive' || k == 'regallowonline') {
						if (v == '1'){
							$('#'+k).val(v);
							$('#'+k).attr('checked', 'true');
						} else {
							$('#'+k).val(v);
							$('#'+k).attr('checked', '');
						}
					} else {
						$('#'+k).val(v);
					}

				});
			}
		});
		$('#startdate').datePicker();
		$('#enddate').datePicker();
		$('#branchid').val("");
		$('#classeditform').fadeIn("fast");

	} else {

		$('#classeditform').css('display','none');
		$('#classesList').css('display','none');
		$('#classstatus').css('display','none');
		$('#formtitle').html('ADD CLASS INFORMATION');
		$('#submitBtn').val('ADD CLASS');
		$('#branchid').val("");
		clearForm($('#classdetails'));
		$('#startdate').datePicker({clickInput:true});
		$('#enddate').datePicker({clickInput:true});
		$('#regstartdate').datePicker({clickInput:true});
		$('#regenddate').datePicker({clickInput:true});
		$('#classeditform').fadeIn("fast");
	}
}
function addClass() {
	var fData = $("#classdetails").serialize();

	if ($('#submitBtn').val() == 'ADD CLASS') {
		var cUrl = def_path +'ajax/addclassinfo';
	    //var pars = 'id=' + id;

		$("#branchloading").css('visibility','visible');

		var myAjax = $.ajax({
		   type: "POST",
		   url: cUrl,
		   data: fData,
		   dataType: "text",
		   success: function(rStr) {
				alert(rStr);
			}
		 });

	} else {
		alert('this was an edit');
	}
}
