/**************************************************************

Newsletter Development and Publishing System 1

Script          : inc/functions.js
Developer       : Ron Hook
Company         : Studio 55
web address     : http://www.studio55.com.au
email address   : studio@studio55.com.au

Last modified   : 22 March 2006
Modified By     : Ron Hook

**************************************************************/

	/* **********************************************************
	*
	*	IE_sux
	*
	************************************************************/

function IE_sux(){
	if(navigator.appName == "Microsoft Internet Explorer"){
		is_IE = true;
	}else{
		is_IE = false;
	}
	return is_IE;
}

var SelectedItem;
var SelectedX, SelectedY;
var MouseX, MouseY;
var z_index = 1000;

function Move_IE(ClickedItem){
	SelectedItem = ClickedItem.parentElement;
	SelectedX = SelectedItem.style.pixelLeft;
	SelectedY = SelectedItem.style.pixelTop;
	MouseX = event.clientX;
	MouseY = event.clientY;
	SelectedItem.style.zIndex = z_index++;
	document.onmousemove = Drag_IE;
	document.onmouseup = Drop;
}
function Move_MOZ(ClickedItem, event){
	SelectedItem = ClickedItem.parentNode;
	MouseX = event.layerX;
	MouseY = event.layerY;
	SelectedItem.style.zIndex = z_index++;
	//alert(SelectedItem.style.zIndex);
	document.onmousemove = Drag_MOZ;
	document.onmouseup = Drop;
}
function Drag_IE(){
	SelectedItem.style.left = SelectedX + (event.clientX - MouseX);
	SelectedItem.style.top = SelectedY + (event.clientY - MouseY);
	return false;
}
function Drag_MOZ(event){
	//alert("moving");
	SelectedItem.style.left = (event.clientX - MouseX);
	SelectedItem.style.top = (event.clientY - MouseY);
}

function Drop()
{
  document.onmousemove = null;
  document.onmouseup = null;
}

function set_row_display(row, val){
	for(var i=0; i<row.length; i++){
		if(val == true){
			//document.getElementById("V").style.display = "block";
			if(IE_sux()){
				document.getElementById(row[i]).style.display = "block";
			}else{
				document.getElementById(row[i]).style.display = "table-row";
			}
		}else{
			document.getElementById(row[i]).style.display = "none";
		}
	}
	//document.getElementById(postal_area[0]).focus();
}

	/***************************************************************
	*
	*	display_date
	*
	***************************************************************/

function display_date(check_date){
	mysql_date = check_date.split(" ");

	timeString = "";
	if(mysql_date[1]){
		time_array = mysql_date[1].split(":");
		if(Number(time_array[0]) > 12){
			timeString += (Number(time_array[0]) - 12);
		}else{
			timeString += time_array[0];
		}
		timeString += ":" + time_array[1];
		if(Number(time_array[0]) > 11){
			timeString += "pm";
		}else{
			timeString += "am";
		}
		timeString += " on the ";
	}

	date_array = mysql_date[0].split("-");
	this_day = Number(date_array[2]);
	this_month = Number(date_array[1]);

	return timeString + ordinal(this_day) + " of " + months[this_month] + " " + date_array[0];
}

	/***************************************************************
	*
	*	ordinal
	*
	***************************************************************/

function ordinal (num){
	if(num == 11 || num == 12 || num == 13){
		return num + "th";
	}else{
		check_num = num.substr(num.length-1, 1);
		if($check_num == "1"){
			return num + "st";
		}else if(check_num == "2"){
			return num + "nd";
		}else if(check_num == "3"){
			return num + "rd";
		}else{
			return num + "th";
		}
	}
}

	/* **********************************************************
	*
	*	createXMLHttpRequest
	*
	************************************************************/

