/*File Picker functions*/

// File Picker modification for FCK Editor v2.0 - www.fckeditor.net
// by: Pete Forde <pete@unspace.ca> @ Unspace Interactive

var urlobj;

function textbox_browser(n)
{    
    /*var children = "children ids\n";
    for (var i=0; i<n.parentNode.childNodes.length; i++) children+=n.parentNode.childNodes[i].id+"\n";
    alert (children);*/
    
    var sib = n.parentNode.childNodes[1].id;
    //alert(sib);
    BrowseServer(sib);
}
function BrowseServer(obj)
{
	urlobj = obj;
	
	//FCKConfig.BasePath + 'filemanager/browser/default/browser.html?Connector=../../connectors/' + _FileBrowserLanguage + '/connector.' + _FileBrowserExtension
	
	OpenServerBrowser(
		'fckeditor/editor/filemanager/browser/default/browser.html?Connector=../../connectors/aspx/connector.aspx',
		screen.width * 0.7,
		screen.height * 0.7 ) ;
		
	/*OpenServerBrowser(
		'fckeditor/editor/fckdialog.html',
		screen.width * 0.7,
		screen.height * 0.7 ) ;*/
}

function OpenServerBrowser( url, width, height )
{
	var iLeft = (screen.width  - width) / 2 ;
	var iTop  = (screen.height - height) / 2 ;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	var oWindow = window.open( url, "BrowseWindow", sOptions ) ;
}

function SetUrl( url, width, height, alt )
{
	document.getElementById(urlobj).value = url ;
	oWindow = null;
}

/*Date picker functions*/
//NOTE: This was used because the .Net built in Calendar created all sorts of
//		problems when being used with a non Internet exploror browser.

// modified by BG to use ISO 8601 dates (YYYY-MM-DD) on 2003-05-16 :^)
// Modified to rely on mouse position and to go back to MM/DD/YYYY
// Relies on the fact that there's 2 spans with the names startpick and endpick, and needs the client id's of
// startDateText and endDateText

// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Script featured on: http://javascriptkit.com/script/script2/timestamp.shtml
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations


//this opens a calendar
//button: the button and the textfield it opens

function opencal(button){
    
	//check if other calendar open
	var pick = document.getElementById("picker");
	if (pick){
		close_calendar();
	}
	
	var i=0;
	var siblings = button.parentNode.childNodes;
	
    while ((i<siblings.length)&&(siblings[i].tagName!="INPUT")){  
        //alert (sib.id);      
        i++;
    }
    	
	var sib = siblings[i];
	
	var str_target = sib.id;
	//alert (sib.id);
	var str_datetime = sib.value;	
	
	show_calendar(str_target, str_datetime);
	//alert("textbox: " + tbox.id+ " value: "+tbox.value);	
}
function getX(obj){
    var currleft = 0;
    if (obj.offsetParent){
        do{
            currleft += obj.offsetLeft;
        }while (obj = obj.offsetParent);
    }
    return currleft;
}
function getY(obj){
    var currtop = 0;
    if (obj.offsetParent){
        do{
            currtop += obj.offsetTop;
        }while (obj = obj.offsetParent);
    }
    return currtop;
}
//draws the calendar
//str_target: the date
//str_datetime: the textbox with the date

