function opcmsGetSearchContent(sys_category_id, sys_sub_category_id, sys_item_id, sys_template, sys_search_term) {
	jQuery(document).ready(function($) {
		$(function() {
			$.ajax({
				url:  sys_site +"secure/getSearchContent.php",
				global: false,  
				async: true,  
				type: "POST",   
				data: ( {sys_search_term: sys_search_term, sys_category_id: sys_category_id, sys_sub_category_id: sys_sub_category_id, sys_item_id: sys_item_id, sys_template: sys_template  } ),
				dataType: "html",
				success: function(msg) {
					$("#opcms" + sys_item_id).html(msg);
				}
			});
		});
	});
}

function opcmsGetContent(sys_tag, sys_category, sys_sub_category, sys_title, sys_template, fn_callback) {
	jQuery(document).ready(function($) {
		$(function() {
				$(sys_tag).slideUp('slow', function() {
					$.ajax({
						url: sys_site +"manage/AJAXClient/getContent.php",
						global: false,  
						async: false,  
						type: "POST",   
						data: ( {sys_category: sys_category, sys_sub_category: sys_sub_category, sys_title: sys_title, sys_template: sys_template } ),
						dataType: "html",
						success: function(msg) {
							$(sys_tag).html(msg);
							$(sys_tag).slideDown('slow', function() {});
							if (fn_callback) fn_callback.apply();
						}
					});
				});
		});
	});
}

jQuery.fn.highlight = function(id, pat, single) {
	function innerHighlight(node, pat) {
		var skip = 0;
		if (node.nodeType == 3) {
			if (node.data.toLowerCase() != "" ) {
//				var pos = node.data.toLowerCase().indexOf(pat);
				var pos = indexOfWord(node.data.toLowerCase(), pat);

				if (pos >= 0 && !opcmsDictionary) {
					var spannode = document.createElement('span');
					var middlebit = node.splitText(pos);
					var endbit = middlebit.splitText(pat.length);
					var middleclone = middlebit.cloneNode(true);
					
					spannode.appendChild(middleclone);
					middlebit.parentNode.replaceChild(spannode, middlebit);
					
					$(spannode).addClass('highlight');
					$(spannode).addClass('opcmsHighLight_' + id);
					skip = 1;
					if (single) opcmsDictionary = true;
				}
			}
		}
		else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
			for (var i = 0; i < node.childNodes.length; ++i) {i += innerHighlight(node.childNodes[i], pat, single); }
		}
		return skip;
	}

	return this.each(function(i,s) {innerHighlight(this, pat.toLowerCase());});

	function indexOfWord(b, p) {
		for (i=0; i < (b.length-p.length); i++) {	
			if (b.substr(i, p.length) == p) {
					if (b.substr(i + p.length, 1) == " " ||
						b.substr(i + p.length, 1) == "." ||
						b.substr(i + p.length, 1) == "," ||
						b.substr(i + p.length, 1) == ";" ||
						b.substr(i + p.length, 1) == "'" ||
						b.substr(i + p.length, 1) == "<" ||
						b.substr(i + p.length, 1) == ">" ||
						b.substr(i + p.length, 1) == "-") return i;
			}
		}
		return -1;
	}
};

jQuery.fn.removeHighlight = function() {
        return this.find("span.highlight").each(function() {
            this.parentNode.firstChild.nodeName;
            with (this.parentNode) {
                replaceChild(this.firstChild, this);
                normalize();
            }
    }).end();
};

function getObject(id) {
	var oLayer;
	if (document.layers) {
		oLayer = document.layers[id].document;
	}
	else if (parseInt(navigator.appVersion)>=5 && navigator.appName=="Netscape") {
		oLayer = document.getElementById(id);
	}		
	else if (document.all) {
		oLayer = document.all[id];
	}
	return oLayer;
}

function opcmsSearchNextPage(options) {
   jQuery(document).ready(function($) {
		$(function() {
			$.ajax({
				  url: sys_site +"manage/AJAXClient/searchNextPage.php",
				  global: false,  
				  async: true,	  
				  type: "POST",
				  data: options,
				  dataType: "html",
				  processData: true,
				  error: function (XMLHttpRequest, textStatus, errorThrown) { },
				  complete: function (XMLHttpRequest, textStatus, errorThrown) { },
				  success: function(msg) { 
					$("#" + options.sys_layer).html(msg); 
					}
			   });				
		});
	});
}

function opcmsPopulateForm(data) {
	jQuery.each(data, function (name,value) {
		jQuery("input[name='"+name+"'],select[name='"+name+"']").each(function() {
			switch (this.nodeName.toLowerCase()) {
				case "input":
					switch (this.type) {
						case "radio":
						case "checkbox":
						if (this.value==value) { jQuery(this).click(); }
						break;
						default:
						jQuery(this).val(value);
						break;
					}
					break;
				case "select":
					jQuery("option",this).each(function(){
					   if (this.value==value) { this.selected=true; }
					});
				break;
				}
			});
		});
}

function opcmsAccountExists() {
	var _email = $("#frm_email").val();
	var _flag = false;
	if (_email != "") {
		jQuery(document).ready(function($) {
			$(function() {
				$.ajax({
					url: sys_site +"manage/AJAXClient/isUniqueAccount.php",
					global: false,  async: false,	  type: "POST",
					   data: ( {sys_email: _email } ),
					dataType: "xml",
					success: function(xmlDoc) { 
						_exists = xmlDoc.getElementsByTagName("flag")[0].childNodes[0].nodeValue;
					   if (_exists == "true") {
							jAlert('This account already exists.', "New Account");
							_flag = false;
						}
					   else {
							_flag = true;
					   }
					}
				});
			});
		});
	}
	return _flag;
}

