/*  This code copyright Brown Paper Bag :: Web Studio
	
	ECN Clarity Version 2.0 Utility Functions Version 1.0
	Created: 24th April 2008
	By: Rob Breuer (Brown Paper Bag) i
	eMail: nfo@brownpaperbag.com.au
	Name: func.utilities.js
	Modified: 
	
*/

//<![CDATA[

/* Extend Prototype Element */

var MyUtils = { 
	isNumber: function(element){ element = $(element); return /^\d+$/.test(element.value); },
	check: function(element){ element = $(element); element.checked = true; return element;	}
};
Element.addMethods(MyUtils);

//General Page load event
Event.observe(document, 'dom:loaded', function(){
	$$('input.is, img.is').each(function(el){
		el.observe('mouseover', swapSrc);
		el.observe('mouseout', swapSrc);
	});
	if ($('filter')) { $('filter').observe('keyup',findSchools); } //$('Filter').observe('focus',findSchools);
	$$('a.nw','area.nw').each(function(el) {
		el.onclick = function() { return (window.open(this.href,"childWindow")) ? false : false; };
		el.onkeypress = function() {return (window.open(this.href,"childWindow")) ? false : false; };
	});
	if ($('filter')) { $('filter').activate();}
});

//Screan Message loader
document.observe('lightview:loaded', function() {
	if ($('ScreenMessages')) {
		$('ScreenMessages').insert({bottom : '<div class="r" style="padding:16px 0;"><input type="button" id="lvc" onclick="Lightview.hide(); return false;" class="btn Ok" /></div>' });
		Lightview.show({ rel: 'inline', href: '#ScreenMessages',
			options: {autosize: true, keyboard: true, overlayClose: true, menubar: false }
		});
	}
});

var PageReload = false;
var PageRedirect = false;
var control = '';
var URL;
Event.observe(document, 'lightview:hidden', function(event){
	if (PageReload) window.location.href=window.location.href;
    if (PageRedirect) window.location.href=URL;
	if (control.length > 0) { 
		var el = $(control);
		control = '';
		if (el) el.click();
	};
	if ($('ValidationErrors')) $('ValidationErrors').update('');
}); 

function showMessage(msg){
		if (!$('ValidationErrors')) $(document.body).insert({ bottom : '<div id="ValidationErrors" class="validationErrors" style="display:none; background-image:url(/img/css/ico/error.48.png);"></div>'});
		$('ValidationErrors').update(msg);
		$('ValidationErrors').insert({bottom : '<div class="r" style="padding:16px 0;"><input type="button" id="lvc" onclick="Lightview.hide(); return false;" class="btn Ok" /></div>' });
		Lightview.show({ rel: 'inline', href: '#ValidationErrors',
			options: {autosize: true, keyboard: true, overlayClose: true, menubar: false }
		});
};

function showElMessage(el){
	elm = $(el);
	elm.insert({bottom : '<div class="r" style="padding:16px 0;"><input type="button" id="lvc" onclick="Lightview.hide(); return false;" class="btn Ok" /></div>' });
	Lightview.show({rel: 'inline', href:'#'+el, options: {autosize: true, keyboard: true, overlayClose: true, menubar: false } });
};

//Validation Error Messages
function showValidationErrors(result,myform){
	if (!result){
		$('ValidationErrors').insert({bottom : '<div class="r" style="padding:16px 0;"><input type="button" id="lvc" onclick="Lightview.hide(); return false;" class="btn Ok" /></div>' });
		Lightview.show({ rel: 'inline', href: '#ValidationErrors',
			options: {autosize: true, keyboard: true, overlayClose: true, menubar: false }
		});
	}
};