function createXMLHttpRequest() {
	IE_sux();
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(is_IE){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

	/* **********************************************************
	*
	*	set_check_box
	*
	************************************************************/

function set_check_box(tgt){
	document.getElementById(tgt).click();
}

	/* **********************************************************
	*
	*	load_data
	*
	************************************************************/

function load_data(page, vars){
	//alert_test(vars);
	//document.getElementById(tgt).innerHTML = "<div style=\"padding: 50px 10px; text-align: center;\">Loading data</div>";
	var xhReq = createXMLHttpRequest();

	xhReq.open("POST", page, true);
	xhReq.onreadystatechange = function(){
		current_state = xhReq.readyState;
		if (current_state != 4)  { return; }
		//alert(xhReq.responseText);
		try{
		eval(xhReq.responseText);
		}
		catch(e){
			if(test_mode == true){
				alert("LOAD DATA EROR\n" + e.message + "\n" + e.lineNumber + "\n\n" + xhReq.responseText);
				//alert(xhReq.responseText);
			}
		}
	}
	xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhReq.send(vars);
}

	/* **********************************************************
	*
	*	emailCheck
	*
	************************************************************/

function emailCheck (emailStr) {
	//alert(emailStr);

	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		error_message = "(check @ and .'s)";
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {
		error_message = "The username doesn't seem to be valid.";
		return false;
	}

	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				error_message = "Destination IP address is invalid!";
				return false;
			}
		}
		return true;
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		error_message = "The domain name doesn't seem to be valid.";
		return false
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 ||
		domArr[domArr.length-1].length>3) {
	   error_message = "The address must end in a three-letter domain, or two letter country.";
	   return false;
	}

	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   error_message = errStr;
	   return false;
	}

	return true;
}

	/* **********************************************************
	*
	*	get_object_value
	*
	*********************************************************** */

function get_object_value(tgt){
	if(document.getElementById(tgt)){
		return document.getElementById(tgt).value;
	}else{
		return "";
	}
}

	/* **********************************************************
	*
	*	trimWhiteSpace
	*
	*********************************************************** */

function trimWhiteSpace(trimString){
	var newString = "";

	for(var i=0; i<trimString.length; i++){
		if(trimString.substr(i, 1)!= " "){
			newString = trimString.substring(i, trimString.length);
			i = trimString.length;
		}
	}

	var newString_test = newString;

	for(i=newString.length; i>0; i--){
		if(newString.substr(i-1, 1)!= " "){
			newString = newString.substring(0, i);
			i = 0;
		}
	}
	//alert("trimString = a" + trimString + "e, newString = b" + newString + "f");
	return newString;
}

	/* **********************************************************
	*
	*	swap_picture
	*
	*********************************************************** */

function swap_picture(tgt_pic, new_pic){
	document.getElementById(tgt_pic).src = new_pic.src;
}

	/* **********************************************************
	*
	*	swap_view
	*
	*********************************************************** */

open_area = "";

function swap_view(tgt_area){
	if(document.getElementById(open_area)){
		document.getElementById(open_area).style.display = 'none';
	}
	if(document.getElementById(tgt_area)){
		if(document.getElementById(tgt_area).style.display == 'none' ){
			document.getElementById(tgt_area).style.display = 'block';
		}
		open_area = tgt_area;
	}
}

	/* **********************************************************
	*
	*	view_area
	*
	*********************************************************** */

function view_area(tgt_area){
	if(document.getElementById(tgt_area).style.display == 'none' ){
		document.getElementById(tgt_area).style.display = 'block';
	}else{
		document.getElementById(tgt_area).style.display = 'none';
	}
}

	/* **********************************************************
	*
	*	switch_view
	*
	*********************************************************** */

function switch_view(view, hide){
	document.getElementById(view).style.display = 'block';
	document.getElementById(hide).style.display = 'none';
}

	/* **********************************************************
	*
	*	write_calander
	*
	*********************************************************** */

