// Filename: incJSFuncs.js
// Copyright: 2002 CreativEngine Pty. Ltd.

// Generic form validation functions

  function clearq(text) {
    var old = "";
    var clear = text;
    var dudDash = "";
    
    dudDash = String.fromCharCode(8211);
    
    while(clear != old){
      old = clear;
    	clear = clear.replace("’", "'");
    	clear = clear.replace(dudDash, "-");
    }
		    
    return clear;
  }

function isEmpty(strValue) {
	if (strValue.length > 0) {
		return false;
	} else {
		return true;
	}
}

function isEmail(strVal) {
	var strRegTestInteger = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return strRegTestInteger.test(strVal);
}

function isPassword(strVal) {
	var strRegTestInteger = /^\w{6,}$/;
	return strRegTestInteger.test(strVal);
}

function isChecked(objField) {
	return objField.checked;
}

function notValidDate(intDay, intMonth, intYear) {
	/* returns 0 if date is valid
	** returns 1 if general invalid date
	** returns 2 if leap year broken
	*/
	switch (intMonth) {
		case "4":
		case "6":
		case "9":
		case "11":
			if (parseInt(intDay) > 30)
				return 1;
			break;
		case "2":
			if (parseInt(intDay) > 29)
				return 1;

			if (parseInt(intDay) == 29) {
				if (intYear.substr(intYear.length-2, intYear.length) == "00") {
					if (parseInt(intYear.substr(0, 2)) % 4 == 0)
						return 0;
					return 2;
				}
				else
					if ((parseInt(intYear) % 4) == 0)
						return 0;
					return 2;
			}
			else
				return 0;
	}
	return 0;
}

function openWin(strFilename, strWindowName, intWidth, intHeight) {
	var newWin = window.open(strFilename, strWindowName, "toolbar=0, location=0, directory=0, status=0, menubar=0, scrollbars=1, resizable=1, width=" + intWidth + ", height=" + intHeight);
}

function markupText(objField, strTag) {
	//Called by admin textbox format buttons
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		document.selection.createRange().text = "<" + strTag + ">" + strSelectedText + "</" + strTag + ">";
		objField.focus();
	}
}

function replaceLIs(strText) {
	var strRet = "";

	for (var i=strText.indexOf("\n") ; strText!="" ; i=strText.indexOf("\n")) {
		if (i == -1) {
			strLine = strText;
			strText = "";
			strRet = strRet + strLine.replace(/^(.*)$/, "<li>$1</li>");
		}
		else {
			strLine = strText.substr(0, i);
			strText = strText.substr(i+1, strText.length);
			strRet = strRet + strLine.replace(/^(.*)\r$/, "<li>$1</li>\n");
		}
	}
	return strRet;
}

function markupTextList(objField, strTag) {
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		document.selection.createRange().text = "<" + strTag + ">\n" + replaceLIs(strSelectedText) + "\n</" + strTag + ">";
		objField.focus();
	}
}

function markupTextLink(objField) {
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		var strURL = prompt("URL:", "http://");
		if (strURL != null)
			document.selection.createRange().text = "<a href=\"" + strURL + "\">" + strSelectedText + "</a>";
		objField.focus();
	}
}

function markupTextMailTo(objField) {
	var strSelectedText = document.selection.createRange().text;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		var strURL = prompt("Email Address:", "mailto:");
		if (strURL != null)
			document.selection.createRange().text = "<a href=\"" + strURL + "\">" + strSelectedText + "</a>";
		objField.focus();
	}
}


function markupTextEscapeChars(objField) {
	var strSelectedText = document.selection.createRange().text;
	var reQuote = /\"/g;
	reQuote.ignoreCase = true;
	reQuote.multiline = true;

	var reLT = /</g;
	reLT.ignoreCase = true;
	reLT.multiline = true;

	var reGT = />/g;
	reGT.ignoreCase = true;
	reGT.multiline = true;

	if (isEmpty(strSelectedText)){
		alert("You must highlight some text in the textbox first.");
		objField.focus();
		return false;
	}
	else {
		document.selection.createRange().text = strSelectedText.replace(reQuote, "&quot;").replace(reLT, "&lt;").replace(reGT, "&gt;");
		objField.focus();
	}
}
//Used to link already uploaded images from the library to the page
//keep track of cursor position
function markTextAreaPosition(txtObj){
  if(txtObj.createTextRange)
    txtObj.caretPos = document.selection.createRange().duplicate();
}
//select and add a file/image
function markupTextAddImageFile_nonWYSIWYG(objField, pType, returnType, pDir){
  /*
    pType: either 'file' or 'image'
    
    returnType: specify if it is either a textarea or label.  It would be a label when selecting images for structured pages
    returnType: values: 'text', 'sp'
    If returnType=='sp' then the objField is just the name of the field, not the object.
  */

/*  if(returnType=='text'){
this type should use other function
    objField.focus();
    if(objField.createTextRange && objField.caretPos){
  		var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
      if(strURL){
        var caretPos = objField.caretPos;
        caretPos.text = caretPos.text + '<code type="' + pType + '" id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strURL.substring(strURL.indexOf('|')+1) + '"></code>';
      }
      markTextAreaPosition(objField);
      objField.focus();
    }else{
      alert('Please put the cursor in a textbox where you wish to put the item.');
    }
  }*/
  if(returnType=='sp'){
  	var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
  	var objLabel;
  	var objHiddenID;
  	var objHiddenName;
  	var newID, newName;
  	
    if(strURL){
    	objLabel = document.getElementById('lbl' + objField + 'NewName');
    	objHiddenID = document.getElementById(objField); 
    	objHiddenName = document.getElementById('txt' + objField + 'NewName');
  	  
  	  newID = strURL.substring(0,strURL.indexOf('|'));
  	  newName = strURL.substring(strURL.indexOf('|')+1);
  	  objLabel.innerText = newName;
  	  objHiddenID.value = newID;
  	  objHiddenName.value = newName;
  	}
  }
}

