//date
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

//3 dimensional button stuff

function button_over(eButton){
	eButton.style.borderBottom = "buttonshadow solid 1px";
	eButton.style.borderLeft = "buttonhighlight solid 1px";
	eButton.style.borderRight = "buttonshadow solid 1px";
	eButton.style.borderTop = "buttonhighlight solid 1px";
	}
function button_out(eButton){
	eButton.style.borderColor = "threedface";
	}
function button_down(eButton){
	eButton.style.borderBottom = "buttonhighlight solid 1px";
	eButton.style.borderLeft = "buttonshadow solid 1px";
	eButton.style.borderRight = "buttonhighlight solid 1px";
	eButton.style.borderTop = "buttonshadow solid 1px";
	}
function button_up(eButton){
	eButton.style.borderBottom = "buttonshadow solid 1px";
	eButton.style.borderLeft = "buttonhighlight solid 1px";
	eButton.style.borderRight = "buttonshadow solid 1px";
	eButton.style.borderTop = "buttonhighlight solid 1px";
	eButton = null; 
	}

//Rich text editor stuff
	
var isHTMLMode=false

function cleanup(myelement)
{
	//alert(myelement);
	myhtml = MWJ_findObj(myelement).innerHTML;
	MWJ_findObj('detag').value=myhtml;
	MWJ_findObj('requester').value = myelement;
	MWJ_findObj('eaterform').submit();
}

function document.onreadystatechange(){
  	document.designMode="On"
	}
	
function cmdExec(cmd,opt) {
  	if (isHTMLMode)
	{
		alert("Please uncheck 'Edit HTML'");
		return;
	}

	if (cmd == "paste") {
		var theOb = MWJ_findObj("icms");
		theOb.focus();
	}
	
  	document.execCommand(cmd,"",opt);
	document.focus();
	}

function inslink() {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	el = document.selection.createRange();
	//alert(el.HTML) 
	mylink = showModalDialog('dialogs/insertlink.htm','','dialogWidth:430px;dialogHeight:380px;unadorned:1;');
	if (mylink != null)
	{
	var linkarr = new Array();
	linkarr = mylink;
	//alert(mylink);
	thisurl = linkarr["LinkUrl"]
  	thislabel = linkarr["LinkLabel"]
	//alert(lin);
	if ((el.text.length > 0) && (thisurl.length > 0))
	{
	el.pasteHTML('<a href="' + thisurl + '">' + el.text + '</a>');
	document.focus();
	}
	else
	{
		alert('You have not selected any text to make a hyperlink');
	}
	}
	}
	
function insimage(target,arguments) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	if (!arguments["ImgUrl"])
		{
		t = MWJ_findObj(target);
		
		//bl = t.createControlRange();
		
		if (t.document.selection.type != "None")
		{
		el = t.document.selection.createRange();
		
		
		myimg = showModalDialog('dialogs/insertimage.htm',arguments,'dialogWidth:460px;dialogHeight:430px;unadorned:1;');
		if (myimg != null)
		{
		var imgarr = new Array();
		imgarr = myimg;
		thisurl = imgarr["ImgUrl"]
		thisalign = imgarr["ImgAlign"]
		thisborder = imgarr["ImgBorder"]
		thishspace = imgarr["HorSpace"]
		thisvspace = imgarr["VerSpace"]
		thisalt = imgarr["AltText"]
		//window.status = ' Image Insterted : ' +thisurl;
		//thislabel = linkarr["LinkLabel"]
		//alert(lin);
		//var image1=new Image()
		//image1.src=thisurl;
		//el.add(image1);
		el.pasteHTML('<img src="' + thisurl + '" border=' + thisborder + ' align=' + thisalign + '>');
		}
		}
		else
		{
			alert('Please select an area to insert image (at least a blank character space)');
		}
	}
	else
	{
		window.status = 'Editing Image : ' + arguments["ImgUrl"];
		el = document.selection.createRange().item(0);
		//alert(el.HTML) 
		myimg = showModalDialog('dialogs/insertimage.htm',arguments,'dialogWidth:460px;dialogHeight:430px;unadorned:1;');
		if (myimg != null)
		{
		var imgarr = new Array();
		imgarr = myimg;
		el.src = imgarr["ImgUrl"];
		el.align = imgarr["ImgAlign"]
		el.border = imgarr["ImgBorder"]
		el.hspace = imgarr["HorSpace"]
		el.vspace = imgarr["VerSpace"]
		el.alt = imgarr["AltText"]
		window.status = 'Image : ' + arguments["ImgUrl"] + ' updated';
		}
	}
	//document.focus();
}

