// 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 ('ddcaption').value;
	var re = /(dd_caption_)([\w]*)/g;
	str = str.replace (re, "$1" + cap_css[i]);
	document.getElementById ('ddcaption').value = str;
	str = document.getElementById ('ddcontent').value;
	re = /(dd_content_)([\w]*)/g;
	str = str.replace (re, "$1" + con_css[i]);
	document.getElementById ('ddcontent').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_code (bout)
{
	var tmp;
	var fragments = new Array ();
	var wskin = get_radio_value ("skin");
	var wtype = get_radio_value ("ddtype");

	// ADVANCED: Global Drag and Drop Object Properties
	fragments["init"] = "";
	if (get_radio_value ("sqr") == 1)
		fragments["init"] = "      dd_pkg.params ({SQRSKIN : true, HIDECVR : false});\n";
	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 : "");


	// 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") + 
		"});";

	if (wtype == 4)
	{ // caption and content are static based on data validation alert type
		fragments["caption"] = css_replace_str (0, '<p class=\"dd_caption_wht\">Window Alert</p>', wskin);
		fragments["content"] = css_replace_str (1, '<p class=\"dd_content_blk\" style=\"font-weight: bold;\">Amount provided is less than 5.</p>', wskin);

	}
	else
	{ // caption and content come from text areas
		fragments["caption"] = escape_html_js (document.getElementById ("ddcaption").value);
		fragments["content"] = escape_html_js (document.getElementById ("ddcontent").value);
	}

	// 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>ddobj Drag and Drop Objects: 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>ddobj Drag and Drop Objects</title>\n\n" +
	
		"    <!-- ddobj style sheet: caption and content style properties -->\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 javascript file -->\n" +
		"    \x3Cscript type = \"text/javascript\" src = \"" + rel_path + "ddobj/js/" + (document.getElementById ("ddoptimal").checked ? "ddobjo.js" : "ddobj.js") + "\">\x3C/script>\n" +

		"\n    <!-- initializing custom properties -->\n" +
		"    \x3Cscript type = \"text/javascript\">\n" +
		"      function init_dd ()\n" +
		"      { // initialize all ddobj stuff here when page finishes loading.\n" +
		(document.getElementById ("ddcopy").checked ? "        dd_copyright = false;\n" : "") +
 		fragments["init"] + " // initialize framework with skin selection(s)\n" +
 		fragments["oparams"] + "\n" +
		"      }\n\n";

		if (wtype == 4)
		{ // conditional input trigger
			sContent += "      function dd_validate (i)\n" +
			"      { // show object following activation condition (data validation alert)\n" +
			"        if (parseFloat (document.getElementById (\"txt\").value) < 5)\n" +
			"        {\n" +
			"          var caption = '" + fragments["caption"] + "';\n" +
			"          var content = '" + fragments["content"] + "';\n" +
			"          dd_obj[i].show (" + wskin + ", caption, content, " + wwidth + ");\n" +
			"          dd_obj[i].release ();\n" +
			"          return false;\n" +
			"        }\n" +
			"        else // automatically remove open popup if input is valid\n" +
			"          dd_obj[i].hide (true);\n" +
			"      }\n\n";
		}
		else
		{ // all other triggers
			sContent += "\n" +
			"      function show_obj (i)\n" +
			"      { // show object following activation\n" +
			"        var caption = '" + fragments["caption"] + "';\n" +
			"        var content = '" + fragments["content"] + "';\n" +
			"        dd_obj[i].show (" + wskin + ", caption, content, " + wwidth + ");\n" +
			(anch || wtype == 0 || wtype == 3 ? "        dd_obj[i].release ();\n" : "") +
			"        return false;\n" +
			"      }\n";
		}

		sContent += "" +
		"    \x3C/script>\n\n"+
		"  </head>\n\n" +
	
		"  <!-- initializing ddobj global properties, skin selector and preloader -->\n" +
		"  <body bgcolor = \"#FFFFFF\" onload = \"javascript: init_dd ();" + (wtype == 0 ? " show_obj (0);" : "") + "\">\n\n";

		// no reset parameter (not applicable to onload popup and standard tooltip)
		var noreset = document.getElementById ("noreset").checked && wtype != 1 && wtype != 0 ? "dd_obj[0].params ({NORESET : true}); " : "";

		if (wtype == 4)
		{ // conditional input activates tooltip
			sContent += "" +
			"    <form id = \"frm\" name = \"frm\" action = \"\" method = \"post\">\n" +
			"      <input id = \"txt\" name = \"txt\" type = \"text\" value = \"0\" size = \"4\" onblur = \"javascript: " + noreset + "dd_validate (0);\">\n" +
			"        Enter a number less than 5.\n" +
			"      <input type = \"button\" value = \"Validate\" onclick = \"javascript: " + noreset + "dd_validate (0);\">\n" +
			"    </form>\n\n";
		}
		else if (wtype != 0)
		{ // hyperlink activates tooltip
			sContent += "" + 
			"    <a href = \"#\" " + (wtype == 1 || wtype == 2 ? "onmouseover" : "onclick") + " = \"javascript: " + noreset + "show_obj (0); return false;\"" + 
			(wtype != 3 ? " onmouseout = \"javascript: " + noreset + "dd_obj[0].release ();" + (wtype == 1 ? " dd_obj[0].hide ();" : "") + " return false;\"" : "") + 
			(wtype == 1 || wtype == 2 ? " onclick = \"javascript: return false;\"" : "") + ">\n" + 
			"      Hyperlink activates tooltip.\n" +
			"    </a>\n";
		}
		else // page onload activates popup
			sContent += "    <p>Popup window appears on completion of Web page load.</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);
}