function markupTextAddLink(objField, pDir){
// shows popup to selct a page via  Zone/Category/Page.  Put into textarea on _admin/PageEdit2.asp
  objField.focus();

  if(objField.createTextRange && objField.caretPos){
  	var strSel = document.selection.createRange().text;
		var strURL = window.showModalDialog(pDir + 'LinkSelectionPopup.asp?t=' + strSel,'dialogWidth:500px;dialogHeight:500px;help:no');
    if(strURL){
      var caretPos = objField.caretPos;
      Zone = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      Cat = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      Page = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      //strURL is left with href text
      caretPos.text = '<code type="link" src="' + Zone + '|' + Cat + '|' + Page + '">' + strURL + '</code>';
    }
    markTextAreaPosition(objField);
    objField.focus();
  }else{
    alert('Please put the cursor in a textbox where you wish to put the item.');
  }
}

/*
****************************************************************
NEW WYSIWYG FUNCTIONS \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/  \/ 
****************************************************************
*/

var selectionRange;

objWYSIWYG.Registry = [];

function objWYSIWYG(pstrIFrameId, pstrTextAreaId, pstrPath){
	
	this.id = pstrIFrameId;
	this.design = true;
	this.objIframe = window[pstrIFrameId].document;
	this.objTarea = document.getElementById(pstrTextAreaId);
	this.cssPath = "<link href=\"" + pstrPath + "../css/default.css\" rel=\"stylesheet\" type=\"text/css\"><style media=\"all\" type=\"text/css\">body { text-align: left; background-color: #fff; }</style>"
	this.imagePath = pstrPath;
	objWYSIWYG.Registry[pstrIFrameId] = this;
	
	this.load();
	
}
// Inherited Method - Sets up editable area

objWYSIWYG.prototype.load = function() {
	
	var editControl_RichText = frames [this.id].document;
	this.objIframe.designMode = "On";
	
	this.objIframe.open();
	this.objIframe.write(this.cssPath + this.objTarea.value);
	this.objIframe.close();
	
	var tempVar = this.id;
	
	editControl_RichText.documentElement.attachEvent("onpaste", function(){objWYSIWYG.pasteWordText(tempVar);});

}

objWYSIWYG.getIFrameDocument = function(aID)
{
		return document.frames[aID].document;
} 

objWYSIWYG.pasteWordText = function(id) {
	
	
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[id];
	
	//selectionRange = objWYSIWYG.getIFrameDocument(id).selection.createRange().duplicate();
	
	var strReturnString = "";

	document.getElementById("tempArea" + obj.id).value = window.clipboardData.getData("Text")
	
	strReturnString = document.getElementById("tempArea" + obj.id).value;
	
	strReturnString = strReturnString.replace(/\r/g, '\n');
	
	window.clipboardData.setData("Text", strReturnString)
	
	setTimeout('updateClipboard("' + obj.id + '")', 50) 
}

function updateClipboard(id){
	//alert(document.getElementById("tempArea" + id))
	window.clipboardData.setData("Text", document.getElementById("tempArea" + id).value)

}

// Method - Switched between HTML and Design Mode

objWYSIWYG.toggleDesign = function(id, objIFrame) {
	
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[id];
	
	if (obj.design) {
		obj.design = false;
		var sContents = objIFrame.document.body.innerHTML;
		objIFrame.document.open();
		objIFrame.document.write("<BODY style=\"font:9pt courier, monospace\">");
		objIFrame.document.close();
		objIFrame.document.body.innerText = sContents;
	} else {
		obj.design = true;
		var sContents = objIFrame.document.body.innerText;
		objIFrame.document.open();
		objIFrame.document.write(obj.cssPath);
		objIFrame.document.close();
		objIFrame.document.body.innerHTML = sContents;
	}
	
}