function move(e){
	var el = Event.element(e);
	switch(e.keyCode){
		case Event.KEY_DOWN:
			n = $(el).next('a.School');
			if (n) {n.focus(); n.hideFocus=true;}
			Event.stop(e);
			break;
		case Event.KEY_UP:
			n = el.previous('a.School');
			if (n) {n.focus(); n.hideFocus=true;}
			Event.stop(e);
			break;
		case Event.KEY_ESC:
			hideResults();
			$('filter').value = '';
			$('filter').activate();
			Event.stop(e);
			break;
		case Event.KEY_BACKSPACE:
		    $('filter').activate();
		    break;
		default:
			//$('Filter').value = $F('Filter')+e.charCode;
	};
};

function oUrl(){
	var DocURL, protocolIndex, ServerIndex, BeginURL
	this.baseURL = null;
	DocURL = document.URL;
	protocolIndex=DocURL.indexOf("://",4);
	serverIndex=DocURL.indexOf("/",protocolIndex + 3);
	BeginURL=DocURL.indexOf("#",1) + 1;
	this.baseURL=DocURL.substring(BeginURL,serverIndex) + '/';
}

function findSchools(e){
	switch(e.keyCode){
		case Event.KEY_DOWN:
			if ($('findresults')) {
				el = $('findresults');
				first = el.down('a.School');
				if (first) {first.focus(); first.hideFocus=true; }
			}
			break;
		case Event.KEY_ESC:
			hideResults();
			Event.stop(e);
			break;
		default:
		if ($F('filter') != ''){
			var myURL = new oUrl();
			new Ajax.Updater('findresults', myURL.baseURL + '_share/ws/schools.aspx',   
				{ parameters: { Q: $F('filter') }, 
					onSuccess: function(transport){
						//var response = transport.responseText;
						//alert("Success! \n\n" + response); 
						$('findresults').appear({ duration: 0}); 
					},
					onFailure: function(){ alert('Something went wrong...') },
					onComplete: function(){
						$$('a.School').each(function(el){ 
							el.observe('keydown', move); 
						});
					}
			});
		}else{ hideResults();}
	}
};

function hideResults(){ $('findresults').fade({duration:0}); return false; };

function setTips(result,form){
	if (!result) {
		$$('a[rel]').each(function(el) {
			new Tip(el, el.rel, {
				hideOthers: true,
				width:150,
				title: 'Validation Error!'
			});
		});
		alert("There were errors!\n\nFields highlighted red are incomplete or invalid, \nrollover the ? after the field for more information.");
	};
};

function doSearch(){
	$('ContentSearch').submit();
	return false;
}

function showLogon(){
	Lightview.show({
		href: '/logon.aspx',
		rel: 'iframe',
		title: 'Login',
		caption: 'Enter your username and password to login',
		options: {
			autosize: true,
			width: 300,
			height: 200
		}
	});
};

  
function showLogonhelp(){
	Lightview.show({
		href: '/logon.aspx?p=help',
		rel: 'iframe',
		title: 'Login',
		caption: 'Enter your username and password to login',
		options: {
			autosize: true,
			width: 300,
			height: 200}
	  });
 };

function showForgetPassword(){
	Lightview.show({
		href: '/forgetpassword.aspx',
		rel: 'iframe',
		title: 'Forget your password',
		caption: 'Enter your email address to get your password',
		options: {
			autosize: true,
			width: 300,
			height: 200
		}
	});
};

function showUploadImg(sid){
    Lightview.show({
       href:'/myaccount/school/UploadImageFile.aspx?sid='+ sid,
       rel: 'iframe',
       title: 'Upload School Image',
       caption: 'Select school image to upload',
       options:{
          autosize: true ,
          width:350,
          height: 200
        }
     });
};

function showEditPage(nid){
    Lightview.show({
       href:'/Document/AddPage.aspx?nid='+ nid,
       rel: 'iframe',
       title: 'Update Page Content',
       caption: 'Enter new page content',
       options:{
          autosize: true ,
          width:700,
          height: 750
        }
     });
};

