

function callUpdater() {
	var url = '/Action?section=alert&action=archive';
	var params;
	new Ajax.Updater(
	{success: 'IFrame'},
	url,
	{method: 'get',  onFailure: alert_err, onSuccess: alertSuccess});
}


function alertSuccess(transport){
	var s = transport.responseText;
	
	if (s.indexOf("err:Unauthorised") != -1)
	{
		window.location.href = "http://" + document.domain + "/?errorMsg=Please log in first";
	}
}

function callUpdaterURL(url,id) {
	
	new Ajax.Updater(
	{success: id},
	url,
	{method: 'get',  onFailure: alert_err, onSuccess: alertSuccess});
	return false;
}


function showAlerts(a,p){
	var Wrapper = document.getElementById("Wrapper");
	var ac = document.getElementById("alertsContainer");
	var action = a;
	var param = p;
	var useIframe = false;
	var url = a+p;
	
	if(ac){
		Wrapper.removeChild(ac);
	}
	
	var ac = document.createElement("div");
	ac.id="alertsContainer";
	ac.className="box";
	ac.style.display="none";
	
	var top = document.createElement("div");
	top.id="top";
	// if draggable, insert below before close div <div id="pin"></div> and comment in new Draggable below
	top.innerHTML='<div id="pin"><img height="40" width="40" src="/media/images/alerts/clear.gif" title="left click mouse &amp; hold to drag around screen" alt="left click mouse &amp; hold to drag around screen" /></div><div id="close"><a href="#" onclick="Effect.Fade('+"'alertsContainer'"+'); return false;" title="Close">X</a></div>';
	
	var sr = document.createElement("input");
	sr.id="shouldrevert2";
	sr.setAttribute('type', 'hidden');
	
	
	var iframe = document.createElement("div");
	iframe.id="IFrame"
	
	if(useIframe){
	iframe.innerHTML='<iframe src="'+action+param+'" width="430" height="305" frameborder="0" scrolling="no" name="alertcontent"></iframe>'
	}
	else{
		callUpdaterURL(url,"IFrame");
	}

	ac.appendChild(sr)
	ac.appendChild(top);
	ac.appendChild(iframe);
	
	Wrapper.appendChild(ac);
	ac.style.display="block";
		
	//comment in if using draggable	
	new Draggable('alertsContainer',{scroll:window,handle:'pin',revert:function(element){return ($('shouldrevert2').checked)}});
	return false;
}



function ajax_request_get (url,onsuccess,onfailure)
{
	new Ajax.Request(url,
  	{
    method:'get',
    onSuccess: onsuccess,
    onFailure: onfailure 
	}
  );

}

function ajax_request_post (name,onsuccess,onfailure,action,validate)
{

	if(action=='validate'){
		if(! validate(name))
		return false;
	}
	
	
	$(name).request({
  	method:'post',
	onSuccess: onsuccess,
	onFailure: onfailure 
	});
	
	return false;
}


function alert_ret(transport,json)
{

	try
	{
	jsonObj1 = transport.responseText.evalJSON();
	}
	catch(err)
	{
		//alert(err);
		alert("There has been an error");
	} 

	arraylist = jsonObj1.ajaxJSON;
	
	
	for(i=0;i<arraylist.length;i++)
	{

		if(arraylist[i].nag > 0){
			showAlerts(arraylist[i].url,arraylist[i].param);
		}
	
	}

}

function alert_err(transport,json)
{
	alert("There has been an error");
}


function deleteAlert(k){
	var cont = confirm ("Are you sure you wish continue?");
	if(cont){
		ajax_request_get("/DataAction?section=alert&action=delete&id="+k,alertDeleteSuccess);
	}
}

function alertDeleteSuccess(){
	callUpdater();
}

function alert_submitForm(name){
	var f = document.forms[name];
	f.submit();
}

function alert_selectType(param){
	
	url="/Action?section=alert&action=archive&showType="+param.value;

	callUpdaterURL(url,'IFrame')
	return false;
}



function alert_validateForm(name){
	
	var f = document.forms[name];
	var isSelected = -1;
	var SelectedAlerts = f.id;

	if(SelectedAlerts.length){

		for (i=0; i<SelectedAlerts.length; i++) {
			if (SelectedAlerts[i].checked) {
				isSelected = i;
			}	
		}
	}
	else{
		if (SelectedAlerts.checked) 
		isSelected = 0;
	}

	if(isSelected == -1){
		alert("You have not selected any items");
		return false;
	}
	else{
		var cont = confirm ("Are you sure you wish delete the selected items");
		if(cont){
			return true;
		}
		return false;
	}	
}

function alert_Help(topic,src){

	var url="/Action?section=help&action=help&topic="+topic+"&src="+src;
	
	var IFrame = document.getElementById("IFrame");
	var divHelp = document.createElement("div");
	divHelp.id="divHelp";
	IFrame.appendChild(divHelp);

	callUpdaterURL(url,"divHelp");
	
	return false;
}

function callHelpURL(url,id) {
	new Ajax.Updater(
	{success: id},
	url,
	{method: 'get',  onFailure: alert_err});
}

function alert_closeHelp(){
	var IFrame = document.getElementById("IFrame");
	var divHelp = document.getElementById("divHelp");
	IFrame.removeChild(divHelp);
	return false;
}

function alert_changeMeasure(resetdata, imperial)
{
	var f;
	var notice = $('measurechange');
	var w1 = $('weightlabel1');
	var w2 = $('weightlabel2');
	var txtw2 = $('txtweight2');
	var waist = $('waistlabel1');
	
	f = document.forms["singleAlert"];
	
	if (imperial==null)
	{
		
		if(f.ismetric.value=="1")
			imperial=false;
		else
			imperial=true;
	}
	
	if(!imperial)
	{
		notice.update('Click here to change to metric');
		w1.update('&nbsp;&nbsp;stone');
		w2.update('&nbsp;&nbsp;lbs');
		waist.update('&nbsp;&nbsp;ins');
		w2.show();		
		txtw2.show()
		f.ismetric.value="0";
	}
	else
	{
		notice.update('Click here to change to imperial');
		w1.update('&nbsp;&nbsp;kg');
		waist.update('&nbsp;&nbsp;cms');
		txtw2.hide()
		w2.hide();
		f.ismetric.value="1";
	}	
}	

function saveAlert(id){
	ajax_request_get("/DataAction?section=alert&action=save&id="+id);
	callUpdater();
	return false;
}