objWYSIWYG.setTitle = function(pstrTag, pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("FormatBlock", 0, "<" + pstrTag + ">");
}

objWYSIWYG.setColor = function(pstrHexColor, pstrFrameId) {
	if (pstrHexColor != "") {
		objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("bold");
		objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("ForeColor", 0, pstrHexColor);
	} else {
		objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("RemoveFormat");
	}
}

// adds an ABBR tag around selected text
objWYSIWYG.setAbbv = function(pstrFrameId) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	var newString = selectionRange.htmlText;
	if ( newString.length > 0 ){
		var pstrTitle = prompt("Please enter the full description of " + newString + ":", "") ;
		if (pstrTitle != null)
			selectionRange.pasteHTML("<acronym title=\"" + pstrTitle + "\">" + newString + "</acronym>");
	}else
		alert("Please select some text first.");
}

objWYSIWYG.setSpan = function(pstrTag, pstrAttr, pstrFrameId) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	
	if (pstrAttr == "") {
		
		oldString = selectionRange.htmlText;
		//alert(oldString)
		var newString = "";
   		var inTag = false;
   		for(var i = 0; i < oldString.length; i++) {
   
        	if(oldString.charAt(i) == '<') inTag = true;
        	if(oldString.charAt(i) == '>') {
              	inTag = false;
              	i++;
        	}
   
        	if(!inTag) newString += oldString.charAt(i);

   		}
		
		// Hack to remove non standard tags
		selectionRange.pasteHTML("<span id=tempId>" + newString + "</span>");
		objWYSIWYG.getIFrameDocument(pstrFrameId).getElementById("tempId").parentNode.removeNode(false)
		objWYSIWYG.getIFrameDocument(pstrFrameId).getElementById("tempId").removeNode(false)
		
	} else {
		//selectionRange.htmlText
		

		selectionRange.pasteHTML("<" + pstrTag + " class='" + pstrAttr + "'>" + selectionRange.text + "</" + pstrTag + ">");
		tempStr = objWYSIWYG.getIFrameDocument(pstrFrameId).body.innerHTML;
		var objRegExp = new RegExp('<SPAN class=highlightgreen>' + selectionRange.text + '</SPAN>', 'gi') ;
		tempStr = tempStr.replace(objRegExp, '');
		objWYSIWYG.getIFrameDocument(pstrFrameId).body.innerHTML = tempStr;
	}
}

objWYSIWYG.setBold = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("bold");
}

objWYSIWYG.setItalic = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("italic");
}

objWYSIWYG.setUnorderedList = function(pstrFrameId) {
	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("insertunorderedlist");
}

objWYSIWYG.setOrderedList = function(pstrFrameId) {
  	objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("insertorderedlist");
}

objWYSIWYG.setLinkExternal = function(pstrFrameId) {
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
	var strSel = objField.selection.createRange().text;
	if(strSel.length > 0){
    objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("CreateLink", true, false);
  }else{
    alert('Please select some text first.');
  }
}

objWYSIWYG.setEmail = function(pstrFrameId) {
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
	var strSel = objField.selection.createRange().text;
	if(strSel.length > 0){
    objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("CreateLink", true, false);
  }else{
    alert('Please select some text first.');
  }
}

objWYSIWYG.setLinkMail = function(pstrFrameId) {
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
	var strSel = objField.selection.createRange().text;
	if(strSel.length > 0){
    objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("CreateLink", true, false);
  }else{
    alert('Please select some text first.');
  }
}

objWYSIWYG.setLink = function(pstrFrameId) {
  //shows the link popup screen LinkSelectionPopup.asp
  //same as function markupTextAddLink() in non-wysiwyg CMS's
  // shows popup to selct a page via  Zone/Category/Page.  Put into textarea on _admin/PageEdit2.asp
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);

	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[pstrFrameId];
  
  objField.focus();
  
	var strSel = objField.selection.createRange().text;
	if(strSel.length > 0){
  	pDir = '';
  	//var strSel = document.selection.createRange().text;
		var strURL = window.showModalDialog(obj.imagePath + 'LinkSelectionPopup.asp?t=' + strSel,'dialogWidth:500px;dialogHeight:500px;help:no');
    if(strURL){
      Zone = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      Cat = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      Page = strURL.substring(0,strURL.indexOf('|'));
      strURL = strURL.substring(strURL.indexOf('|')+1);
      //strURL is left with href text
      objField.selection.createRange().pasteHTML('<code type="link" src="' + Zone + '|' + Cat + '|' + Page + '">' + strURL + '</code>');
    }
    //markTextAreaPosition1(objField);
    objField.focus();
  }else{
    alert('Please select some text first.');
  }
}

