// Filename: incJSFuncs.js
// Copyright: 2002 CreativEngine Pty. Ltd.

// Browser Detect Lite  v2.1
// http://www.dithered.com/javascript/browser_detect/index.html
// modified by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)
//
// modified by Michael Lovitt to include OmniWeb and Dreamcast


//---------------------------------------------------------------


//---------------------------------------------------------------
// 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;
	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();
	}
}
//---------------------------------------------------------------


//---------------------------------------------------------------
// Opacity Displayer, Version 1.0
// Copyright Michael Lovitt, 6/2002.
// Distribute freely, but please leave this notice intact.
//---------------------------------------------------------------

//---------------------------------------------------------------
// OPACITY OBJECT
//
// Instantiates the object, defines the properties and methods.

//---------------------------------------------------------------

//



/*
****************************************************************
NEW WYSIWYG FUNCTIONS \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/  \/ 
****************************************************************
*/

var selectionRange;

objWYSIWYG.Registry = [];

function objWYSIWYG(pstrIFrameId, pstrTextAreaId, pstrPath, popstrPath){
	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\">"
	this.imagePath = pstrPath;
	if(popstrPath)
	  this.popUpPath = popstrPath;
	else
	  this.popUpPath = 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");
	}
}

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);
    objWYSIWYG.getIFrameDocument(pstrFrameId).execCommand("CreateLink", true, "mailto:" + strSel);
  }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);
  
  objField.focus();
  
	var strSel = objField.selection.createRange().text;
	var obj = objWYSIWYG.Registry[pstrFrameId];
	if(strSel.length > 0){
  	pDir = '';
  	//var strSel = document.selection.createRange().text;
		var strURL = window.showModalDialog(obj.popUpPath + '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.popUpPath + '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";
		}
		
		if (selectionRange == "") {
			selectionRange = strFileName;
		}
		
		if (pType == "file") {
			objField.selection.createRange().pasteHTML('<CODE type="' + pType + '" id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strFileName + '" style="background-image : url(' + obj.imagePath + '../images/' + strIcon + ');background-repeat : no-repeat;padding:4px 4px 4px 4px;" >' + selectionRange + '</CODE>');	 
	  	} else {
			objField.selection.createRange().pasteHTML('<CODE type="' + pType + '" id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strFileName + '" style="background-image : url(' + obj.imagePath + 'images/icon_IMG.gif);width: 15px;height: 15px;background-repeat : no-repeat;padding:4px 4px 4px 20px;" >&nbsp;</CODE><img src="' + obj.imagePath + '../_upload/images/' + strFileName + '" />')
		}
		
		
	   }
      //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(obj.popUpPath + '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;
  	}
  }

}

/*
****************************************************************
NEW WYSIWYG FUNCTIONS /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\
****************************************************************
*/


function markTextAreaPosition1(txtObj){
  if(txtObj.createTextRange)
    txtObj.caretPos = document.selection.createRange().duplicate();
}

//select and add a file/image
function markupTextAddImageFile(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'){
    objField.focus();
    if(objField.createTextRange && objField.caretPos){
  		var strURL = window.showModalDialog(pDir + 'ImageFileSelection.asp?noCache=' + (new Date()).valueOf() + '&type=' + pType,'','dialogWidth:500px;dialogHeight:500px;help:no');
      if(strURL){
        var caretPos = objField.caretPos;
        caretPos.text = caretPos.text + '<CMS' + pType + ' id="' + strURL.substring(0,strURL.indexOf('|')) + '" src="' + strURL.substring(strURL.indexOf('|')+1) + '"></CMS' + pType + '>';
      }
      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?noCache=' + (new Date()).valueOf() + '&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 roundAmount(n, bstrip) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + 
     s.substring(i + 1, i + 3)
  if (i + 2 == s.length) {
  	t += "0"
  }
  return t;
}

function stripChars(pNumberVal)
{
	var objRegExp = new RegExp(/[^0123456789.]/);
	sNew = pNumberVal.replace(objRegExp, '');
	if (sNew.length < pNumberVal.length) {
		stripChars(sNew);
	}
	return sNew;
}

function checkNumber(pValue)
{
	var strReturn;
	
	if (isNaN(pValue)) {
		alert("Must be a Numeric Value")
		strReturn = "";
	} else {
		strReturn = pValue;
	}
	
	return strReturn;

}

function ImageBuilder (imgSrc, imgLink){
	this.imgSrc = imgSrc;
	this.imgLink = imgLink;
}

// displays the random image on the home page
var theImages = new Array()

var CreativeYouthInit = "z=3&c=363&p=781"
var HowHelp = "z=2&c=370&p=775"
var CorpPartners = "z=2&c=247&p=777"
var TripleCare = "z=3&c=362&p=780"
var TripleCare = "z=3&c=362&p=780"

theImages[0] = new ImageBuilder('images/home_img_1.jpg', 'Page.asp?'+CorpPartners);
theImages[1] = new ImageBuilder('images/home_img_2.jpg', 'Page.asp?'+CreativeYouthInit);
theImages[2] = new ImageBuilder('images/home_img_3.jpg', 'Page.asp?'+HowHelp);
theImages[3] = new ImageBuilder('images/home_img_4.jpg', 'Page.asp?'+TripleCare);
theImages[4] = new ImageBuilder('images/home_img_5.jpg', 'Page.asp?'+TripleCare);

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i].imgSrc
}

var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<a href="'+theImages[whichImage].imgLink+'"><img src="'+theImages[whichImage].imgSrc+'" border="0" alt="Home image"></a>');
}

function validSearchForm(objForm, blnDoSubmit) {
	if(isEmpty(objForm.Search.value)) {
		alert("Enter a comma separated list of keyword(s) to search for.");
		objForm.Search.focus();
		return false;
	}

	if(blnDoSubmit)
		objForm.submit();
	return true;
}