function show_calendar(str_target, str_datetime){
    //get target input name
	var inputDateField = document.getElementById(str_target);

    var xcoord = getX(inputDateField);
    var ycoord = getY(inputDateField);
    //alert(xcoord+" "+ycoord);

	var arr_months = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
	var week_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
	var n_weekstart = 0; // day week starts from (normally 0 or 1)

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
	
	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setTime(dt_datetime.getTime()-30*24*60*60*1000);
	//subtract 28 days;
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setTime(dt_datetime.getTime()+30*24*60*60*1000);
	//add 28 days;
	var dt_firstday = new Date(dt_datetime);
	
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	//alert (targ.id);
	//make targ name
		
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		
		//use a div instead		
		"<table align=\"center\" cellspacing=\"0\" border=\"0\" class=\"calendar_table\">\n"+
		"<tr><td>&nbsp;</td><td width=\"85%\"><div class=\"cal_header\">"
		+arr_months[dt_datetime.getMonth()]+"<br>"+dt_datetime.getFullYear()+"</div></td>"+
		"<td align=\"right\"><a href=\"javascript:close_calendar();\"><div class=\"cal_buttons\">x</div></a></td></tr>\n"+		
		"<tr valign=\"bottom\"><td><a href=\"javascript:show_calendar('"+ 
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+ 
		"<div class=\"cal_buttons\">&lt;</div></a></td>\n"+ //Prev Button
		"<td>&nbsp;</td>"+
		"<td align=\"right\"><a href=\"javascript:show_calendar('" 
		+str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
		"<div class=\"cal_buttons\">&gt;</div></a></td>\n</tr>\n"+ //Next Button
		"<tr><td colspan=\"3\">\n"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"
		//Month and Year		
	);

	var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td><div class=\"cal_weeknames\">"+
		week_days[(n_weekstart+n)%7]+"</div></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// print current date
					str_buffer += "	<td class=\"cal_chosen_day\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td class=\"cal_weekend_day\">";
				else
					// print working days of current month
					str_buffer += "	<td class=\"cal_weekday_day\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:assignDate('"+str_target+"','"
					+dt2dtstr(dt_current_day)+"');\">"+
					"<div class=\"cal_currentmonth\">";
				else 
					// print days of other months
					str_buffer += "<a href=\"javascript:assignDate('"+str_target+"','"
					+dt2dtstr(dt_current_day)+"');\">"+
					"<div class=\"cal_othermonth\">";
				str_buffer += dt_current_day.getDate()+"</div></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	str_buffer +=
		"</table>\n" +
		"</td></tr>\n</td>\n</table>\n";
		//"</body>\n" +
		//"</html>\n";

	//check to see if another calendar window is open
	if (document.getElementById("picker") != null)
		close_calendar();

	var newdiv = document.createElement("DIV");
	newdiv.setAttribute("id","picker");
	
	//set style and position of the calendar
	newdiv.style.backgroundColor = "White";
	newdiv.style.borderStyle = "solid";
	newdiv.style.borderWidth = 2;

	newdiv.style.position = "absolute";
	newdiv.style.left = xcoord +"px";
	newdiv.style.top = ycoord + inputDateField.offsetHeight + "px";
	//newdiv.style.width = 190; //must be used in firefox

	newdiv.style.zIndex = 1;
	
	newdiv.innerHTML = str_buffer;
		
	document.body.appendChild(newdiv); //append div to body
	//newdiv.onmouseover = opencal;
	
}
function assignDate(dateboxID, newdate){
    var date = document.getElementById(dateboxID);
    date.value = newdate;
    close_calendar();
}
//looks for the calendar if it's there and closes it
function close_calendar(){
	var pick = document.getElementById("picker");
	if (pick) pick.parentNode.removeChild(pick);	
}

// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
	//var re_date = /^(\d+)\/(\d+)\/(\d+)\s+(\d+)\:(\d+)$/; //
	var re_date = /^(\d+)\/(\d+)\/(\d+)\s?$/;
	if (!re_date.exec(str_datetime))
		return alert("Invalid Datetime format: "+ str_datetime);
	return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2));
}
function dt2dtstr (dt_datetime) {
	var month = (dt_datetime.getMonth () + 1);
	if (month < 10) month = "0" + month;
	var day = dt_datetime.getDate ();	
	if (day < 10) day = "0" + day;
	return (new String (
			month + "/" + day + "/" +dt_datetime.getFullYear()));
}
function dt2tmstr (dt_datetime) {
	return ("");
}