function insasset() {
		myasset= showModalDialog('dialogs/insertasset.htm','','dialogWidth:460px;dialogHeight:300px;unadorned:1;');
		if (myasset != null)
		{
		var assetarr = new Array();
		assetarr = myasset;
		thisurl = myasset["ImgUrl"]
		assetfile.value = thisurl
		ispdf = thisurl.indexOf(".pdf");
		isxls = thisurl.indexOf(".xls");
		isdoc = thisurl.indexOf(".doc"); 
		isimg1 = thisurl.indexOf(".jpg");
		isimg2 = thisurl.indexOf(".gif"); 
		isppt = thisurl.indexOf(".ppt");
		if (ispdf != -1) { thistype = 1}
		if (isxls != -1) { thistype = 2}
		if (isdoc != -1) { thistype = 3}
		if (isppt != -1) { thistype = 4}
		if (isimg1 != -1) { thistype = 5}
		if (isimg2 != -1) { thistype = 5}
		atype.selectedIndex = thistype -1;
		//is = thisurl.indexOf(".pdf"); 
		//window.status = ' Image Insterted : ' +thisurl;
		//thislabel = linkarr["LinkLabel"]
		//alert(lin);
		}
}


function insdiv(arguments) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	if (!arguments["ID"])
	{
	el = document.selection.createRange();
	//alert(el.HTML) 
	mydiv = showModalDialog('dialogs/insertdiv.htm','','dialogWidth:430px;dialogHeight:290px;unadorned:1;');
	if (mydiv != null)
	{
	var divarr = new Array();
	divarr = mydiv;
	thisid = divarr["ID"]
	thiswidth = divarr["Width"]
	thisheight = divarr["Height"]
	thistop = divarr["Top"]
	thisleft = divarr["Left"]
	thisz = divarr["Z-Index"]
	thistype = divarr["Postype"]
	window.status = ' DIV Insterted : ' +thisid;
  	//thislabel = linkarr["LinkLabel"]
	//alert(lin);
	el.pasteHTML('<div id="' + thisid + '" style="position:' + thistype + '; top:' + thistop + '; left:' + thisleft + '; width:' + thiswidth + '; height:' + thisheight + '; z-index:' + thisz + '">DIV Inserted Here</div');
	}
	else
	{
	alert('Coming Soon...');
	}
	document.focus();
	}
}
	
function changestyle(cmd,opt) {
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	el = document.selection.createRange();
	//alert(el.HTML) 
	el.pasteHTML('<SPAN class='+ opt +'>'+ el.text + '</SPAN>');
	document.focus();
	}

function getprop() {
	var arr, args;
  	arr=null;
  	args=new Array();
  	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	el = document.selection.createRange();
	el2 = document.selection.type;
	el3 = document.selection.type.style;
	//alert(el3)
	
	if(el2 == "Control"){
     var oImg = document.selection.createRange().item(0);
	 if (oImg.tagName == "IMG")
	 {

     args["ImgUrl"] = oImg.src;
     args["AltText"] = oImg.alt;
     args["ImgBorder"] = oImg.border;
     args["HorSpace"] = oImg.hspace;
     args["VerSpace"] = oImg.vspace;
     args["ImgAlign"] = oImg.align;
	 insimage(args)
	 }
	 if (oImg.tagName == "TABLE")
	 {
     args["ImgUrl"] = oImg.src;
     args["AltText"] = oImg.alt;
     args["ImgBorder"] = oImg.border;
     args["HorSpace"] = oImg.hspace;
     args["VerSpace"] = oImg.vspace;
     args["ImgAlign"] = oImg.align;
	 insimage(args)
	 }
	 if (oImg.tagName == "DIV")
	 {
	 alert('Boom')
     args["ID"] = oImg.id;
     args["Width"] = oImg.width;
     args["Height"] = oImg.height;
     args["Top"] = oImg.top;
     //args["Z-Index"] = oImg.z-index;
     args["Left"] = oImg.left;
	 insdiv(args)
	 }
	 window.status = "Selected HTML Object Type = " + oImg.tagName;
  	}
	}
	
function setMode(ditem, bMode){
	var sTmp;
  	isHTMLMode = bMode;
  	if (isHTMLMode){sTmp=eval(ditem).innerHTML;eval(ditem).innerText=sTmp;} 
	else {sTmp=eval(ditem).innerText;eval(ditem).innerHTML=sTmp;}
  	//document.focus();
	}
	
//Form stuff
function fninit()
	{
	for(i=0;i<document.all.length;i++) document.all(i).unselectable = "on";
	document.unselectable = "off";
	}

function deletedata(table,did)
	{
	//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	MWJ_findObj('fid').value=did;
	MWJ_findObj('ftable').value=table;
	MWJ_findObj('faction').value="deln";	
	MWJ_findObj('secret').submit();
	}

