
var Calendar = {
	oPopupWindow : null
	
	, Show : function(txt)
	{
		var obj = document.getElementById(txt);
		var sURL = "/RipeCMS/components/calendar.asp?lang=AU&dt=" + obj.value.replace(/\//g, "-") + "&rslt=" + txt;
		
		oPopupWindow = WindowOpenCentre(sURL, 255, 200, 'no', 'no', 'no');
	}
	
	, LoadResult : function(dt, sRsltID)
	{
		try{
			var obj = document.getElementById(sRsltID);
			if(dt != null && dt != undefined)
			{
				if(obj != null)
					obj.value=dt;
			}
			oPopupWindow.close();
		} catch(e) { alert(e); }
	}
	
	, RenderDateControl : function(ID, InitialValue)
	{
		var sSCom = String.fromCharCode(34);
		document.write("<input type='text' value='" + InitialValue + "' name='" + ID + "' id='" + ID + "' onClick='Calendar.Show(this.id);'/>");
		document.write("&nbsp;<input name='" + ID + "' type='button' onClick='Calendar.Show(" + sSCom + ID + sSCom + ");' value='...' />");
	}
}

/* Javascript forms helper */
var FormsHelper = {
	IsValid: false
	, LoadedHTMLScripts: false
	
	// Validates a text field, shows message if empty.
	, ValidateTextField : function(CtrlID, ValMessage)
	{
		var oCtrl = document.getElementById(CtrlID);
		if (oCtrl)
			if (oCtrl.value == '')
			{
				alert(ValMessage);
				oCtrl.focus();
				return false;
			}
		return true;
	}
	
	, SubmitFormAction : function(FormID, Action, ActionValue)
	{
		if (!$get(FormID).hdAction){
			var e = document.createElement("input");
			e.setAttribute("type","hidden");
			e.setAttribute("id","hdAction");
			e.setAttribute("name","hdAction");
			$get(FormID).appendChild(e);
		}
		if (!$get(FormID).hdActionValue){
			var e = document.createElement("input");
			e.setAttribute("type","hidden");
			e.setAttribute("id","hdActionValue");
			e.setAttribute("name","hdActionValue");
			$get(FormID).appendChild(e);
		}

		$get(FormID).hdAction.value = Action;
		$get(FormID).hdActionValue.value = ActionValue;
		$get(FormID).submit();		  
	}
	
	, ValidateEmail : function(emailStr) {
		
		// checks if the e-mail address is valid
		var emailPat = /^(\".*\"|[A-Za-z0-9\_][A-Za-z0-9\.\-\_]*)@(\[\d{1,3}(\.\d{1,3}){3}]|([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,3})$/; //"
		var matchArray = emailStr.match(emailPat);
		if (matchArray == null) {
			//field.select();
			//field.focus();
			alert("Please enter a valid Email address.\n(check the '@' and '.' characters)");
			return false;
		}
		
		// make sure the IP address domain is valid
		var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
		if (IPArray != null) {
			for (var i=1; i<=4 ;i++) 
			{
				if (IPArray[i]>255) 
				{
					//field.select();
					//field.focus();
					alert("Please enter a valid destination IP address.")
					return false;
				}
			}
		}
		return true;
	}
	
	, SetSubmit: function(Bool) {
		FormsHelper.IsValid = Bool;
		return Bool
	}
	
	// Hidden form submitting:
	, RegisterLiveSubmit: function(FormID, SubmitCallback)
	{
		var oFrame = $get('fmSubmitter');
		if (!oFrame || oFrame == undefined)
		{
			oFrame = document.createElement("iframe");
			oFrame.setAttribute("id","fmSubmitter");
			oFrame.setAttribute("name","fmSubmitter");
			oFrame.setAttribute("height","1");
			oFrame.style.visibility = "hidden";
			oFrame.style.height = "1";
			document.body.appendChild(oFrame);
		}
		this._LiveSubmitCallback = SubmitCallback;
		
		// Mozilla, Opera and webkit nightlies currently support this event
		if ( document.addEventListener ) {//alert('moz');
			oFrame.removeEventListener("load", FormsHelper._DoLiveSubmitCallback, false);
			oFrame.addEventListener("load", FormsHelper._DoLiveSubmitCallback, false);
	
		// If IE event model is used
		} else if ( document.attachEvent ) {//alert('ie');
			oFrame.detachEvent("onload", FormsHelper._DoLiveSubmitCallbackIE);
			oFrame.attachEvent("onload", FormsHelper._DoLiveSubmitCallbackIE);
		}
		$get(FormID).target = oFrame.id;
	}
	, _DoLiveSubmitCallback: function(){ 
		if (FormsHelper.IsValid)
			FormsHelper._LiveSubmitCallback($get("fmSubmitter").contentDocument.body.innerHTML); 
	}
	, _DoLiveSubmitCallbackIE: function(){ 
		if (FormsHelper.IsValid)
			FormsHelper._LiveSubmitCallback($get("fmSubmitter").contentWindow.document.body.innerHTML);
	}
	
	, ReloadPage: function() {
		document.location.href = document.location.href;
	}
	
	, OpenWindow: function(url,wd,hg,ov,p,opt)
	{
		// Has scripts??
		/*if (!FormsHelper.LoadedHTMLScripts)
		{
			document.write("<scr" + "ipt src='/RipeCMS/HTMLEditor/scripts/innovaeditor.js'></scr" + "ipt>");
			FormsHelper.LoadedHTMLScripts = true;
		}*/
		var id = "ID"+(new Date()).getTime();
		var f = new ISWindow(id);
		f.iconPath = "/RipeCMS/HTMLEditor/scripts/icons/";
		f.show({width:wd+"px", height:hg+"px",overlay:ov,center:true, url:url, openerWin:p, options:opt});
	}
}

function $get(CtrlID)
{
	var oCtrl = document.getElementById(CtrlID);
	if (oCtrl)
		return oCtrl;
}


function WindowOpenCentre(URL, width, height, resizable, scrollable, status)
{
	// Show edit form:
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=' + resizable;
	params += ', scrollbars=' + scrollable;
	params += ', status=' + status;
	params += ', toolbar=no';
	newwin=window.open(URL, 'Popup', params, true);
	if (window.focus)
		newwin.focus()
	else
		alert("It appears you have a popup blocker installed.  Please add us to your popup blocker.");
	return newwin;
}

function SetCheckboxSet(SetName, Checked)
{
	var chks = document.getElementsByTagName('input');
	for(var idx=0; idx<chks.length; idx++)
	{
		var oInp = chks[idx];
		if (oInp.type == 'checkbox' && oInp.name == SetName)
			oInp.checked = Checked;
	}
}
