// custom inline skin caption and content stored inside string variables
var sCCaption = "<table width = \"100%\" cellspacing = \"0\" cellpadding = \"0\" border = \"0\"><tr><td bgcolor = \"#C0C0C0\" height = \"22\"><p class=\"dd_caption_blk\">Custom Inline Skin</p></td></tr></table>";
var sCContent = "<table width = \"100%\" cellspacing = \"0\" cellpadding = \"0\" border = \"0\"><tr><td bgcolor = \"#EDEDED\"><p class=\"dd_content_blk\">" + gen_text () + "</p></td></tr></table>";

// CSS Colors for Skins
var cap_css = new Array ("blk", "blue", "wht", "blk", "blk", "blk", "blk", "blk", "blk", "blk", "wht", "wht");
var con_css = new Array ("blk", "wht", "wht", "blk", "blk", "blk", "blk", "blk", "blk", "blk", "blk", "blk");


function open_window (sContent, iWndWidth, iWndHeight, iScroll)
{
	var iLeft = screen.width / 2 - iWndWidth / 2;
	var iTop = screen.height / 2 - iWndHeight / 2;
	opnWindow = window.open ('', 'opnWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+iScroll+',resizable=1,width='+iWndWidth+',height='+iWndHeight+',left='+iLeft+',top='+iTop+'');
	opnWindow.document.open ();
	opnWindow.document.write (sContent);
	opnWindow.document.close ();
	opnWindow.focus ();
}

function escape_html_js (str)
{
	re = /(\\\')/g; // escape the escaped apostrophe (\')
	str = str.replace (re, "\\\\\\'");
	
	re = /([^\\])'/g; // escape the apostrophe (')
	str = str.replace (re, "$1\\\'");
	
	re = /([\r]{0,1}[\n]{1})/g;
	str = str.replace (re, "\\n"); // escape the newline (\n)

	// escape first character in string if apostrophe and not escaped by regular expression replacement
	if (str.charAt (0) == "'")
		return "\\" + str;

	return str;
}

function css_replace (i)
{
	var str = document.getElementById ('caption').value;
	var re = /(dd_caption_)([\w]*)/g;
	str = str.replace (re, "$1" + cap_css[i]);
	document.getElementById ('caption').value = str;
	str = document.getElementById ('header').value;
	re = /(dd_content_)([\w]*)/g;
	str = str.replace (re, "$1" + con_css[i]);
	document.getElementById ('header').value = str;
	str = document.getElementById ('footer').value;
	re = /(dd_content_)([\w]*)/g;
	str = str.replace (re, "$1" + con_css[i]);
	document.getElementById ('footer').value = str;
}

function css_replace_str (type, str, i)
{
	var needle = "dd_content_";
	var clr = con_css[i];
	if (!type)
	{
		needle = "dd_caption_";
		clr = cap_css[i];
	}
	return str.replace (eval ("/(" + needle + ")([\\w]*)/g"), "$1" + clr);
}

function get_radio_value (sid)
{
	var id = eval ("document.frm." + sid);
	for (var i = 0; i < id.length; i++)
	{
		if (id[i].checked)
			return id[i].value;
	}
}

function snap_bounds (wnd)
{
	var limit = (wnd ? -1 : 0);
	document.getElementById ("limt").value = limit;
	document.getElementById ("limb").value = limit;
	document.getElementById ("liml").value = limit;
	document.getElementById ("limr").value = limit;
}

function gen_text ()
{
	return "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
}

function gen_close (obj)
{
	obj.value += "<p align = \"center\"><a href = \"#\" onclick = \"javascript: dd_obj[0].hide (1); return false;\">close</a></p>";
}

function gen_code (bout)
{
	var tmp;
	var fragments = new Array ();
	var wskin = get_radio_value ("skin");

	// ADVANCED: Global Drag and Drop Object Properties
	fragments["init"] = fragments["lang"] = "";
	if (get_radio_value ("sqr") == 1)
		fragments["init"] = "      dd_pkg.params ({SQRSKIN : true, HIDECVR : false}); ";
	fragments["init"] += "        dd_pkg.init (" + wskin + ");";

	fragments["count"] = "\x3Cscript type = \"text/javascript\">dd_count = " + document.getElementById ("objs").value + ";\x3C/script>\n\n";

	// ddobj relative path to Web page
	var rel_path = (bout ? document.getElementById ("relp").value : "");

	// language inclusion
	tmp = document.getElementById ("lang");
	if (tmp.selectedIndex > 0) // english is hard-coded default
		fragments["lang"] = "    \x3Cscript type = \"text/javascript\" src = \"" + rel_path + "ddobj/ddcal/js/language/" + tmp.options[tmp.selectedIndex].value + ".js\">\x3C/script>\n";
	// CSS template
	tmp = document.getElementById ("template");
	fragments["template"] = tmp.options[tmp.selectedIndex].value;

	// Calendar Properties
	fragments["params"] = "        calendar.params ({\n" + 
		"          SKIN : 0, \n" +
		"          INDEX : 0, \n" +
		"          CSS : '" + fragments["template"] + "', \n" +
		"          ROWS : " + document.getElementById ("rows").value + ", \n" +
		"          COLS : " + document.getElementById ("cols").value + ", \n" +
		"          WKEND_ALLOW : " + document.getElementById ("wkend").checked + ", \n" +
		"          WD_FIRST : " + document.getElementById ("firstd").value + ", \n" + 
		"          HOUR24 : " + (document.getElementById ("hour").checked ? 'true' : 'false') + ", \n" + 
		"          NAV_NOY : " + (document.getElementById ("noy").checked ? 'true' : 'false') + ", \n" +
		"          TIME_NOS : " + (document.getElementById ("tnos").checked ? 'true' : 'false') + ", \n" +
		"          TIME_NOM : " + (document.getElementById ("tnom").checked ? 'true' : 'false') + ", \n";

	tmp = document.getElementById ("trunc").value;
	if (parseInt (tmp) > 0)
		fragments["params"] += "          DAY_LEN : " + tmp + ", \n";
	tmp = document.getElementById ("focus").value;
	if (parseInt (tmp) > 0)
		fragments["params"] += "          CELL : " + tmp + ", \n";
	tmp = document.getElementById ("mask").value;
	if (tmp != "")
		fragments["params"] += "          MASK : '" + tmp + "', \n";
	tmp = document.getElementById ("caldate").value;
	fragments["input"] = tmp;
	if (tmp != "")
		fragments["params"] += "          INPUT : '" + tmp + "', \n";
	tmp = document.getElementById ("tmask").value;
	if (tmp != "")
		fragments["params"] += "          TMASK : '" + tmp + "', \n";
	tmp = document.getElementById ("caltime").value;
	fragments["time"] = tmp;
	if (tmp != "")
		fragments["params"] += "          TIME : '" + tmp + "', \n";
	tmp = document.getElementById ("minyr").value;
	if (tmp != "")
		fragments["params"] += "          MIN_YR : " + tmp + ", \n";
	tmp = document.getElementById ("maxyr").value;
	if (tmp != "")
		fragments["params"] += "          MAX_YR : " + tmp + ", \n";
	tmp = document.getElementById ("ddate").value;
	if (tmp != "")
		fragments["params"] += "          DDATE : '" + tmp + "', \n";
	if (parseInt (wskin) != 8) // not an inline custom skin
		fragments["params"] += "          CAP_DEFAULT : false, \n";
	else // inline custom skin (insert header and footer within calendar template itself)
		fragments["params"] += "          EMBED : true, \n";
	tmp = escape_html_js (document.getElementById ("caption").value);
	if (tmp)
		fragments["params"] += "          CAPTION : '" + css_replace_str (0, tmp, wskin) + "', \n";
	tmp = escape_html_js (document.getElementById ("header").value);
	if (tmp)
		fragments["params"] += "          HEADER : '" + css_replace_str (1, tmp, wskin) + "', \n";
	tmp = escape_html_js (document.getElementById ("footer").value);
	if (tmp)
		fragments["params"] += "          FOOTER : '" + css_replace_str (1, tmp, wskin) + "', \n";
	tmp = parseInt (document.getElementById ("width").value);
	if (parseInt (tmp) > 0)
		fragments["params"] += "          WIDTH : " + tmp + ", \n";

	fragments["params"] += "" + 
		"          ADJ_MONTH : " + document.getElementById ("adjm").checked + "\n" +
		"        });\n";

	// ADVANCED: Drag and Drop Object Properties
	fragments["oparams"] = "        dd_obj[0].params ({";

	// Initial (X, Y) Coordinate Params.
	tmp = parseInt (document.getElementById ("xpos").value);
	if (tmp > -1)
		fragments["oparams"] += "X : " + tmp + ", ";
	tmp = parseInt (document.getElementById ("ypos").value);
	if (tmp > -1)
		fragments["oparams"] += "Y : " + tmp + ", ";
	// Width Parameter Assignment
	var wwidth = parseInt (document.getElementById ("width").value);
	wwidth = wwidth ? wwidth : 350;

	// Opacity Params.
	tmp = parseInt (document.getElementById ("opacity").value);
	if (tmp >= 25 && tmp < 100)
		fragments["oparams"] += "OPACITY_ON : " + tmp + ", ";
	tmp = parseInt (document.getElementById ("opacityoff").value);
	if (tmp != 55 && tmp >= 25 && tmp <= 100)
		fragments["oparams"] += "OPACITY_OFF : " + tmp + ", ";

	// Offset X, Y Params.
	tmp = parseInt (document.getElementById ("offx").value);
	if (tmp != -15)
		fragments["oparams"] += "OFFX : " + tmp + ", ";
	tmp = parseInt (document.getElementById ("offy").value);
	if (tmp != -15)
		fragments["oparams"] += "OFFY : " + tmp + ", ";

	// Close Param.
	if (!document.getElementById ("ctrl").checked)
		fragments["oparams"] += "CONTROL : false, ";

	// Resize Params.
	if (document.getElementById ("resize").checked)
		fragments["oparams"] += "RESIZE : true, ";
	if (document.getElementById ("scroll").value != "")
		fragments["oparams"] += "SCROLL : '" + document.getElementById ("scroll").value + "', ";
	if (document.getElementById ("minw").value)
		fragments["oparams"] += "MINW : " + parseInt (document.getElementById ("minw").value) + ", ";
	if (document.getElementById ("minh").value)
		fragments["oparams"] += "MINH : " + parseInt (document.getElementById ("minh").value) + ", ";
	if (document.getElementById ("maxw").value)
		fragments["oparams"] += "MAXW : " + parseInt (document.getElementById ("maxw").value) + ", ";
	if (document.getElementById ("maxh").value)
		fragments["oparams"] += "MAXH : " + parseInt (document.getElementById ("maxh").value) + ", ";
	if (document.getElementById ("scrollh").value)
		fragments["oparams"] += "H : " + parseInt (document.getElementById ("scrollh").value) + ", ";

	// Boundary Params.
	fragments["oparams"] += "LIMIT_T : " + parseInt (document.getElementById ("limt").value) + ", ";
	fragments["oparams"] += "LIMIT_L : " + parseInt (document.getElementById ("liml").value) + ", ";
	fragments["oparams"] += "LIMIT_B : " + parseInt (document.getElementById ("limb").value) + ", ";
	fragments["oparams"] += "LIMIT_R : " + parseInt (document.getElementById ("limr").value) + ", ";

	// Anchor Params.
	var anch = document.getElementById ("ancid").value;
	if (anch)
		fragments["oparams"] += "ANCHOR : '" + anch + "', ";
	if (document.getElementById ("ancw").checked)
		fragments["oparams"] += "ANCHOR_W : true, ";
	if (document.getElementById ("anch").checked)
		fragments["oparams"] += "ANCHOR_H : true, ";

	// Drag Param.
	fragments["oparams"] += "DRAG : " + get_radio_value ("drag") + 
		"});";
	
	fragments["readonly"] = document.getElementById ("ro").checked ? " readonly" : "";

	// generate ddobj embedded Web Page based on Wizard
	var sContent = sShell = "";
	if (bout)
	{ // generated code output
		sShell = "<html>\n" +
		"  <head>\n\n" +
		"    <!-- ISO-8859-1 CHARSET -->\n" +
		"    <meta http-equiv = \"Content-Type\" content = \"text/html; charset=iso-8859-1\">\n" + 
		"    <title>ddcal Drag and Drop Calendar: Generated Source Code</title>\n\n" +
		"  </head>\n\n" +
		"  <body bgcolor = \"#FFFFFF\">\n\n" +
		"  <pre>";
	}

	sContent += "<html>\n" +
		"  <head>\n\n" +
		"    <!-- UNICODE: Supports multilingual pages -->\n" +
		"    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" + 
		"    <title>ddcal Drag and Drop Calendar</title>\n\n" +
	
		"    <!-- ddobj, calendar style sheet: template style properties -->\n" +
		"    <link rel = \"stylesheet\" href = \"" + rel_path + "ddobj/ddcal/css/ddcal.css\" type = \"text/css\">\n" +
		"    <link rel = \"stylesheet\" href = \"" + rel_path + "ddobj/css/ddobj.css\" type = \"text/css\">\n\n" +
	
		"    <!-- override default maximum concurrent drag and drop objects -->\n" +
		"    " + fragments["count"] +
		"    <!-- loading ddobj and calendar javascript files -->\n" +
		"    \x3Cscript type = \"text/javascript\" src = \"" + rel_path + "ddobj/js/" + (document.getElementById ("ddoptimal").checked ? "ddobjo.js" : "ddobj.js") + "\">\x3C/script>\n" +
		"    \x3Cscript type = \"text/javascript\" src = \"" + rel_path + "ddobj/ddcal/js/ddcal.js\">\x3C/script>\n" +
		fragments["lang"] +

		"\n    <!-- initializing calendar properties -->\n" +
		"    <!-- instance of ddcal object (duplicate and modify as many instances as needed on this Web Page) -->\n" +
		"    \x3Cscript type = \"text/javascript\">\n" +
		"      function init_cal ()\n" +
		"      {\n" +
		(document.getElementById ("ddcopy").checked ? "        dd_copyright = false;\n" : "") +
 		fragments["init"] + " // initialize framework with skin selection(s)\n" +
 		fragments["oparams"] + "\n" +
		fragments["params"] + 
		"      }\n" +
		"    \x3C/script>\n\n"+
		"  </head>\n\n" +
	
		"  <!-- initializing ddobj global properties, skin selector and preloader -->\n" +
		"  <body bgcolor = \"#FFFFFF\" onload = \"javascript: init_cal ();\">\n\n" +

		"    <form id = \"calform\" name = \"calform\" action = \"\" method = \"post\">\n" +
		"      <input id = \"" + fragments["input"] + "\" name = \"" + fragments["input"] + "\" type = \"text\" value = \"\"" + fragments["readonly"] + ">&nbsp;&nbsp;\n";
		if (document.getElementById ("caltime").value != "")
		{
			sContent += "" +
			"      <input id = \"" + fragments["time"] + "\" name = \"" + fragments["time"] + "\" type = \"text\" value = \"\"" + fragments["readonly"] + ">&nbsp;&nbsp;\n";
		}

		// no reset parameter (not applicable to onload popup and standard tooltip)
		var noreset = document.getElementById ("noreset").checked ? "dd_obj[0].params ({NORESET : true}); " : "";

		sContent += "" + 
		"      <a href = \"#\" onclick = \"javascript: " + noreset + "calendar.show (); return false;\">\n" +
		"        <img src = \"" + rel_path + "ddobj/ddcal/images/calendar.gif\" border = \"0\" align = \"absmiddle\">\n" +
		"      </a>\n" +
		"    </form>\n\n" +

		"    <p>Click the icon to display the pop-up calendar.</p>\n";

		// Anchor Params.
		if (anch)
			sContent += "\n    <div id = \"" + anch + "\" name = \"" + anch + "\" style = \"position: relative; border: solid #0000FF; width: 150px; height: 40px;\">HTML Element Anchor</div>\n";

		sContent += "\n" +
		"  </body>\n" +
		"</html>\n";

	if (bout)
	{ // generated code output
		re = new RegExp('<', 'g');
		sContent = sContent.replace (re, '&lt;');
		re = new RegExp('>', 'g');
		sContent = sContent.replace (re, '&gt;');

		sContent = sShell + sContent +
		"  </pre>\n\n" +
		"  </body>\n" +
		"</html>\n";
	}

	open_window (sContent, 900, 500, 1);
}