months = Array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function write_calander(src_date, tgt_display, ref_tgt, required, is_search){

	//alert(src_date + ", " + required);

	if(required == "DATE"){
		var target_date = document.getElementById(ref_tgt).value.split("-");
		var show_date = src_date.split("-");
		var date_value = "";
		var time_value = "";
	}else if(required == "TIME"){
		var show_time = src_date.split(":");
		var date_value = "";
		var time_value = "";
	}else if(required == "DATETIME"){
		ref_date = src_date.split(" ");
		var target_date = document.getElementById(ref_tgt).value.split(" ");
		var target_date = target_date[0].split("-");
		var show_date = ref_date[0].split("-");
		var date_value = show_date[0] + "-" + show_date[1] + "-" + show_date[2] + " ";
		var show_time = ref_date[1].split(":");
		var time_value = " " + show_time[0] + ":" + show_time[1] + ":00";
		src_date = ref_date[0];
	}

	var date_html = "";
	if(required == "DATE" || required == "DATETIME"){
		//alert(document.getElementById(ref_tgt).value);

		if(show_date.length == 3){
			//alert("1");
			//alert(Number(show_date[1])-1);
			this_date = new Date();
			this_date.setFullYear(Number(show_date[0]));
			this_date.setDate(1);
			this_date.setMonth(Number(show_date[1]));
			this_date.setDate(-1);
			var temp_date = this_date.getDate() + 1;
			//alert("month = " + this_date.getDate());

			if(Number(show_date[2]) < (temp_date + 1)){
				this_date.setDate(Number(show_date[2]));
			}
			//this_date = new Date(Number(show_date[0]), (Number(show_date[1])-1), Number(show_date[2]));

			//var ref_day = Number(show_date[0]);
		}else{
			//alert("2");
			this_date = new Date();
			show_date[2] = this_date.getDate();
			show_date[1] = this_date.getMonth();
			show_date[0] = this_date.getFullYear();
		}
		current_date = new Date();
		var ref_day = this_date.getDate();
		//var ref_month = Number(show_date[1]);
		var ref_month = this_date.getMonth();
		this_date.setDate(1);
		start_day = this_date.getDay();
	}

	//alert(ref_day);

	if(required == "DATE" || required == "DATETIME"){

		date_html = "<table class=\"js-calender\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"280\">\n";

		/* **********************************************************
		*	Month and year row
		*********************************************************** */

			date_html += "<tr class=\"page_headings\" style=\"background-color: " + back_colour + ";\">\n";
			date_html += "<td colspan=\"7\" valign=\"middle\" style=\"text-align: center; padding: 4px;\" nowrap=\"nowrap\">\n";
				date_html += "<select name=\"changeDate\" style=\"margin-right: 10px;\" onchange=\"write_calander('" + show_date[0] + "-' + this.value + '-" + show_date[2] + time_value + "', '" + tgt_display + "', '" + ref_tgt + "', " + "'" + required + "', '" + is_search + "')\">\n";
				//alert(this_date.getMonth());
				for(i=1; i<months.length; i++){
					date_html += "<option value=\"" + (i) + "\"";
					if(i == this_date.getMonth()+1){
						date_html += " selected=\"selected\"";
						//alert(months[i]);
					}
					date_html += ">" + months[i] + "</option> \n";
				}
				date_html += "</select> \n";
				date_html += "<a href=\"javascript:write_calander('" + (Number(show_date[0]) - 10) + "-" + show_date[1] + "-" + show_date[2] + time_value + "', '" + tgt_display + "', '" + ref_tgt + "', " + "'" + required + "', '" + is_search + "')\" style=\" font-weight: bold; font-size: 14px; color: " + fore_colour + "; text-decoration: none;\"><<10</a> \n";
				date_html += "<select name=\"changeDate\" style=\"\" onchange=\"write_calander(this.value + '-" + show_date[1] + "-" + show_date[2] + time_value + "', '" + tgt_display + "', '" + ref_tgt + "', " + "'" + required + "', '" + is_search + "')\">\n";
				for(i=this_date.getFullYear()-5; i<this_date.getFullYear() + 6; i++){
					date_html += "<option value=\"" + i + "\"";
					if(i == this_date.getFullYear()){
						date_html += " selected=\"selected\"";
					}
					date_html += ">" + i + "</option> \n";
				}
				date_html += "</select> \n";
				date_html += "<a href=\"javascript:write_calander('" + (Number(show_date[0]) + 10) + "-" + show_date[1] + "-" + show_date[2] + time_value + "', '" + tgt_display + "', '" + ref_tgt + "', " + "'" + required + "', '" + is_search + "')\" style=\" font-weight: bold; font-size: 14px; color: " + fore_colour + "; text-decoration: none;\">10>></a> \n";
				date_html += "</td>\n";
			date_html += "</tr>\n";

			/* **********************************************************
			*	days of the week row
			*********************************************************** */

			date_html += "<tr style=\"background-color: #FFFFFF;\">\n";
				date_html += "<td width=\"40\" style=\"text-align: center; padding: 8px;\">Sun</td>\n" +
					"<td width=\"40\" style=\"text-align: center; padding: 8px;\">Mon</td>\n"+
					"<td width=\"40\" style=\"text-align: center; padding: 8px;\">Tue</td>" +
					"<td width=\"40\" style=\"text-align: center; padding: 8px;\">Wed</td>" +
					"<td width=\"40\" style=\"text-align: center; padding: 8px;\">Thu</td>" +
					"<td width=\"40\" style=\"text-align: center; padding: 8px;\">Fri</td>" +
					"<td width=\"40\" style=\"text-align: center; padding: 8px;\">Sat</td>";
			date_html += "</tr>\n";

			/* **********************************************************
			*	First week row
			*********************************************************** */

			date_html += "<tr style=\"background-color: #FFFFFF;\">\n";
			for(i=0; i<7; i++){
				if(i < this_date.getDay()){
					date_html += "<td>&nbsp;</td>\n";
				}else{
					if(ref_day != this_date.getDate() || Number(target_date[1]) != this_date.getMonth() + 1 ||  Number(target_date[0]) != this_date.getFullYear()){
						date_html += "<td style=\"text-align: center; padding: 5px; cursor: pointer;\" " +
						"onmouseover=\"this.style.background='" + mouse_over + "'\" " +
						"onmouseout=\"this.style.background='white'\" " +
						"onclick=\"set_date('" + ref_tgt + "', '" + this_date.getFullYear() + "-" + (this_date.getMonth()+1) + "-" + this_date.getDate() + time_value + "', '" + tgt_display + "', " + "'" + required + "', '" + is_search + "')\">\n" +
						this_date.getDate() + "\n</td>\n";
						//"onclick=\"set_date('" + ref_tgt + "', '" + this_date.getFullYear() + "-" + (this_date.getMonth()+1) + "-" + this_date.getDate() + time_value + "', '" + tgt_display + "', " + "'" + required + "', '" + is_search + "')\">\n" +
					}else{
						date_html += "<td style=\"text-align: center; padding: 5px; background-color: " + selected + ";\">" + this_date.getDate() + "</td>";
					}
					this_date.setDate(this_date.getDate()+1);
				}
			}
			date_html += "</tr>\n";

			/* **********************************************************
			*	Rest of the weeks
			*********************************************************** */

			while(this_date.getMonth() == (ref_month)){
				date_html += "<tr style=\"background-color: #FFFFFF;\">\n";
				for(i=0; i<7; i++){
					//if(this_date.getMonth() == (show_date[1]-1)){
					if(this_date.getMonth() == (ref_month)){
						if(ref_day != this_date.getDate() || Number(target_date[1]) != (this_date.getMonth() + 1) ||  Number(target_date[0]) != this_date.getFullYear()){
							date_html += "<td style=\"text-align: center; cursor: pointer; padding: 5px; background-color: #FFFFFF;\" " +
								"onmouseover=\"this.style.background='" + mouse_over + "'\" " +
								"onmouseout=\"this.style.background='white'\" " +
							"onclick=\"set_date('" + ref_tgt + "', '" + this_date.getFullYear() + "-" + (this_date.getMonth()+1) + "-" + this_date.getDate() + time_value + "', '" + tgt_display + "', " + "'" + required + "', '" + is_search + "')\">" +
							this_date.getDate() + "</td>";
							//alert(Number(target_date[1]) + ", " + (this_date.getMonth() + 1));
						}else{
							date_html += "<td style=\"text-align: center; padding: 5px; background-color: " + selected + ";\">" + this_date.getDate() + "</td>";
						}
					}else{
						date_html += "<td>&nbsp;</td>\n";
					}
					date_html += "</td>";
					this_date.setDate(this_date.getDate()+1);
				}
				date_html += "</tr>";
			}
		}

			/* **********************************************************
			*	Time
			*********************************************************** */

		if(required == "DATETIME"){
			date_html += "<tr class=\"page_headings\" style=\"background-color: #FFFFFF;\">\n";
			date_html += "<td colspan=\"7\" valign=\"middle\" style=\"text-align: center; padding: 5px; color: #000000;\" nowrap=\"nowrap\"> Time \n";
		}
		if(required == "TIME" || required == "DATETIME"){
					date_html += "<select " +
						"onChange=\"set_date('" + ref_tgt + "', '" + date_value + "' + this.value + ':" + show_time[1] + ":00', '" + tgt_display + "', " + "'" + required + "', '" + is_search + "')\" " +
						"id=\"hours\" name=\"hours\" style=\"margin-right: 3px;\">\n";
					for(i=0; i<24; i++){
						if(i<10){
							hour_value = "0" + i;
						}else{
							hour_value = i;
						}
						if(i==0){
							hour_display = "12";
						}else if(i > 12){
							hour_display = 	i - 12;
						}else{
							hour_display = i;
						}
						if(show_time[0] == hour_value){
							is_selected = "selected=\"selected\"";
						}else{
							is_selected = "";
						}
						if(i==0){
							date_html += "<optgroup label=\"AM\">";
						}else if(i==12){
							date_html += "<optgroup label=\"PM\">";
						}
						date_html += "<option value=\"" + hour_value + "\" " + is_selected + ">" + hour_display + "</option>\n";
						if(i==11 || i==23){
							date_html += "</optgroup>";
						}
					}
					date_html += "</select>:\n";
					date_html += "<select " +
						"onChange=\"set_date('" + ref_tgt + "', '" + date_value + show_time[0] + ":' + this.value + ':00', '" + tgt_display + "', " + "'" + required + "', '" + is_search + "')\" " +
						"id=\"minutes\" name=\"minutes\">\n";
					for(i=0; i<60; i++){
						if(i<10){
							minute_value = "0" + i;
						}else{
							minute_value = i;
						}
						if(show_time[1] == minute_value){
							is_selected = "selected=\"selected\"";
						}else{
							is_selected = "";
						}
						date_html += "<option value=\"" + minute_value + "\"" + is_selected + ">" + minute_value + "</option>\n";
					}
					date_html += "</select>\n";
					if(show_time[0] > 11){
						date_html += "PM";
					}else{
						date_html += "AM";
					}
		}
		if(required == "DATETIME"){
			date_html += "</td>\n";
			date_html += "</tr>\n";
		}
	if(required == "DATE" || required == "DATETIME"){
		if(is_search){
			if(is_search != "undefined"){
				date_html += "<tr class=\"page_headings\" style=\"background-color: #FFFFFF;\">\n";
					date_html += "<td colspan=\"7\" valign=\"middle\" style=\"text-align: center; padding: 5px; color: #000000;\" nowrap=\"nowrap\">\n";
						date_html += "<a href=\"javascript:set_search_date('" + ref_tgt + "', '" + is_search + "')\">Add date</a> ";
					date_html += "</td>\n";
				date_html += "</tr>\n";
			}
		}
		date_html += "</table>";
	}else if(required == "TIME"){
		if(is_search){
			if(is_search != "undefined"){
				date_html += "<a href=\"javascript:set_search_date('" + ref_tgt + "', '" + is_search + "')\" style=\"margin-left: 15px;\">Add date</a> ";
			}
		}
	}
	document.getElementById(tgt_display).innerHTML = date_html;
}

	/* **********************************************************
	*
	*	set_date
	*
	*********************************************************** */

function set_date(ref_tgt, tgt_date, tgt_display, required, is_search){
	var date_time_ar= tgt_date.split(" ");
	var sel_date = date_time_ar[0];
	var date_ar = sel_date.split("-");
	var month = date_ar[1];
	var day = date_ar[2];

	//alert(day);

	if(date_ar[1].length != 2){
		date_ar[1] = "0"+date_ar[1];
	}
	if(date_ar[2].length != 2){
		date_ar[2] = "0"+date_ar[2];
	}
	document.getElementById(ref_tgt).value = date_ar[0] + "-" + date_ar[1] + "-" + date_ar[2] + " " + date_time_ar[1];
	write_calander(tgt_date, tgt_display, ref_tgt, required, is_search);
}

	/* **********************************************************
	*
	*	set_search_date
	*
	*********************************************************** */

function set_search_date(ref_tgt, is_search){
	if(document.getElementById(is_search).value != ""){
		document.getElementById(is_search).value = (document.getElementById(ref_tgt).value);
	}else{
		document.getElementById(is_search).value += (document.getElementById(ref_tgt).value);
	}
}
