// *********************************************************************************
// MERLIN SOFTWARE ENGINEERING INC. COPYRIGHT 2005, ALL RIGHTS RESERVED.
// *********************************************************************************
// Modification of this page by anyone other than MerlinSE.com voids all warranties
// made to the client, whether or not the client is aware of these changes.
// *********************************************************************************
// File:	library.js
// Purpose:	JavaScript function library
// Author:	Merlin
// Date:	November 2000, version 1.0
// *********************************************************************************

// ---------------------------- web site specific code --------------------------------- 

function homepage(dns) {
	if(dns!="") top.location.href = dns;
	else openpage("http://www.ritasdiary.com","window");
}
function RITAintro_DoFSCommand() {}
function RITAintro_swf() {
	document.write("<embed name='RITAintro' src='common/intro.swf' quality='best' wmode='transparent' width='100%' height='100%' type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}

function RITAmenu_DoFSCommand() {}
function RITAmenu_swf(dns,x) {
	if(!dns || dns=="") var dns = "/";
	document.write("<embed name='RITAmenu' src='" + dns + "img/rita_menus3" + x + ".swf' quality='best' wmode='transparent' width=135 height=238 type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}
function RITAhome_DoFSCommand() {}
function RITAhome_swf(dns,x) {
	if(!dns || dns=="") var dns = "/";
	document.write("<embed name='RITAhome' src='" + dns + "img/rita_home3.swf' quality='best' wmode='transparent' width=478 height=496 type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}
function RITAmenu2a_DoFSCommand() {}
function RITAmenu2a_swf(dns,x) {
	if(!dns || dns=="") var dns = "/";
	document.write("<embed name='RITAmenu2a' src='" + dns + "img/rita_menus2a" + x + ".swf' width=441 height=30 type='application/x-shockwave-flash' quality='best' wmode='transparent' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}
function RITAmenu2b_DoFSCommand() {}
function RITAmenu2b_swf(dns) {
	if(!dns || dns=="") var dns = "/";
	document.write("<embed name='RITAmenu2b' src='" + dns + "img/rita_menus2b.swf' width=90 height=30 type='application/x-shockwave-flash' quality='best' wmode='transparent' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}
function RITAmenu3a_DoFSCommand() {}
function RITAmenu3a_swf(dns) {
	if(!dns || dns=="") var dns = "/";
	document.write("<embed name='RITAmenu3a' src='" + dns + "img/rita_menus3a.swf' width=451 height=30 type='application/x-shockwave-flash' quality='best' wmode='transparent' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}
function RITAmenu3b_DoFSCommand() {}
function RITAmenu3b_swf(dns) {
	if(!dns || dns=="") var dns = "/";
	document.write("<embed name='RITAmenu3b' src='" + dns + "img/rita_menus3b.swf' width=208 height=30 type='application/x-shockwave-flash' quality='best' wmode='transparent' pluginspage='http://www.adobe.com/go/getflashplayer'></embed>");
}

// ---------------------------------- standard functions ------------------------------- 
var newWin; var newWiz;
function openpage(pn,frm,w,h,x,y) {
	if(pn == undefined || pn == "") return;
	if(frm == undefined || frm == "") frm="parent";
	if(x == undefined) x=20;
	if(y == undefined) y=20;
	if(frm == "wizard") {
		if(w == undefined) w=350;
		if(h == undefined) h=475;
	} else if(frm == "window") {
		if(w == undefined) w=700;
		if(h == undefined) h=450;
	}
	if(x == -1) x = Math.floor((screen.width/2) - (w/2));
	if(y == -1) y = Math.floor((screen.height/2) - (h/2));
	if(frm == "wizard") {
		if(top.newWiz && !top.newWiz.closed) top.newWiz.close();
		newWiz = window.open(pn, "newWiz", "scrollbars=yes,top=" + x + ",left=" + y + ",width=" + w + ",height=" + h);
	} else if(frm == "window") {
		if(top.newWin && !top.newWin.closed) top.newWin.close();
		newWin = window.open(pn, "newWin", "menubar=yes,toolbar=yes,status=yes,scrollbars=yes,resizable=yes,top=" + x + ",left=" + y + ",width=" + w + ",height=" + h);
	} else {
		var s = "self.";
		if(frm == "top") s = "self.top.";
		else if(frm == "parent") s = "self.parent.";
		else if(frm == "opener") s = "self.window.opener.";
		else if(frm != "self") s = "top." + frm + ".window.";
		if(window.location.replace == undefined) eval(s + "location.href=pn;");
		else eval(s + "location.replace(pn);");
	}
}
function nothing() {}
function getHtmlObj(s) {
	var obj = undefined;
	if(document.all) obj = document.all[s];
	if(obj == undefined && document.getElementById) obj = document.getElementById(s);
	if(obj == undefined) eval("obj = window." + s + ";");
	return obj;
}
function enterKey(e,a) {
	if(!a || a == "") return;
	if(!e) var e = event;
	if ((e && e.which == 13) || (window.event && window.event.keyCode == 13)) eval(a);
}
function validHex(s) {
	var RE = /[^0-9a-fA-F]/;
	s = ""+s;
	if(s.length != 6) return false;
	return !RE.test(s);
}
function validField2(xdata,xname,xtype,minSize,lowVal,hiVal,focus) {
	return validField(0,xname,xtype,minSize,lowVal,hiVal,1,xdata);
}
function validField(field,xname,xtype,minSize,lowVal,hiVal,focus,xdata) {
	if(xdata == undefined) var xdata = field.value;
	if(xdata == undefined) return false;
	if(minSize == undefined) var minSize = 0;
	if(focus == undefined) var focus = 0;
	
	if(xtype.substring(0,3) == "url") xdata = removeHTTP(xdata);
	xdata = myTrim(xdata);
	
	var mess = "Sorry, the " + xname + " field does not appear to be valid";
	var check = true;

	if(xdata.length < minSize) check = false;
	else if(xdata.length > 0 && xtype != "text") {

		if(xtype == "email") {
			var a = xdata.split("@");
			if( xdata.indexOf(" ") != -1 || xdata.indexOf(",") != -1 ) check = false;
			else if(a.length != 2 || a[0].length < 2) check = false;
			else {
				var a = a[1].split("."); var b = a.length;
				if(b == 1 || a[b-2].length < 2) check = false;
				else if(a[b-1].length < 2) check = false;
			}
		}
		else if(xtype.substring(0,3) == "url") {
			var a = xdata.split(".");
			var end = a[a.length-1]; end = end.toLowerCase();
			var a = xdata.split("/"); var xdata = a[0];
			if(xtype == "urlb" && a.length == 1) check = false;
			else {
				var a = xdata.split("."); var b = a.length;
				if(b == 1 || a[b-2].length < 2) check = false;
				else if(a[b-1].length < 2) check = false;
				else {
					var a = a[0].split("@");
					if(a.length > 1) check = false;
					else if(xtype == "urlb" && end != "gif" && end != "jpg" && end != "bmp" ) check = false;
				}
			}
		}
		else if(xtype == "tele") {
			xdata = replaceChar(xdata,"\[^0-9\]","");
			if(xdata.length < 10 || allSame(xdata) == true) {
				mess = "Please enter a valid Telephone Number as: (area code) ###-####"; check = false;
			} else {
				var RE = /(\S*)(\d{3})(\d{4})/;
				var a = xdata.match(RE); xdata = a[2] + "-" + a[3];
				if(a[1] != "") xdata = "(" + a[1] + ") " + xdata;
			}
			if(focus == 0) field.value = xdata;
		}
		else if(xtype == "zip") {
			xdata = replaceChar(xdata,"\[^0-9a-z\]","");
			xdata = xdata.toUpperCase();
			if(xdata.length < 5 || allSame(xdata) == true) check = false;
			else if(replaceChar(xdata.substring(0,1),"\[^a-z\]","") != "") {
				var x = replaceChar(xdata,"\[^a-z\]","");
				if(x.length < 3) check = false;
				else {
					var RE = /(\S*)(\S{3})/;
					var a = xdata.match(RE); xdata = a[1] + " " + a[2];
				}
			}
			else if(replaceChar(xdata,"\[^a-z\]","") != "") check = false;
			else if(xdata.length == 9)
				xdata = xdata.substring(0,5) + "-" + xdata.substring(5,xdata.length+1);
			if(focus == 0) field.value = xdata;
		}
		else if(xtype == "number") {
			xdata = replaceChar(xdata,"\[^0-9\\.\\,\\-\]","");
			var x = properNum(xdata);
			if((lowVal != undefined && x < properNum(lowVal)) || (hiVal != undefined && x > properNum(hiVal))) {
				mess = "Please enter a Number "
				if(lowVal != undefined && hiVal != undefined) mess += "between " + lowVal + " and " + hiVal; 
				else if(lowVal != undefined) mess += "greater than (or equal to) " + lowVal; 
				else if(hiVal != undefined) mess += "smaller than (or equal to) " + hiVal; 
				mess += " in the " + xname + " field"; check = false;
			}
			else if(focus == 0) field.value = xdata;
		}
		else if(xtype == "pw") {
			xdata = replaceChar(xdata,"\[^0-9a-z\]","");
			if(xdata.length < 6) check = false;
			if(focus == 0) field.value = xdata;
			if(check == false) mess = "Sorry, the Password field can only contain alphanumeric characters and it must be at least 6 characters in length.";
		}
		else if(xtype == "date") {
			check = checkDate(xdata);
		}
		else if(xtype == "time") {
			check = checkTime(xdata);
		}
		else if(xtype == "alpha") {
			var RE = /^[a-z \.]/i;
			if( RE.test(xdata) ) check = false;
			if(check == false) mess = "Please only enter ALPHA characters (no digits) in the " + xname + " field";
		}
	}

	if(check == false) { alert(mess); if(focus == 0) field.focus();	}
	return check;	
}
function checkDate(s) {
	s = myTrim(s); var x = s.split("/"); var check = true;
	if(x.length < 3) check = false;
	else {
		var mo = properNum(x[0]); var dy = properNum(x[1]); var yr = properNum(x[2]);
		if(mo < 1 || mo > 12 || dy < 1 || dy > 31 || yr < 2000 || yr > 3000) check = false;
		else if((mo == 4 || mo == 6 || mo == 9 || mo == 11) && dy > 30) check = false;
		else if(mo == 2 && dy > 29) check = false;
	}
	return check;
}
function checkTime(s) {
	s = myTrim(s); var x = s.split(" "); var y = x[0].split(":"); var check = true;
	if(y.length < 2 || y.length > 3) check = false;
	else {
		var hr = properNum(y[0]); var mn = properNum(y[1]); var sc = 0;
		if(y.length == 3) sc = properNum(y[2]);
		if(hr < 0 || mn < 0 || sc < 0 || mn > 59 || sc > 59) check = false;
		else {
			if(x.length > 1) {
				x = x[1].toLowerCase();
				if(hr > 24 && (x == "hrs" || x == "hours")) check = false;
				else if(hr > 12 || (x != "am" && x != "pm")) check = false;
			}
			else if(hr > 24) check = false;
		}
	}
	return check;
}
function date2str(ddate,mode) {
	if(!mode) var mode = "";
	var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	var weeks  = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	var dy = ddate.getDate();
	var mo = ddate.getMonth()+1;
	var yr = ddate.getFullYear();
	if(!dy || !mo || !yr) return "";
	var s = ""; yr = ""+ yr;									// --- format date stamp
	if(mode == "") s = padStr(mo,"0",2) + "/" + padStr(dy,"0",2) + "/" + yr;
	else if(mode == "abb")  s = padStr(mo,"0",2) + "/" + padStr(dy,"0",2) + "/" + yr.substring(2,4);
	else if(mode != "time"){
		var mo = months[mo-1];
		if(mode == "short" || mode == "full") mo = mo.substring(0,3);
		else if(mode == "long") {
			mo = weeks[ddate.getDay()] + " " + mo;
			var end = "th";
			if (dy == 1 || dy == 21 || dy == 31) end = "st";
			else if (dy == 2 || dy == 22) end = "nd";
			else if (dy == 3 || dy == 23) end = "rd";
			dy += end;
		}
		s = mo + " " + dy + ", " + yr;
	}
	if(mode == "full" || mode == "time") {
		if(mode == "full") s += " - ";
		s += padStr(ddate.getHours(),"0",2) + ":" + padStr(ddate.getMinutes(),"0",2) + ":" + padStr(ddate.getSeconds(),"0",2);
	}
	return s;
}
function maxChars(field,xname,max) {
	if(!field) return false;
	if(!max) var max=255;
	var len = field.value.length; var agree=true;
	if(len > max) {
		field.focus();
		agree = confirm("Sorry, the " + xname + " field can only hold " + max + " characters (" + len + " have been entered).\n\nPress OK to have it truncated, or CANCEL to edit it yourself.");
	}
	return agree;
}
function allSame(s) {
	var i=0; var xchar = ""; var flag = true;
	for(i=0; i < s.length; i++) {
		if(xchar == "") xchar = s.substring(i,i+1);
		if(xchar != s.substring(i,i+1)) flag = false;
	}
	return flag;
}
function blankZero(s) {
	if(!s || properNum(s)==0) return "";
	return s;
}
function properNum(num,type) {
	if(!type) var type = 0;
	var RE1 = /[ \,\$]/g; var RE2 = /[^0-9\.\-\+]/i;
	var x = 0; num = "" + num;
	num = num.replace(RE1,"");
	if(num != "" && RE2.test(num) == false) {
		x = parseFloat(num);
		if(type == 1) x = parseInt(x);
	}
	return x;
}
function replaceChar(s,o,n,c) {			
	if(!s || s == "") return "";
	if(!c) var c = "i"; else c = "";
	eval("var RE = /" + o + "/g" + c + ";");
	return s.replace(RE,n);
}
function encodeChars(s,i) {
	if(!s || s == "") return "";
	if(!i) var i=0;
	s = myTrim(s);
	if(i==0) {
		s = replaceChar(s,"\\?","[question]");
		s = replaceChar(s,"&","[ampersand]");
		s = replaceChar(s,"=","[equals]");
		s = replaceChar(s,"~","[tilde]");
		s = replaceChar(s,"%","[percent]");
	} else {
		s = replaceChar(s,"\\?","");
		s = replaceChar(s,"&","");
		s = replaceChar(s,"=","");
		s = replaceChar(s,"~","");
		s = replaceChar(s,"%","");
		s = replaceChar(s,",","");
	}
	return s;
}
function myTrim(s,side) {
	if(!s) return "";
	if(!side) var side = "both";
	var RE1 = /\S/;
	s = ""+ s; var i;
	if(side == "left" || side == "both" || side == "all") {
		var i = s.search(RE1);
		if(i == -1) return "";
		else s = s.substring(i,s.length);
	}
	if(side == "right" || side == "both" || side == "all") {
		for(i=s.length-1; i > -1; i--) {
			var xchar = s.substring(i,i+1);
			if(RE1.test(xchar)) { s = s.substring(0,i+1); break; }
		}
	}
	if(side == "all") {
		var s2 = ""; var ok = true;
		for(i=0; i < s.length; i++) {
			var xchar = s.substring(i,i+1);
			if(RE1.test(xchar)) { s2 += xchar; ok = true; }
			else if(ok) {s2 += " "; ok = false; }
		}
		s = s2;
	}
	return s;
}
function padStr(s,xchar,iLen,side) {
	if(!s) var s = "";
	if(!side) var side = "left";
	s = ""+ s;
	if(s.length >= iLen) return s;
	var end = iLen-s.length;
	for(var i=0; i < end; i++) {
		if(side == "left") s = xchar + s;
		else s += xchar;
	}
	return s;
}
function removeHTTP(s) {
	if(!s || s == "") return "";
	s = replaceChar(s,"\\\\","/");
	s = replaceChar(s,"[\"']","");
	s = replaceChar(s,"https*:*\\/*","");
	return myTrim(s);
}
function removeHTML(s,crlf) {
	if(!s || s == "") return "";
	if(!crlf) var crlf="";
	var x="`"; s = replaceChar(s,x,"'");
	s = replaceChar(s,"$",x);
	s = replaceChar(s,"<br>",x);
	s = replaceChar(s,"<\\/tr>",(x + x));
	s = replaceChar(s,"<hr[^>]*>",(x + "-------------------------------------------" + x));
	s = replaceChar(s,"<.*?>","");
	s = replaceChar(s,x,crlf);
	s = replaceChar(s,"&gt;",">");
	s = replaceChar(s,"&lt;","<");
	s = replaceChar(s,"&nbsp;"," ");
	s = replaceChar(s,"&amp;","&");
	return myTrim(s);
}