function showAddPage(){
    Lightview.show({
       href:'/Document/AddPage.aspx',
       rel: 'iframe',
       title: 'Add New Page Content',
       caption: 'Enter new page content',
       options:{
          autosize: true ,
          width:700,
          height: 750
        }
     });
};

function showSystemField(sid){
    Lightview.show({
       href:'/myaccount/school/NewSystemField.aspx?sid='+ sid,
       rel: 'iframe',
       title: 'Add New System Property',
       caption: 'Enter new system property',
       options:{
          autosize: true ,
          width:350,
          height: 200
        }
     });
};

function showAddRoot(){
    Lightview.show({
       href:'/Document/AddRoot.aspx',
       rel: 'iframe',
       title: 'Add New Root',
       caption: 'Enter new root title',
       options:{
          autosize: true ,
          width:300,
          height: 350
        }
     });
};

//function EditSchoolProfile(sid){
//    Lightview.show({
//       href:'/myaccount/school/EditProfile.aspx?sid='+ sid,
//       rel: 'iframe',
//       title: 'Update school profile',
//       caption: 'Enter updated school profile',
//       options:{
//          autosize: true ,
//          width:350,
//          height: 250
//        }
//     });
//};

function showSystem(el){
	el = $(el);
	url = el.href
	Lightview.show({
		href: url,
		rel: 'iframe',
		title: 'Schools Solar System Profile',
		caption: '',
		options: {
			autosize: true,
			width: 400,
			height: 450
		}
	});
};

function showMap(){
	Lightview.show({
		href: '/map.aspx',
		rel: 'iframe',
		title: 'Schools Map',
		caption: 'Use the map below to locate schools in the program.',
		options: {
			autosize: true,
			width: 700,
			height: 500
		}
	});
};

function showContact(){
	Lightview.show({
		href: '/contact.aspx',
		rel: 'iframe',
		title: 'Contact Us',
		caption: 'Fill in you details below to contact us at solar schools',
		options: {
			autosize: true
		}
	});
	return false;
};

function showModal(url,title,caption){
	Lightview.show({
		href: url,
		rel: 'iframe',
		title: title,
		caption: caption,
		options: {
			autosize: true,
			width: 400,
			height: 600
		}
	});
	return false;
};


function showReport(){
	Lightview.show({
		href: '/myaccount/ReportRunTime.aspx' ,
		rel: 'iframe',
		title: 'solarschools.net reporting',
		caption: 'Select a report form the list.',
		options: {
			autosize: true,
			fullscreen: true
		}
	});
};


// School Page charts
var CurrentChart //= $('a.TodayGreen');
var CurrentConsumption //= $('a.Today');
function showChart(el, chart, command) {
	el = $(el);
	var Chart = el.id.startsWith('myChart');
	if (Chart){
		if (!CurrentChart) CurrentChart = $('myChart_ad');
		if (CurrentChart) CurrentChart.removeClassName('active')
		el.addClassName('active');
		CurrentChart = el;
		
	} else {
		if (!CurrentConsumption) CurrentConsumption = $('myConsumption_ad')	;
		if (CurrentConsumption) CurrentConsumption.removeClassName('active')
		el.addClassName('active');
		CurrentConsumption = el;
	}
	ChartCB.callAsync(null,chart,command);
	return false;
}


/* Image Swapper */
var current = null;
function swapSrc(event) {  // Image swapper
	var o = event.element();
	var ext = o.src.substring(o.src.lastIndexOf("."), o.src.length)
	var path = o.src.substring(0, o.src.lastIndexOf("/")+1)
	var file = o.src.substring(o.src.lastIndexOf("/")+1,o.src.lastIndexOf("."))
	if (o != current){
		o.src = (((i = file.indexOf(".ovr")) >= 0) || ((i = file.indexOf(".on")) >= 0)) ? path + file.substring(0, file.lastIndexOf(".")) + ext : path + file + ".ovr" + ext;
	}
};

       

//]]>