function Save(tname,did,ename,edata,ename2,edata2) 
	{
	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
	thiscopy = document.body.innerHTML;
	form1.html.value = document.body.innerHTML;
	form1.saveme.value = 1
	form1.table.value = tname
	form1.id.value = did
	form1.field_a_name.value = ename
	form1.field_a_data.value = edata
	form1.field_b_name.value = ename2
	form1.field_b_data.value = edata2

	//window.open(url,'savedata','width=200,height=60')
	document.form1.action = "savedata.asp";
	//document.form_1.save.value = 1;
	document.form1.submit();
	}

function savenews(did,data) 
	{
	window.open('savedatanews.asp?id=' + did + '&data='+ data);
	}

function savedata(table,did,dcopy,dtitle,ddate)
	{
	//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
	if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}

	if ((ddate=='null') || (isDate(ddate)==true)){
		MWJ_findObj('fid').value=did;
		MWJ_findObj('ftable').value=table;
		MWJ_findObj('ftitle').value=dtitle;	
		MWJ_findObj('fcopy').value=dcopy;
		MWJ_findObj('fdate').value=ddate;
		MWJ_findObj('secret').submit();
	}
	
	}
function savendata(table,dtitle,dcopy,ddate)
	{
	//window.open('savedata.asp?table=' + table + '&id=' + did + '&title=' + dtitle + '&copy=' + dcopy);
	//MWJ_findObj('fid').value=did;
	error = "";

	if (dtitle == "Add New News Title Here...") {error += "No title given for the news article!\n";}
	if (dcopy == "Add new news copy here....") {error += "No news copy entered!\n";}

	if (error == "") {
		if (isDate(ddate)==true) {
			MWJ_findObj('ntable').value=table;
			MWJ_findObj('ntitle').value=dtitle;
			MWJ_findObj('ncopy').value=dcopy;
			MWJ_findObj('ndate').value=ddate;
			MWJ_findObj('nsecret').submit();
		}
	}else{
		alert (error);
	}
	
	}
	
function saverowdata(did)
	{
	///MWJ_findObj('fid').value=did;
	//MWJ_findObj('ftable').value=table;
	MWJ_findObj('special').value=did;
}

function addnewasset(aid,ftype,title,file)
{
	url = 'saveasset.asp?addme=1&aid=' + aid + '&ftype=' + ftype + '&title=' + title + '&file=' + file
	window.open(url,'savedata','width=200,height=60')
}

function deleteasset(aid)
{
	url = 'saveasset.asp?deleteme=1&aid=' + aid
	window.open(url,'savedata','width=200,height=60')
}
	
function SearchAndReplace(Content, SearchFor, ReplaceWith) {
   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;
   if (SearchFor.length == 0)
      return;
   while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {
      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;
      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;
      // Get the rest of the content after the match until
      // the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);
   }
   return tmpOutput + tmpContent;
}

function findStyleRules() {
  //alert(document.styleSheets.length)
  a = 0;
  for (i = 0; i < document.styleSheets.length; i++) { 
    for (j = 0; j < document.styleSheets(i).rules.length; j++) {
	  myselector = 	document.styleSheets(i).rules(j).selectorText;
	  if ((myselector.indexOf(".")!=-1) && (myselector.indexOf(":")==-1))
	  {
	  	a = a + 1
	  	//alert(myselector)
		myselector = myselector.substring(1,myselector.length);
		document.form_1.select1.options[a] = new Option(myselector,myselector)
	  }
      //alert();
    }     
  }
}

function ShowTableBorders()
{
	document.SHOWBORDER = !document.SHOWBORDER;
	tableBorders();
}

function tableBorders()
{
	var aTables = document.getElementsByTagName("TABLE");
	for (i=0;i<aTables.length;i++)
	{
		if(document.SHOWBORDER && (aTables[i].border == 0))
		{            
			aTables[i].runtimeStyle.borderWidth = 1;
			aTables[i].runtimeStyle.borderColor = "#BBBBBB";
			aTables[i].runtimeStyle.borderStyle = "dotted";
			aTables[i].runtimeStyle.borderCollapse = "collapse";
			window.status = "Table Borders ON"
		}
		if(!document.SHOWBORDER && (aTables[i].border == 0))
		{            
			aTables[i].runtimeStyle.cssText = ''; 
			window.status = "Table Borders OFF"
		}
	}
}
function getstyles()
{
	document.form_1.select1.options.length = 0;
	setTimeout("findStyleRules()",1000);
	document.form_1.select1.options[0] = new Option("Style...","")
}

