//window.onerror = HandleError
function HandleError(){
	return true;
}

var hidemap;
function beginmap(){
	hidemap="no";
}
function endmap(){
	hidemap="yes";
}
//Menus and Navigation***************************************************************
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function navigate(str){
	MM_goToURL('parent',str);return document.MM_returnValue
}
function navigate_external(str){
	MM_openBrWindow(str,'','top=0,left=0,width=1024,height=768,scrollbars=yes,resizable=yes,status=yes')
}
function restartLogin(){
	popupwindow.window.close();
	launchpopup('../Popup_DMS_Login.php',400,230);
}
function BC_swapClass() {
   BC_swapClassRestore()
   endmap();
   var i,j=0,x,a=BC_swapClass.arguments; 
   document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oClassName) x.oClassName=x.className; x.className=a[i+2];}
}
function BC_swapClassRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oClassName;i++) x.className=x.oClassName;
}
function calmenutop(imgid){
	var TT = findObj(imgid)
	var yPos = eval(TT).offsetTop;
	var tempEl = eval(TT).offsetParent;
  	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}
function calmenuleft(imgid){
	var LL = findObj(imgid)
	var xPos = eval(LL).offsetLeft;
	var tempEl = eval(LL).offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}
//End of Menus and Navigation************************************************************

//Generic Functions**********************************************************************
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function tmt_formManager(f,c){
	if(MM_findObj(f)){eval("MM_findObj('"+f+"')."+c);}
}
function tmt_formAction(f,p){
	if(MM_findObj(f)){eval("MM_findObj('"+f+"').action='"+p+"'");}
}
function findObj(theObj, theDoc){
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length){
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}
function reposmenus(){
	newwidth=getWidth();
	if(newwidth!=currentwidth){
	var curloc = document.location.href
    MM_goToURL('parent',curloc); 
	return document.MM_returnValue;
	}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function getHeight(){
	return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
}
function getWidth(){
	return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
}
function close_window() {
    window.close();
}
//End of Generic Functions***************************************************************
//Popup Boxes
function launchpopup(url,w,h){
	var width=w, height=h;
    var left = (screen.width/2) - width/2;
    //var top = (screen.height/2) - height/2;
	var top = 10;
    MM_openBrWindow(url,'','top='+top+',left='+left+',width='+w+',height='+h)
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  popupwindow=window.open(theURL,winName,features);
}
function close_window() {
    window.close();
}
//End of Popup Boxes
//Link to Admin
function gotoadmin(){
	MM_goToURL('parent','Admin/Login.php');return document.MM_returnValue;
}
//End of Link to Admin
//Gallery Functions
var popupwindow;
function changeOutImage(imgname,width,height){
	
	if(popupwindow){
		popupwindow.window.focus();
		popupwindow.window.close();
	}
	 var previewOption=document.getElementById('previewOption').checked
	 if(previewOption==1){
		 document.getElementById('ImageContainer').className="hidden";
		 var popupURL=("PreviewGalleryImage.php?img="+imgname);
		 launchpopup(popupURL,width,height);
	 }else{
     document.getElementById('preview_image').src=(imgname);
	 document.getElementById('preview_image').width=width
	 document.getElementById('preview_image').height=height
	 document.getElementById('ImageContainer').className="";
	 }
}
function clearPopVar(){
	popupwindow=null;
}
function changeCat(str){
	if(!str){
		alert("No Category Selected!");
		return false;
	}
	MM_goToURL('parent',str);return document.MM_returnValue
}
//Contact us functions
function ContactUs(){
	var requiredCount=0;
	var requiredRows=new Array('FirstName_Row','LastName_Row','Email_Row','Enquiry_Row');
	var requiredFields=new Array('FirstName','LastName','Email','Enquiry');
	for (var i=0;i<requiredFields.length;i++) {
		var fieldValue=document.getElementById(requiredFields[i]).value
		if(!fieldValue){
			document.getElementById(requiredRows[i]).style.backgroundColor="#FD7302";
			requiredCount++;
		}else{
			document.getElementById(requiredRows[i]).style.backgroundColor="#E6E7E8";
		}
	}
	if(requiredCount>0){
		alert('The Following Field(s) are Required');
	}else{
		//document.getElementById('Recipient').value="brian@cobussen.biz";
		document.getElementById('Recipient').value="info@waterforce.co.nz";
		tmt_formAction('ContactForm','Mail.php')
		tmt_formManager('ContactForm','submit()');
	}
}
function popLocationData(str){
	var inputData=document.getElementById(str).innerHTML
	newData=inputData.replace(/\n\r/g, "");
	document.getElementById('ContactInfo').innerHTML=newData;
}
function resetLocationData(str){
	var inputData=document.getElementById(str).innerHTML
	document.getElementById('ContactInfo').innerHTML=inputData;
}
//End of Contact Us functions
//Misc
function toggleIntro(){
	var currentClass=document.getElementById('AgIntro').className;
	if(currentClass=="display"){
		document.getElementById('AgIntro').className="hidden";
	}else{
		document.getElementById('AgIntro').className="display";
	}
}
//Gallery Insert calculation
function recaltable(str){
	if(str==0){
		return false;
	}else{
	document.getElementById('imagepanel').width=str
	}
}
//End of Gallery Insert calculation
//News Registration
function unsubscribe(){
	launchpopup('Popup_Unsubscribe.php',400,210)
}
function dosubdetails(){
	var a = document.getElementById('Email').value
	findsubdetails(a,'std')
}
function updatesubscribe(){
	launchpopup('Popup_Update.php',400,210)
}
function updatesubdetails(){
	var a = document.getElementById('Email').value
	findsubdetails(a,'update')
}
//Newsletter Subscription
function CheckSubFields(){
	var requiredCount=0;
	var requiredRows=new Array('FirstName_Row','LastName_Row','Email_Row');
	var requiredFields=new Array('FirstName','LastName','Email');
	for (var i=0;i<requiredFields.length;i++) {
		var fieldValue=document.getElementById(requiredFields[i]).value
		if(!fieldValue){
			document.getElementById(requiredRows[i]).style.backgroundColor="#FD7302";
			requiredCount++;
		}else{
			document.getElementById(requiredRows[i]).style.backgroundColor="#E6E7E8";
		}
	}
	if(requiredCount>0){
		alert('The Following Field(s) are Required');
		return false;
	}else{
		subscribetoNews();
	}
}
function subscribetoNews(){
	var a = document.getElementById('mode').value
	if(a=="Subscribe"){
	document.getElementById('Recipient').value="noreply@waterforce.co.nz";
	tmt_formAction('former','Mail.php');
	tmt_formManager('former','submit()');
	return false;
	}
	else if(a=="Update"){
	tmt_formAction('former','Subscription_Update.php');
	tmt_formManager('former','submit()');
	return false;
	}else{
	tmt_formAction('former','Newsletter_Unsubscribe.php');
	tmt_formManager('former','submit()')
	}
}
//End of Newsletter Subscription

//Endof News Registration
//Preview Enquiry Content
function showPreviewLink(){
	var currentLinkStatus=document.getElementById('PreviewLink').className;
	if(currentLinkStatus=="hidden"){
		document.getElementById('PreviewLink').className="display";
	}else{
		return false;
	}
}
function PreviewContent(){
	launchpopup('Popup_Preview_Enquiry.php',400,420)
}
function PopContent(){
	var EnquiryContent=window.opener.document.getElementById('Enquiry').value;
	EnquiryContent=nl2br (EnquiryContent, 'yes');
	document.getElementById('EnquiryContent').innerHTML=EnquiryContent;
}
function nl2br (str, is_xhtml) {
    var breakTag = '';
 
    breakTag = '<br />';
    if (typeof is_xhtml != 'undefined' && !is_xhtml) {
        breakTag = '<br>';
    }
 
    return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}
//Endof Preview Enquiry Content
//Gallery Popup
function chkimgsize(str){
	HM_UserAgent = navigator.userAgent;
    HM_IsSafari = (HM_UserAgent.indexOf("Safari")!=-1);
    HM_IsFireFox = (HM_UserAgent.indexOf("Firefox")!=-1);
	HM_IsIE = (HM_UserAgent.indexOf("MSIE")!=-1);
	var wsize = str
	if(!str){
		return false;
	}
	var sploc = calmenutop('CBut');
	if(HM_IsIE){
	nwinsize=eval(sploc+120)
	top.resizeTo(610,nwinsize)
	}
	else if(HM_IsFireFox){
	nwinsize=eval(sploc+120)
	top.resizeTo(606,nwinsize)
	}else{
	nwinsize=eval(sploc+120)
	top.resizeTo(610,nwinsize)
  }
}
function recaltable(str){
	if(str==0){
		return false;
	}else{
	document.getElementById('imagepanel').width=str
	}
}
function resetlist(str,wsize){
	var selindex = eval(str-1)
	var a = document.getElementById('ImgSelect');
	a.selectedIndex=selindex
}
function changeProductCat(str,obj){
	var obj=document.getElementById('ProductLister');
	var currentSel=obj.selectedIndex;
	var textSend=document.getElementById('ProductLister').item(currentSel).text
	document.getElementById('ProductID').innerHTML=textSend;
	GalURL="Gallery_Panel.php?productid="+str;
    document.getElementById('GF').src=GalURL;
}
function showimage(str,Gwidth,Gheight){
    document.getElementById('previewContainer').style.backgroundImage="url("+str+")";
	document.getElementById('previewContainer').style.width=Gwidth+"px";
	document.getElementById('previewContainer').style.height=Gheight+"px";
	var BotBut=document.getElementById('CBut');
	if(BotBut){
	chkimgsize(Gheight);
	}
}
//Imbrowser tools
function RestoreContent(){
	LinkCheck=CheckLinks();
	confirmation=confirm('Exit Editor?');
	if((confirmation==true)&&(LinkCheck)){
		alert('There is a broken or missing link in your '+LinkCheck+' Tile!\nPlease use the chain icon in the tool bar to re-create your link.');
		return false;
	}
	else if((confirmation==true)&&(!LinkCheck)){
		SaveOnCancel()
	}
}
function SaveOnCancel(){
	saveconfirm=confirm('Do you wish to save any changes?');
	if(saveconfirm==true){
		document.getElementById('status').value="Saved";
	    tmt_formManager('former','submit()');
	}else{
		Dest=("index.php?CurrentStatus=published")
		MM_goToURL('parent',Dest); 
		return document.MM_returnValue;
	}
}
function RetreiveEditorHeight(){
	var RecordId=document.getElementById('id').value;
	if(!RecordId){
		document.getElementById('saved').style.height=600+'px';
	}else{
		var ContentTop=calmenutop('ContentTop');
		var ContentBottom=calmenutop('ContentBottom');
		var ContentRequired=eval(ContentBottom-ContentTop);
		document.getElementById('saved').style.height=ContentRequired+'px';
	}
	document.getElementById('ContentStoreContainer').className="hidden";
}
function CreateToolBarStyle(){
	var LocationReference=calmenuleft('HeaderImage');
	LocationReferenceAdjust=eval(LocationReference+140)+'px !important;';
	var ss1 = document.createElement('style');
	var def = '.o2k7Skin .mceExternalToolbar {\nposition: fixed !important;\n top:7px !important;\n left: '+LocationReferenceAdjust+'\n z-index:25;!important;\n display:block !important;\n height:0px !important;\n}\n';
    ss1.setAttribute("type", "text/css");
    if (ss1.styleSheet) {   // IE
        ss1.styleSheet.cssText = def;
    } else {                // the world
        var tt1 = document.createTextNode(def);
        ss1.appendChild(tt1);
    }
    var hh1 = document.getElementsByTagName('head')[0];
    hh1.appendChild(ss1);
	//ckeckAppendage()
}
function IncontextSave(){
	LinkCheck=CheckLinks();
	if(LinkCheck){
		alert('There is a broken or missing link in your '+LinkCheck+' Tile!\nPlease use the chain icon in the tool bar to re-create your link.');
		return false;
	}
	AppendMessage="This record has now been updated and is now saved but not currently live.";
	alert(AppendMessage);
	confirmation=confirm('Do you wish to save any edited tiles to your Tile Library?');
	if(confirmation==true){
		document.getElementById('TileSave').value="Yes";
	}
	document.getElementById('status').value="Saved";
	tmt_formManager('former','submit()')
}
function IncontextPublish(){
	Publishconfirmation=confirm('Publish this content and put live?');
	LinkCheck=CheckLinks();
	if((Publishconfirmation==true)&&(!LinkCheck)){
	    AppendMessage="This record has now been updated and is now ";
	    CarryMessage=(AppendMessage + 'live on the website.');
		confirmpublish(CarryMessage)
	}else{
		alert('There is a broken or missing link in your '+LinkCheck+' Tile!\nPlease use the chain icon in the tool bar to re-create your link.');
		return false;
	}
}
function CheckLinks(){
    var ed = tinyMCE.get('saved');
	var CaptionArray=new Array('CaptionOneContainer','CaptionTwoContainer','CaptionThreeContainer','CaptionFourContainer');
	var CaptionIdentity=new Array('Top Left','Top Right','Bottom Left','Bottom Right');
	for (var i=0;i<CaptionArray.length;i++) {
        var el = ed.contentDocument.getElementById(CaptionArray[i]);
		CurrentLinkCount=el.getElementsByTagName("a").length;
	    if(CurrentLinkCount!=2){
			return CaptionIdentity[i];
			break;
		}
    }
}
function confirmpublish(CarryMessage){
	confirmation=confirm('Do you wish to save any edited tiles to your Tile Library?');
	if(confirmation==true){
		document.getElementById('TileSave').value="Yes";
	}
	    alert(CarryMessage);
		document.getElementById('status').value="Published";
		tmt_formManager('former','submit()');
}
function TileManagement(caption,mode){
	var width=850, height=780;
	var top = (screen.availHeight/2-500);
	var top = 10;
    var left = (screen.availWidth/2-425);
	if(mode=="UpdateLinks"){
		var CaptionImage=RetreiveCaptionLink(caption);
	}else{
	    var CaptionImage=RetreiveCaptionImage(caption);
	}
	var url = ("PopupTitleManagement.php?caption="+caption+"&mode="+mode+"&CaptionImage="+CaptionImage);
    MM_openBrWindow(url,'','top='+top+',left='+left+',width='+width+',height='+height);
	return false;
}
function RetreiveCaptionImage(str){
    var ed = tinyMCE.get('saved');
    var el = ed.contentDocument.getElementById(str).getElementsByTagName("td").item(0);
    if(el){
	CurrentImage=el.style.backgroundImage.slice(4, -1); 
	return CurrentImage;
  }
}
function RetreiveCaptionLink(str){
    var ed = tinyMCE.get('saved');
    var el = ed.contentDocument.getElementById(str);
    if(el){
		currentlink=el.getElementsByTagName("a").item(0).href;
	    return currentlink;
  }
}
function RetreiveCaptionTitle(str){
    var ed = tinyMCE.get('saved');
    var el = ed.contentDocument.getElementById(str);
    if(el){
		currentlinktitle=el.getElementsByTagName("a").item(0).title;
	    return currentlinktitle;
  }
}
function updateCaptionImage(CaptionImage,idTarget){
	var imageSRC=("../CaptionImages/"+CaptionImage);
    var ed = tinyMCE.get('saved');
    var el = ed.contentDocument.getElementById(idTarget);
    if(el){
	contentIn=imageSRC;
	ImageIn="url('"+contentIn+"')";
	el.style.backgroundImage="";
	el.removeAttribute("style")
	ed.contentDocument.getElementById(idTarget).getElementsByTagName("td").item(0).removeAttribute("_mce_style");
	RequestedBackground="url('"+contentIn+"')";
	ed.contentDocument.getElementById(idTarget).getElementsByTagName("td").item(0).style.backgroundImage=RequestedBackground;
	ed.contentDocument.getElementById(idTarget).style.height=195+"px";
  }
}
function CheckImageName(){
	var SavedName=document.getElementById('NameReplace').value;
	if(!SavedName){
		alert('Please enter a name that you wish to save this images as');
		return false;
	}else{
		tmt_formManager('former','submit()');
	}
}
function passhtml(CaptionTarget,Content){
	CaptionTarget=CaptionTarget.replace(/Container/g, "");
	CaptionPlacement=(CaptionTarget+"Store");
	CaptionPlaceHolders="\n<a name="+CaptionTarget+"Content></a>\n";
	Content=(CaptionPlaceHolders+Content+CaptionPlaceHolders);
    var ed = tinyMCE.get('saved');
    var el = ed.contentDocument.getElementById(CaptionPlacement);
    if(el){
		el.innerHTML=Content;
  }
}
function PassCaptionLinks(obj,str){
	document.getElementById('LinkSelected').value=str;
}
function DisplayChars(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		var CurrentStrLen=eval(limitNum - limitField.value.length);
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		document.getElementById(limitCount).innerHTML=limitField.value.length;
	}
}

function InsertCaptionLinks(linkref,linktitle,caption) {
    var ed = tinyMCE.get('saved');
	var el = ed.contentDocument.getElementById(caption);
	var NumOfLinks=el.getElementsByTagName("a").length;
	for (var i=0;i<NumOfLinks;i++) {
        if(el){
			el.getElementsByTagName("a").item(i).setAttribute("_mce_href",linkref);
			el.getElementsByTagName("a").item(i).setAttribute("href",linkref);
			el.getElementsByTagName("a").item(i).setAttribute("title",linktitle);
		}
	}
}
function ResetHPContent(str){
	document.getElementById('CurrentStatus').value=str;
	document.getElementById('FSC').src="HPContent.php?status="+str;
}
function RemoveTileImage(FileName){
	confirmation=confirm('Delete this image from the server?');
	if(confirmation==true){
		var curloc = document.location.href;
		Dest=(curloc+"?cmd=del&filename="+FileName)
		MM_goToURL('self',Dest); 
		return document.MM_returnValue;
	}
}