objWYSIWYG.toTop = function(pstrFrameId) {
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	selectionRange.pasteHTML("<a href=\"javascript:setTop()\">top</a>");
}

//UNUSED AT THE MOMENT
objWYSIWYG.tableDialog = function(pstrFrameId){
	objWYSIWYG.getIFrameDocument(pstrFrameId).caretPos = objWYSIWYG.getIFrameDocument(pstrFrameId).selection.createRange().duplicate();
	selectionRange = objWYSIWYG.getIFrameDocument(pstrFrameId).caretPos;
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[pstrFrameId];

	if (selectionRange != undefined) {
		showModalDialog(obj.imagePath + 'wEditorTable.asp',window,'status:true;dialogWidth:20em;dialogHeight:20em');
	}
}


objWYSIWYG.markupTextAddImageFile = function(pstrFrameId, pType, returnType, pDir){
	//alert(objField)
	var reg = objWYSIWYG.Registry;
	var obj = objWYSIWYG.Registry[pstrFrameId];
  var objField = objWYSIWYG.getIFrameDocument(pstrFrameId);
	var selectionRange = objField.selection.createRange().text;

  if(returnType=='text'){
    if(selectionRange != undefined){
  		var strURL = window.showModalDialog(obj.imagePath + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
      if(strURL){
	  
	    strFileName = strURL.substring(strURL.indexOf('|')+1);
		
		if (strFileName.toLowerCase().indexOf(".pdf") != -1){
			strIcon = "IconPDF.gif";
		} else if (strFileName.toLowerCase().indexOf(".doc") != -1) {
			strIcon = "IconWORD.gif";
		} else if (strFileName.toLowerCase().indexOf(".xls") != -1) {
			strIcon = "IconXLS.gif";
		} else {
			strIcon = "icon_html.gif";
		}
      objField.selection.createRange().pasteHTML('<CODE type="' + pType + '" id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strFileName + '" style="width:25px;height25px;background-image : url(' + obj.imagePath + '../images/' + strIcon + ');background-repeat : no-repeat;padding:4px 4px 4px 4px;" >' + selectionRange + '</CODE>');	  }
      //objField.focus();
    }else{
      alert('Please put the cursor in a textbox where you wish to put the item.');
    }
  }
  if(returnType=='sp'){
  	var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
  	var objLabel;
  	var objHiddenID;
  	var objHiddenName;
  	var newID, newName;
  	
    if(strURL){
    	objLabel = document.getElementById('lbl' + objField + 'NewName');
    	objHiddenID = document.getElementById(objField); 
    	objHiddenName = document.getElementById('txt' + objField + 'NewName');
  	  
  	  newID = strURL.substring(0,strURL.indexOf('|'));
  	  newName = strURL.substring(strURL.indexOf('|')+1);
  	  objLabel.innerText = newName;
  	  objHiddenID.value = newID;
  	  objHiddenName.value = newName;
  	}
  }

}

	// display Flash Movie
 	function displaySWF(movie, width, height, play, loop, quality, menu){
		var playO="", playE="", loopO="", loopE="", qualityO="", qualityE="", menuO="", menuE=""
		if ( play != null ){
			playO = '<PARAM NAME="PLAY" VALUE="'+play+'">\n';
			playE = 'PLAY="'+play+'" ';
		}
		if ( loop != null  ){
			loopO = '<PARAM NAME="LOOP" VALUE="'+loop+'">\n';
			loopE = 'LOOP="'+loop+'" '
		}
		if ( quality != null  ){
			qualityO = '<PARAM NAME="QUALITY" VALUE="'+quality+'">\n';
			qualityE = 'QUALITY="'+quality+'" ';
		}
		if ( menu != null  ){
			menuO = '<PARAM NAME="MENU" VALUE="'+menu+'">\n';
			menuE = 'MENU="'+menu+'" ';
		}else{
			menuO = '<PARAM NAME="MENU" VALUE="false">\n';
			menuE = 'MENU="false" ';
		}

		var oeTags = '<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
		+ 'WIDTH="'+width+'" HEIGHT="'+height+'" '
		+ 'CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0">\n'
		+ '<PARAM NAME="MOVIE" VALUE="'+movie+'">\n'
		+ playO
		+ loopO
		+ qualityO
		+ menuO
		+ '<EMBED SRC="'+movie+'" \n'
		+ 'WIDTH="'+width+'" HEIGHT="'+height+'" '
		+ playE
		+ loopE
		+ qualityE
		+ menuE
		+ 'TYPE="application/x-shockwave-flash" \n'
		+ 'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">\n'
		+ '<\/EMBED>\n'
		+ '<\/OBJECT>';
		//alert(oeTags);
		document.write(oeTags);   // embed the flash movie
	}