function rollOn(current) {if (document.images)	document[current].src = eval(current + '_lit.src');}
function rollOff(current) {if (document.images) document[current].src = eval(current + '_off.src');}
function outpostToggleLayer( whichLayer ) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  return (vis.display==''||vis.display=='block')? false : true;
}

function outpostValidateObject(control) {
	if ($(control).length) {
		$(control).css("background-color", '#ffffff');
		if ($.trim($(control).val()) == '') {    
				$("#formMsg").html("<p>Please fill in all mandatory fields, which are highlighted in red.</p>");
				$("#formMsg").show();

				$(control).css("background-color", '#ffccbe');
				control.focus();
				return false;
		}
		else return true;
    }
    return true;
}

function outpostIsEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|school)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			return false;
	   }
	}
	if (user.match(userPat)==null) {
		alert("The username doesn't seem to be valid.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
			   }
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid.");
			return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	if (len<2) {
		alert("This address is missing a hostname!");
		return false;
	}
	return true;
}

function opcmsLogoutUser(sys_session_id, path) {document.location = path + "?opcms_logout=" + sys_session_id;}
function opcmsLoginUser(path) {document.location = path;}

Date.prototype.format=function(format){var returnStr='';var replace=Date.replaceChars;for(var i=0;i<format.length;i++){var curChar=format.charAt(i);if(replace[curChar]){returnStr+=replace[curChar].call(this);}else{returnStr+=curChar;}}return returnStr;};Date.replaceChars={shortMonths:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],longMonths:['January','February','March','April','May','June','July','August','September','October','November','December'],shortDays:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],longDays:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],d:function(){return(this.getDate()<10?'0':'')+this.getDate();},D:function(){return Date.replaceChars.shortDays[this.getDay()];},j:function(){return this.getDate();},l:function(){return Date.replaceChars.longDays[this.getDay()];},N:function(){return this.getDay()+1;},S:function(){return(this.getDate()%10==1&&this.getDate()!=11?'st':(this.getDate()%10==2&&this.getDate()!=12?'nd':(this.getDate()%10==3&&this.getDate()!=13?'rd':'th')));},w:function(){return this.getDay();},z:function(){return"Not Yet Supported";},W:function(){return"Not Yet Supported";},F:function(){return Date.replaceChars.longMonths[this.getMonth()];},m:function(){return(this.getMonth()<9?'0':'')+(this.getMonth()+1);},M:function(){return Date.replaceChars.shortMonths[this.getMonth()];},n:function(){return this.getMonth()+1;},t:function(){return"Not Yet Supported";},L:function(){return"Not Yet Supported";},o:function(){return"Not Supported";},Y:function(){return this.getFullYear();},y:function(){return(''+this.getFullYear()).substr(2);},a:function(){return this.getHours()<12?'am':'pm';},A:function(){return this.getHours()<12?'AM':'PM';},B:function(){return"Not Yet Supported";},g:function(){return this.getHours()%12||12;},G:function(){return this.getHours();},h:function(){return((this.getHours()%12||12)<10?'0':'')+(this.getHours()%12||12);},H:function(){return(this.getHours()<10?'0':'')+this.getHours();},i:function(){return(this.getMinutes()<10?'0':'')+this.getMinutes();},s:function(){return(this.getSeconds()<10?'0':'')+this.getSeconds();},e:function(){return"Not Yet Supported";},I:function(){return"Not Supported";},O:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+'00';},T:function(){var m=this.getMonth();this.setMonth(0);var result=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,'$1');this.setMonth(m);return result;},Z:function(){return-this.getTimezoneOffset()*60;},c:function(){return"Not Yet Supported";},r:function(){return this.toString();},U:function(){return this.getTime()/1000;}};
function mysqlTimeStampToDate(timestamp) {if (timestamp) {if (timestamp > "") {var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;	var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');	var _d = new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);	return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);}}}

function outpostNextContentPage(options) {
	var sys_layer = options.sys_layer;
	var sys_item_id = (options.sys_item_id ? options.sys_item_id : "");
	options.sys_auto_page = false;
	jQuery(document).ready(function($) {
		$(function() {
			$.ajax({
				  url: sys_site +"manage/AJAXClient/getContentNextPage.php",
				  global: false,  async: true,	  type: "POST",
				  data: ( options ),
				  dataType: "html",
				  success: function(msg) { 
						$("#" + sys_layer).html(unescape(msg));
						$("#opcmsContentSection").html(unescape(msg));
						
						$.ajax({
							  url: sys_site +"manage/AJAXClient/getNextPagination.php",
							  global: false,  async: true,	  type: "POST",
							  data: ( options ),
							  dataType: "html",
							  success: function(msg) {
								  if ($("#outpostPagination").length > 0) {$("#outpostPagination").html(unescape(msg));}
								  if ($("#outpostPaginationTop").length > 0) {$("#outpostPaginationTop").html(unescape(msg));}
								  if ($("#outpostPaginationBottom").length > 0) {$("#outpostPaginationBottom").html(unescape(msg));}
								  
								  $('body').trigger('opcmsEventContentLoaded');
							  }
					   });				
				   }
			   });	
		});
	});
}

