var lastEditedRecord = -1;  //holds the current edited record index

var scr_data_raw = {};
var scr = {};
var scr_fields = new Array();
var scr_data = new Array();
var scr_tips = new Array();

var validationErrors = new Array();
var currentErrorField = null;

var recordChanged = false;
var screenChanged = false;


//types

function FieldValidationError(code, text, warning)
{
	var result = {};
	result.code = code;
	result.text = text;
	result.isWarning = warning;
	
	return result;
}

function clearSelection() 
{
	try
	{
		var sel ;
		if(document.selection && document.selection.empty)
		{
			document.selection.empty() ;
		} 
		else if(window.getSelection) 
		{
			sel=window.getSelection();
			if(sel && sel.collapse)
				try{sel.collapse();}catch(f){}
			if(sel && sel.removeAllRanges)
				sel.removeAllRanges();
		}
	}
	catch(e){}
} 

function getPosition(e) {
	var left = 0, top = 0;

	while (e.offsetParent) {
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}

	left += e.offsetLeft;
	top += e.offsetTop;

	return {x: left, y: top};
}


function nextScreenTip()
{
	var block = document.getElementById("tipsBlock");
	if (!block.position) block.position = 0;
	block.position++;
	adjustScreenTips();
}

function prevScreenTip()
{
	var block = document.getElementById("tipsBlock");
	if (!block.position) block.position = 0;
	block.position--;
	adjustScreenTips();
}

function adjustScreenTips()
{
	var content = "";
	
	if (document.getElementById("tipsCenter") && scr_tips && scr_tips.length)
	{
		for (var i=0;i<scr_tips.length;i++)
		{
			content += scr_tips[i].text;
			if (i+1<scr_tips.length)
				content += "<br/><br/>";
		}
		
		document.getElementById("tipsCenter").innerHTML = content;
	}
	
	if (document.getElementById("tipsBlock"))
	{
		if (scr_tips && scr_tips.length == 0)
		{
			document.getElementById("tipsBlock").style.display = "none";
		}
		else
		{
			document.getElementById("tipsBlock").style.display = "";
		}
	}
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix) return getXmlHttpPrefix.prefix;

	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;

	for (var i = 0; i < prefixes.length; i++) 
	{
		try 
		{
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		} 
		catch (ex) {}
	}
}

function getSocket() {
	//try 
	//{
		if (window.XMLHttpRequest) 
		{
			var req = new XMLHttpRequest();
			// implement readyState property and onreadystatechange event on all browsers
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
      
      		// flag to indicate to manually appley "onreadystatechange" event when in sync mode
    		req.verifySyncCall = true;
      
			req.trueOpen = req.open;
			req.open = function(method, url, async) 
			{
				req.async = async;
			    return req.trueOpen(method, url, async);
          	};
			
			return req;
		}
		if (window.ActiveXObject) 
		{
			var req = new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
			return req;
		}
	//}
	//catch (ex) {}
}

function jsonRequest(url, callBackFunction, postData)
{
	try
	{
		document.getElementsByTagName("body")[0].style.cursor = "wait";
		var req = getSocket();
		req.open("POST", url, /*async*/true);
		req.onreadystatechange = function()
		{
		   if (req.readyState == 4 /*complete*/) 
		   {
		       var obj = JSON.parse(req.responseText);
		       document.getElementsByTagName("body")[0].style.cursor = "";
		       callBackFunction(obj);
		   }
		   //else
		   //	req.onreadystatechange = jsonHandler;
		}
		
		if (!postData)
			postData = null;
			
		req.send(postData);
	}
	catch(e)
	{
		document.getElementsByTagName("body")[0].style.cursor = "";
	}
}


function requestScreenData(screenId)
{
	if (document.getElementById("hiddenTemplate"))
	{
		document.getElementById("hiddenTemplate").value = hiddenTemplate;
	}
	
	jsonRequest("getData?screenId=" + screenId, loadScreenData);
}

function loadScreenData(data)
{
	if (data.exception)
	{
		if (data.exception.code == 200)
		{
			window.location.href = "/login.do";
		}
		else
		{
			alert("detected error: " + data.exception.message);
			window.location.href = "/";
		}
	}
	else if (data.screen)
	{
		//alert("screen comment: " + data.screen.comment);
		//alert("data length: " + data.data.length);
		scr_data_raw = data;
		scr = data.screen;
		scr_data = data.data;
		scr_fields = data.fields;
		scr_tips = data.tips;
		
		adjustScreenTips();
		
		fillCombos(data.fieldValues);
		
		if (data.screen.multiRecord)
			initializeMultiRecordScreen();
		else
			initializeSingleRecordScreen();
	}
	//alert(data);
}

function fillCombos(values)
{
	for (var i=0;i<scr_fields.length;i++)
	{
		//normal selection box
		if (scr_fields[i].type == 4)
		{
			var element = document.getElementById(scr_fields[i].name);
			
			if (scr_fields[i].special && scr_fields[i].special.indexOf("OTHER") > -1)
			{
				element.options[element.options.length] = new Option("אחר - לא מהרשימה", "אחר");				
			}
			
			if (values[scr_fields[i].name])
			{
				for (var k=0;k<values[scr_fields[i].name].length;k++)
				{
					var value = (scr_data_raw.gender == 2 ? values[scr_fields[i].name][k].femaleText :  values[scr_fields[i].name][k].maleText);
					var displayText = value;
					
					if (values[scr_fields[i].name][k].value)
						value = values[scr_fields[i].name][k].value;
						
					element.options[element.options.length] = new Option(displayText, value);
				}
			}
		}
		//special 5 rows language selection
		if (scr_fields[i].type == 12)
		{
			if (values[scr_fields[i].name])
			{
				for (var k=0;k<values[scr_fields[i].name].length;k++)
				{
					var value = values[scr_fields[i].name][k];
					
					if (value.special == "lang") //fill the language combos
					{
						for (var j=0;j<4;j++)
						{
							var element = document.getElementById(scr_fields[i].name + j);
							element.options[element.options.length] = new Option(values[scr_fields[i].name][k].maleText, values[scr_fields[i].name][k].maleText);
						}
					}
					else
					{
						for (var j=0;j<4;j++)
						{
							var element = document.getElementById(scr_fields[i].name + "level" + j);
							element.options[element.options.length] = new Option(values[scr_fields[i].name][k].maleText, values[scr_fields[i].name][k].maleText);
						}
					}
				}
			}
		}
		//auto complete field
		if (scr_fields[i].type == 16)
		{
			var theArray = new Array();
			if (values[scr_fields[i].name])
			{
				for (var k=0;k<values[scr_fields[i].name].length;k++)
				{
					var value = (scr_data_raw.gender == 2 ? values[scr_fields[i].name][k].femaleText :  values[scr_fields[i].name][k].maleText);
					var displayText = value;
					
					if (values[scr_fields[i].name][k].value)
						value = values[scr_fields[i].name][k].value;
						
					theArray[theArray.length] = displayText;
				}
			}
			
			$("#" + scr_fields[i].name).autocompleteArray(
					theArray,
					{
						delay:1,
						minChars:1,
						matchSubset:1,
						autoFill:true,
						maxItemsToShow:20
					}
				);
						//			onItemSelect:selectItem,
						//			onFindValue:findValue,
		}
	}
}

function initializeSingleRecordScreen()
{
	screenChanged = false;
	if (!scr_data) scr_data = new Array();
	
	if (scr.id == 11)
	{
		var templates = scr_data_raw.templates;
		
		
		document.getElementById("screenRightBanner").style.display = "none";
		document.getElementById("screenHeader").style.marginRight = "0px";
		document.getElementById("titlePanel").className = "";
		document.getElementById("titlePanel").style.marginRight = "44px";
		
		var headerDiv = document.createElement("DIV");
		
		var headerImg = document.createElement("DIV");
		headerImg.appendChild(document.getElementById("screenHeader"));

		headerImg.setAttribute("style","float:right");
		headerImg.style.styleFloat = "right";
		headerImg.style.width = "100px";
		headerImg.style.height = "35px";
		headerDiv.appendChild(headerImg);
		
		var headerComment = document.createElement("DIV");
		headerComment.appendChild(document.getElementById("screenComment"));
		headerComment.setAttribute("style","float:right");
		headerComment.style.styleFloat = "right";
		headerComment.style.width = "500px";
		headerComment.style.height = "35px";
		
		headerDiv.appendChild(headerComment);
		
		if (templates.length > 6 && navigator.userAgent.toLowerCase().indexOf('msie 6') == -1)
		{
			
			headerComment = document.createElement("DIV");
			headerComment.setAttribute("style","float:right");
			headerComment.style.styleFloat = "right";
			if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
				headerComment.style.width = "166px";
			else
				headerComment.style.width = "318px";
			headerComment.style.height = "35px";
		

			var anotherNext = document.createElement("DIV");
			anotherNext.className = "nextLinkOrange";
			anotherNext.onclick = function() {
				saveScreen(+1);
			}
			anotherNext.innerHTML = "<img src=\"images/common/spacer.gif\"/>";
			headerComment.appendChild(anotherNext);
			
			headerDiv.appendChild(headerComment);
		}
		
		document.getElementById("titlePanel").appendChild(headerDiv);
		
		var mainDiv = document.createElement("DIV");
		mainDiv.id = "templatesContainer";
		mainDiv.className = "templatesContainer";
		
		var i;
		for (i=0;i<templates.length;i++)
		{
			var div = document.createElement("DIV");
			div.className = "templateSelectionBox";
			
			var pic = document.createElement("DIV");
			pic.id = "templateSelector" + i;
			pic.className = "templatePreviewPic";
			pic.template = templates[i];
			if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
			{
				pic.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/wizard/templates/T" + templates[i].id + (templates[i].id == scr_data[0].template ? "_Select" : "") + ".png')";
			}
			else
			{
				pic.style.backgroundImage = "url(\"images/wizard/templates/T" + templates[i].id + (templates[i].id == scr_data[0].template ? "_Select" : "") + ".png\")";
			}
			
			pic.position = i;
			
			pic.onmouseover = function()
			{
				showTemplateTooltip(this, this.template.text);
			}
			
			pic.onmouseout = function()
			{
				hideTemplateTooltip();
			}
			
			if (scr_data_raw.isProUser || !pic.template.pro)
			{
				pic.onclick = function()
				{
					//clear old selected box
					var i=0;
					var selector = document.getElementById("templateSelector" + i);
					while (selector)
					{
						if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
						{
							selector.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/wizard/templates/T" + selector.template.id  + ".png')";
						}
						else
						{
							selector.style.backgroundImage = "url(\"images/wizard/templates/T" + selector.template.id + ".png\")";
						}
						
						selector = document.getElementById("templateSelector" + (++i));
					}
					//mark the new one
					if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
						this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/wizard/templates/T" + this.template.id  + "_Select.png')";
					else
						this.style.backgroundImage = "url(\"images/wizard/templates/T" + this.template.id + "_Select.png\")";
					
					document.getElementById("template").value = this.template.id;
				}
				
				pic.ondblclick = function()
				{
					//clear old selected box
					var i=0;
					var selector = document.getElementById("templateSelector" + i);
					while (selector)
					{
						selector.style.backgroundImage = "url(\"images/wizard/templates/T" + selector.template.id + ".png\")";
						selector = document.getElementById("templateSelector" + (++i));
					}
					//mark the new one
					this.style.backgroundImage = "url(\"images/wizard/templates/T" + this.template.id + "_Select.png\")";
					
					document.getElementById("template").value = this.template.id;
					
					saveScreen(1);
				}
			}
			else
			{
				pic.onclick = function()
				{
					showDialogBox("ok", "תבנית זו זמינה למשתמשי רזומט פרו בלבד.<br/> תוכל <a href=\"product_pro.do\" target=\"_blank\">\"לשדרג\"</a> את חשבונך כעת.<br/>", null, null);
				}
			}
			
			div.appendChild(pic);
			
			var descDiv =  document.createElement("DIV");
			descDiv.className = "templateTitle";
			descDiv.innerHTML = templates[i].name;
			div.appendChild(descDiv);
			//descDiv =  document.createElement("DIV");
			//descDiv.className = "templateText";
			//descDiv.innerHTML = templates[i].text;
			//div.appendChild(descDiv);
			
			var preview =  document.createElement("DIV");
			preview.className = "templatePreviewButton";
			preview.templateId = templates[i].id;
			preview.onclick = function ()
			{
				showTemplatesPreviewDialog(this.templateId);
			}
			div.appendChild(preview);
			
			mainDiv.appendChild(div);
		}
		
		
		while (i % 6 != 0)
		{
			var div = document.createElement("DIV");
			div.className = "templateEmptySelectionBox";
			mainDiv.appendChild(div);
			i++;
		}
		
		document.getElementById("titlePanel").appendChild(mainDiv);
		
		/*mainDiv = document.createElement("div");
		mainDiv.className = "templatesBanner";
		var tbanner = document.createElement("img");
		tbanner.src = "/images/wizard/banners/templates_left_banner.png";
		tbanner.height = 202;
		tbanner.width = 130;
		mainDiv.appendChild(tbanner);
		
		document.getElementById("titlePanel").appendChild(mainDiv);*/
		
		lastEditedRecord = 0;
		document.getElementById("template").value = scr_data[0].template;
		
		if (document.getElementById("template").value == "0" && document.getElementById("templateSelector0"))
		{
			document.getElementById("templateSelector0").onclick();
		}
		

	}
	else
	{
		disableEnableContentTable(true);
		
		if (document.getElementById("tipsPanel"))
			document.getElementById("tipsPanel").style.display = "";
		if (document.getElementById("fieldsPanel"))
			document.getElementById("fieldsPanel").style.display = "";
		
		if (scr_data.length > 0)
			selectScreenRecord(0);
		else
			addScreenRecord();
	}
}

function initializeMultiRecordScreen()
{
	recordChanged = false;
	screenChanged = false;
	
	if (!scr_data) scr_data = new Array();
	
	document.getElementById("tipsPanel").style.display = "";
	document.getElementById("fieldsPanel").style.display = "";
	document.getElementById("screenContentList").style.display = "";
	document.getElementById("fields").style.display = "none";
	if (document.getElementById("tooltip"))
		document.getElementById("tooltip").style.display = "none";
	
	//hide any unwanted warnings
	for (var i=0;i<scr_fields.length;i++)
	{
		hideFieldWarning(document.getElementById(scr_fields[i].name));
	}
	
	var table = document.getElementById("screenContentTable");
	
	//empty table if already exist
	while (table.rows.length > 0)
		table.deleteRow(0);
	
	//try to sort data acording to custom screen function
	try {
		var sortingfunction = eval("scr" + scr.id + "SortMethod");
		if (sortingfunction)
			scr_data.sort(sortingfunction);
	}
	catch(e){}
	
	//rebuild records table
	for (var i=0;i<scr_data.length;i++)
	{
		var row = table.insertRow(-1);
		
		var cell = row.insertCell(-1);
		
		cell.className = "listItem";
		if (i == 0) cell.className += " topBorder";
		if (i == scr_data.length - 1) cell.className += " bottomBorder";
		cell.className += " sideBorder";
		
		var displayFunction = null;
		try{displayFunction = eval("scr" + scr.id + "RecordDisplayAs");}catch(e){}
		
		var displayStr = "רשומה " + (i+1);
		if (displayFunction)
		{
			displayStr = displayFunction(scr_data[i]);
		}
		
		cell.innerHTML = displayStr;
		cell.position = i;
		cell.onclick = function()
		{
			if (table.allowEdit)
			{
				if (recordChanged)
				{
					var pos = this.position;
					showDialogBox("yn", "הרשומה הנוכחית לא נשמרה, האם לשמור כעת?",
						function ()
						{
							if (saveScreenRecord())
							{
								selectScreenRecord(pos);
							}
						},
						function ()
						{
							selectScreenRecord(pos);
						}
					);
					return;
				}
				else
				{
					selectScreenRecord(this.position);
				}
			}
		}
		cell.style.cursor = "pointer";
		
		cell = row.insertCell(-1);
		cell.innerHTML = "&nbsp;&nbsp;<a id=\"screenContentDelete" + i + "\" class=\"\" href=\"#\" onclick=\"deleteScreenRecord(" + i + ")\"><img id=\"screenContentDeleteImg" + i + "\" class=\"imageLink multiDeleteButton\" src=\"images/wizard/delete.png\"/></a>";

	}
	
	disableEnableContentTable(true);
	
	if (scr_data.length > 0)
		table.style.display = "";
	else
		table.style.display = "none";
}

function drawTemplatesBox(field, items)
{
	var tableId = field.name;
	
	var table = document.getElementById(tableId);
	
	//empty table if already exist
	while (table.rows.length > 0)
		table.deleteRow(0);
	
	if (items.length == 0)
	{
		items[0] = null;
	}
	
	//rebuild records table
	for (var i=0;i<items.length;i++)
	{
		var row = table.insertRow(-1);
		
		var cell = row.insertCell(-1);
		
		cell.className = "templateListItem";
		
		if (items[i])
		{
			if (i == 0) cell.className += " templateTopBorder";
			if (i == items.length - 1) cell.className += " templateBottomBorder";
			cell.className += " templateSideBorder" + (items[i] == null ? " templateListItemEmpty" : "");
			cell.innerHTML = items[i];
		}
		else
		{
			//cell.innerHTML = (field.tip ? field.tip : "הרשימה ריקה");
			var img = document.createElement("img");
			img.src = "/images/wizard/banners/st_" + field.id + ".gif";
			cell.appendChild(img);
		}
		cell.position = i;
		
		if (items[i] != null)
		{
			cell.onclick = function()
			{
				selectTemplateRecord(tableId, this.position);
			}
			cell.ondblclick = function()
			{
				showSmartTemplatesDialog(tableId, this.position);
			}
			cell.style.cursor = "pointer";
		}
		
		cell = row.insertCell(-1);
		if (items[i])
			cell.innerHTML = "&nbsp;&nbsp;<a class=\"\" href=\"#\" onclick=\"showSmartTemplatesDialog('" + tableId + "'," + i + ")\"><img class=\"imageLink multiDeleteButton\" src=\"images/wizard/edit.png\"/></a>" +
						 	 "&nbsp;<a class=\"\" href=\"#\" onclick=\"deleteSmartTemplateRecord('" + tableId + "'," + i + ")\"><img class=\"imageLink multiDeleteButton\" src=\"images/wizard/delete.png\"/></a>";

	}
}

function drawNewTemplatesBox(field, items)
{
	document.getElementById("helpmewriteInline").style.display = "none";
	
	var tableId = field.name;
	
	var table = document.getElementById(tableId);
	
	//empty table if already exist
	while (table.rows.length > 0)
		table.deleteRow(0);
	
	if (items.length == 0)
	{
		items[0] = null;
	}
	
	//rebuild records table
	for (var i=0;i<items.length+1 || i < 3;i++)
	{
		var row = table.insertRow(-1);
		
		var cell = row.insertCell(-1);
		cell.innerHTML = "<li></li>";
		
		cell = row.insertCell(-1);
		
		var inp = document.createElement("input");
		cell.appendChild(inp);
		
		inp.className = "templateLongField";
		inp.id = field.name + "Line" + i;
		inp.pos = i;
		inp.fname = field.name;
		inp.onchange = function() {
			onFieldChange(this.fname);
		}
		inp.onfocus = function() {
			adjustHelpme(this.fname , this.pos);
		}
		inp.onkeydown = function() {
			var name = this.id.substring(0,this.id.indexOf("Line"));
			name = name + "Line" + (this.pos + 1);
			
			if (!document.getElementById(name))
			{
				var table = document.getElementById(this.fname);
				var row = table.insertRow(-1);
				
				var cell = row.insertCell(-1);
				cell.innerHTML = "<li></li>";
				
				cell = row.insertCell(-1);
				
				var inp = document.createElement("input");
				cell.appendChild(inp);
				
				inp.className = "templateLongField";
				inp.id = this.fname + "Line" + (this.pos+1);
				inp.pos = (this.pos + 1);
				inp.fname = this.fname;
				
				inp.onkeydown = this.onkeydown;
				inp.onfocus = this.onfocus;
				inp.onchange = this.onchange;
			}
		}
		
		inp.value = (i < items.length && items[i] != null ?  items[i] : "");
	}
}

function setFieldData(field, value)
{
	if (field.type == 3)
	{
		document.getElementById(field.name).value = value;
		document.getElementById(field.name + "Again").value = value;
	}
	else if (field.type == 4)
	{
		document.getElementById(field.name).value = value;
		if (document.getElementById(field.name).value != value)
		{
			var element = document.getElementById(field.name);
			element.options[element.options.length] = new Option(value, value);
			element.value = value;
		}
	}
	else if (field.type == 5)
	{
		var parts = value.split("/");
		
		if (parts.length > 1)
		{
			document.getElementById(field.name + "Year").value = parts[1];
			document.getElementById(field.name + "Month").value = parts[0];
		}
		else
		{
			document.getElementById(field.name + "Year").value = "";
			document.getElementById(field.name + "Month").value = "";
		}
	}
	else if (field.type == 10)
	{
		var parts = value.split("/");
		
		if (parts.length > 2)
		{
			document.getElementById(field.name + "Year").value = parts[2];
			document.getElementById(field.name + "Month").value = parts[1];
			document.getElementById(field.name + "Day").value = parts[0];
		}
		else
		{
			document.getElementById(field.name + "Year").value = "";
			document.getElementById(field.name + "Month").value = "";
			document.getElementById(field.name + "Day").value = "";
		}
	}
	else if (field.type == 12)
	{
		var records = value.split(";");
		var updated = 0;
		
		for (var i=0;i<records.length;i++)
		{
			var fields = records[i].split(",");
			if (fields.length > 1)
			{
				document.getElementById(field.name + i).value = fields[0];
				document.getElementById(field.name + "level" + i).value = fields[1];
				updated++;
			}
		}
		
		for (var i=updated;i<4;i++)
		{
			document.getElementById(field.name + i).value = "";
			document.getElementById(field.name + "level" + i).value = "";
		}
	}
	else if (field.type == 6)
	{
		if (value)
		{
			document.getElementById(field.name).value = value.split("\n");
			drawTemplatesBox(field, value.split("\n"));
		}
		else
		{
			document.getElementById(field.name).value = new Array();
			drawTemplatesBox(field, new Array());
		}
	}
	else if (field.type == 17)
	{
		if (value)
		{
			document.getElementById(field.name).value = value.split("\n");
			drawNewTemplatesBox(field, value.split("\n"));
		}
		else
		{
			document.getElementById(field.name).value = new Array();
			drawNewTemplatesBox(field, new Array());
		}
	}
	else if (field.type == 13)
	{
		var elem = document.getElementById('wizardForm').elements;
		for(var i = 0; i < elem.length; i++)
		{
			if (elem[i].name == field.name &&  elem[i].value == value)
			{
				elem[i].checked = true;
				break;
			}
		} 
	}	
	else
	{
		document.getElementById(field.name).value = value;
	}
	
	onFieldChange(field.name);
}

function getFieldData(field)
{
	if (field.type == 5)
	{
		if (document.getElementById(field.name + "Year").value != "" && document.getElementById(field.name + "Month").value != "")
			return document.getElementById(field.name + "Month").value + "/" + document.getElementById(field.name + "Year").value;
		else
			return "";
	}
	if (field.type == 10)
	{
		if (document.getElementById(field.name + "Year").value != "" && document.getElementById(field.name + "Month").value != "" && document.getElementById(field.name + "Day").value != "")
			return document.getElementById(field.name + "Day").value + "/" + document.getElementById(field.name + "Month").value + "/" + document.getElementById(field.name + "Year").value;
		else
			return "";
	}
	else if (field.type == 6)
	{
		return document.getElementById(field.name).value.join("\n");
	}
	else if (field.type == 12)
	{
		var result = "";
		
		for (var i=0;i<4;i++)
		{
			if (document.getElementById(field.name + i).value != "")
			{
				result += document.getElementById(field.name + i).value + "," + document.getElementById(field.name + "level" + i).value + ";";
			}
		}

		return result;
	}
	else if (field.type == 13)
	{
		var result = "";
		var elem = document.getElementById('wizardForm').elements;
		for(var i = 0; i < elem.length; i++)
		{
			if (elem[i].name == field.name && elem[i].checked)
			{
				result = elem[i].value;
				break;
			}
		} 
		return result;
	}
	else if (field.type == 17)
	{
		var result = new Array();
		
		var count = 0;
		while (document.getElementById(field.name + "Line" + count))
		{
			if (document.getElementById(field.name + "Line" + count).value != "")
			{
				result[result.length] = document.getElementById(field.name + "Line" + count).value;
			}
			count++;
		}
		
		return result.join('\n');
	}
	else
	{
		return document.getElementById(field.name).value;
	}
}

function clearField(field)
{
	
	if (field.type == 0)
		setFieldData(field, "-1");
	else if (field.type == 13)
		setFieldData(field, "false");
	else
		setFieldData(field, "");
}

function addScreenRecord()
{
	var table = document.getElementById("screenContentTable");
	if (table && table.allowEdit)
	{
		lastEditedRecord = -1;
		
		if (scr.multiRecord)
			initializeMultiRecordScreen();
		
		disableEnableContentTable(false);
		
		if (document.getElementById("fields"))
			document.getElementById("fields").style.display = "";
			
		var choseFocus = false;
		
		for (var i=0;i<scr_fields.length;i++)
		{
			clearField(scr_fields[i]);
			if (scr_fields[i].type != 0 && !choseFocus)
			{
				try{document.getElementById(scr_fields[i].name).focus();}catch(e){}
				choseFocus = true;
			}
		}
	}
}

function deleteScreenRecord(position)
{
	var table = document.getElementById("screenContentTable");
	if (table && table.allowEdit)
	{
		showDialogBox("yn", "אתה בטוח שברצונך למחוק את הרשומה?",
			function ()
			{
				lastEditedRecord = -1;
				
				scr_data.splice(position, 1);
				
				initializeMultiRecordScreen();
			},
			function ()
			{
			}
		);
	}
}

function validateFields(field)
{
	var result = true;
	for (var i=0;i<scr_fields.length;i++)
	{
		result = result && validateField(scr_fields[i]);
	}
	return result;
}

function fieldsToDataRecord()
{
	var record = {};

	if (document.getElementById("hiddenTemplate"))
	{
		record["hiddenTemplate"] = document.getElementById("hiddenTemplate").value;
	}
	
	for (var i=0;i<scr_fields.length;i++)
	{
		record[scr_fields[i].name] = getFieldData(scr_fields[i]);
	}
	
	return record;
}

function disableEnableContentTable(state)
{
	var table = document.getElementById("screenContentTable");
	if (table)
	{
		table.allowEdit = state;
		
		if (state)
		{
			for (var i=0;i<table.rows.length;i++)
			{
				var cell = table.rows[i].cells[0];
				cell.className = cell.className.replace(" disabledItem","");
				
				var delButton = document.getElementById("screenContentDeleteImg" + i);
				if (delButton)
				{
					delButton.src = "images/wizard/delete.png";
				}
			}
			
			var addButton = document.getElementById("screenContentAddRecordImg");
			if (addButton)
			{
				addButton.src = addButton.src.replace("record_dis.png", "record.png");
			}
		}
		else
		{
			for (var i=0;i<table.rows.length;i++)
			{
				var cell = table.rows[i].cells[0];
				cell.className = cell.className.replace(" disabledItem","");
				
				cell.className += " disabledItem";
				
				var delButton = document.getElementById("screenContentDeleteImg" + i);
				if (delButton)
				{
					delButton.src = "images/wizard/delete_dis.png";
				}
			}
			
			var addButton = document.getElementById("screenContentAddRecordImg");
			if (addButton)
			{
				addButton.src = addButton.src.replace("record.png", "record_dis.png");
			}
		}
	}
}

function saveScreenRecord()
{
	if (!validateFields())
		return false;
		
	for (var i=0;i<scr_fields.length;i++)
	{
		hideFieldWarning(document.getElementById(scr_fields[i].name));
	}
	
	if (lastEditedRecord < 0)
		scr_data[scr_data.length] = fieldsToDataRecord();
	else
		scr_data[lastEditedRecord] = fieldsToDataRecord();
	
	if (scr.multiRecord)
		initializeMultiRecordScreen();
	
	recordChanged = false;
	
	disableEnableContentTable(true);
	
	return true;
}

function selectScreenRecord(position)
{
	lastEditedRecord = position;
	
	if (document.getElementById("fields"))
		document.getElementById("fields").style.display = "";
	
	if (document.getElementById("tooltip"))
		document.getElementById("tooltip").style.display = "none";
	
	var table = document.getElementById("screenContentTable");
	if (table)
	{
		for (var i=0;i<table.rows.length;i++)
		{
			var cell = table.rows[i].cells[0];
			cell.className = cell.className.replace(" selectedItem","");
			
			if (i==position)
				cell.className += " selectedItem";
		}
		
		disableEnableContentTable(false);
	}
	
	for (var i=0;i<scr_fields.length;i++)
	{
		hideFieldWarning(document.getElementById(scr_fields[i].name));
		
		if (scr_data[position][scr_fields[i].name])
			setFieldData(scr_fields[i], scr_data[position][scr_fields[i].name]);
		else
			clearField(scr_fields[i]);
			
		var loadFunctioName = "onload_" + scr.id + "_" + scr_fields[i].name;
		var loadFunction = null;
		try{loadFunction = eval(loadFunctioName);}catch(e){}
		if (loadFunction)
		{
			loadFunction(scr_fields[i]);
		}
	}
	
	recordChanged = false;
}

function onload_1_password(field)
{
	if (scr_data[0].email != "")
	{
		document.getElementById(field.name).disabled = true;
		document.getElementById(field.name + "Again").disabled = true;
		
		document.getElementById(field.name).className = document.getElementById(field.name).className + " disabledElement";
		document.getElementById(field.name + "Again").className = document.getElementById(field.name + "Again").className + " disabledElement";
	}
	else
	{
		document.getElementById(field.name).disabled = false;
		document.getElementById(field.name + "Again").disabled = false;
		
		document.getElementById(field.name).className = document.getElementById(field.name).className.replace("disabledElement","");
		document.getElementById(field.name + "Again").className = document.getElementById(field.name + "Again").className.replace("disabledElement", "");
	}
}

var duringPreview = false;

function downloadSaveScreen(dir, preview)
{
	if (duringPreview)
		return;
	
	hideProgressBar();
	
	var img = document.createElement("img");
	img.id = "progressBar";
	img.className = "progressBar";
	img.src = "./images/wizard/finish/DownloadResume" + (!preview ? "_nopreview":"") + ".gif";
	
	document.getElementsByTagName("body")[0].appendChild(img);
	
	var container = document.getElementById("container");
	img.style.left = (getPosition(container).x + parseInt((container.offsetWidth - img.offsetWidth)/2,10)) + "px";
	img.style.top = (getPosition(container).y + parseInt((container.offsetHeight - img.offsetHeight)/2,10)) + "px";
	
	duringPreview = true;
	saveScreen(dir);
}

function hideProgressBar()
{
	if (document.getElementById("progressBar"))
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("progressBar"));
}

function gotoScreenIdx(nextScreen)
{
	saveScreen(nextScreen - screenIdx);
}

function saveScreen(dir)
{
	nextScreen = screenIdx + dir;
	if (!scr_data)
	{
		saveDone({"status":"ok"});
		return;
	}
	
	var overrideWarnings = !screenChanged;
	
	if (scr.multiRecord)
	{
		if (document.getElementById("fields").style.display == "")
		{
			/*if (recordChanged)
			{
				showDialogBox("yn", "הרשומה הנוכחית לא נשמרה, האם לשמור כעת?",
					function ()
					{
						if (saveScreenRecord())
						{
							doActualScreenSave(dir, false);
						}
					},
					function ()
					{
						doActualScreenSave(dir, false);
					}
				);
				return;
			}*/
			if (recordChanged)
			{
				if (!saveScreenRecord())
					return;
			}
		}
		
		//no need for save or validation because nothing was changed
		doActualScreenSave(dir, overrideWarnings);
	}
	else
	{
		if (saveScreenRecord())
		{
			doActualScreenSave(dir, overrideWarnings);
		}
	}
}

function doActualScreenSave(dir, overrideWarnings)
{
	validationErrors = new Array();
	var data = {};
	data.screenId = scr.id;
	data.data = scr_data;
	data.direction = dir;
	data.overrideWarnings = overrideWarnings;
	var postData = JSON.stringify(data);
	jsonRequest("saveData?screenId=" + data.screenId, saveDone, postData);
}

function displayValidationErrors(pos, dir)
{
	var theError = validationErrors[pos];
	
	if (theError.warning)
	{
		showDialogBox("yn", theError.text + "<br/>האם להמשיך בכל זאת?",
			function ()
			{
				if (pos == validationErrors.length -1)
				{
					doActualScreenSave(dir, true);
				}
				else
				{
					displayValidationErrors(pos+1, dir)
				}
			},
			function ()
			{
			}
		);
	}
	else
	{
		showDialogBox("ok", theError.text + "<br/>אנא תקן את הליקוי ונסה שוב.",
			function ()
			{
			},
			function ()
			{
			}
		);
	}
}

function saveDone(data)
{
	duringPreview = false;
	hideProgressBar();
	if (!data.status || data.status != "ok")
		alert("save error: " + data.status);
	else if (data.validationErrors.length > 0 && !data.overrideWarnings)
	{
		validationErrors = data.validationErrors;
		displayValidationErrors(0, data.direction);
	}
	else
		window.location.href = "wizard.do?screenIdx=" + nextScreen;
}

function getFieldByName(name)
{
	if (!name)
		return null;
	
	for (var i=0;i<scr_fields.length;i++)
	{
		if (scr_fields[i].name == name)
		{
			return scr_fields[i];
		}
	}

	var result = null;
	
	if (name.indexOf("Year") == name.length - 4)
	{
		name = name.substring(0,name.length - 4);
		result = getFieldByName(name);
	}
		
	return result;
}

function adjustHelpme(fieldName, pos)
{
	var element = document.getElementById(fieldName + "Line" + (pos > -1 ? pos : "") );
	if (element)
	{
		var helpme = document.getElementById("helpmewriteInline");
		
		helpme.fname = fieldName;
		helpme.pos = pos;
		helpme.style.display = "";
		helpme.style.left = (getPosition(element).x - helpme.offsetWidth - 5) + "px";
		helpme.style.top = (getPosition(element).y - 10 + (pos < 0?10:0) )+ "px";
	}
}

function adjustTooltipPosition(element, tooltip, anchorName, rightTooltip)
{
	//remove old class names
	tooltip.textElement.className = tooltip.textElement.className.replace("ttTextShort","").replace("ttTextLong","");

	//start with a narrow tooltip and expand if needed
	tooltip.textElement.className += " ttTextShort";
	
	if (tooltip.offsetHeight > 60)
	{
		tooltip.textElement.className = tooltip.textElement.className.replace("ttTextShort","");
		tooltip.textElement.className += " ttTextLong";
	}
	
	var div = document.getElementById(anchorName);
	if (rightTooltip)
	{
		tooltip.style.left = (getPosition(div).x + div.offsetWidth - 5) + "px";
		tooltip.style.top = (getPosition(div).y + 8)+ "px";
	}
	else
	{
		tooltip.style.left = (getPosition(div).x - tooltip.offsetWidth + 5) + "px";
		tooltip.style.top = getPosition(element).y + "px";
	}
}

function showTemplateTooltip(element, text)
{
	var tooltip = document.getElementById("tooltip");

	//if a warning tooltip was already created remove it
	if (tooltip)
		document.getElementsByTagName("body")[0].removeChild(tooltip);
		
	var prefix= "r";
	if (element.position % 5 == 0) prefix = "";
	
	tooltip = createActualTooltip(prefix);
	
	document.getElementsByTagName("body")[0].appendChild(tooltip);
	
	tooltip.style.display = "";
	
	tooltip.textElement.innerHTML =text;
	
	adjustTooltipPosition(element, tooltip, element.id, prefix == "r");
	
	tooltip.style.display = "";
}

function hideTemplateTooltip()
{
	var tooltip = document.getElementById("tooltip");

	//if a warning tooltip was already created remove it
	if (tooltip)
		document.getElementsByTagName("body")[0].removeChild(tooltip);
}

function showFieldTooltip(element)
{
	var tooltip = document.getElementById("tooltip");

	//if a warning tooltip was already created remove it
	if (tooltip)
		document.getElementsByTagName("body")[0].removeChild(tooltip);
	
	var field = getFieldByName(element.id);
	
	var anchor;
	
	//special case for smart template field
	if (field == null && element.id && element.id.substring(0,5) == "smart")
	{
		var theName = element.id.substring(5);
		field = getFieldByName(theName);
		anchor = element.id + "Line";
	}
	else
	{
		if (field != null)
			anchor = field.name + "Line";
	}
	
	if (field == null)
	{
		if (tooltip)
			tooltip.style.display = "none";
		return;
	}
	
	var prefix= "r";
	
	
	if (!field.rightTooltip)
		prefix="";
	
	//create a new tooltip element
	tooltip = createActualTooltip(prefix);
	
	document.getElementsByTagName("body")[0].appendChild(tooltip);
	
	tooltip.style.display = "";
	
	tooltip.textElement.innerHTML = field.tip;
	
	adjustTooltipPosition(element, tooltip, anchor, field.rightTooltip);
	
	tooltip.style.display = (field.tip != null && field.tip != "" ? "" : "none");
}

function createActualTooltip(prefix)
{
	var tooltip = document.createElement("DIV");
	tooltip.style.position = "absolute";
	tooltip.id = "tooltip";
	
	var table = document.createElement("TABLE");
	table.cellSpacing = "0px";
	
	var row = table.insertRow(-1);
	
	var cell = row.insertCell(-1);
	cell.className = prefix + "ttRightMiddle";
	var innerDiv = document.createElement("DIV");
	innerDiv.className = prefix + "ttRightTop";
	cell.appendChild(innerDiv);
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttCenterTop";
	cell.rowSpan = "2";
	tooltip.textElement = cell;
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttLeftMiddle";
	innerDiv = document.createElement("DIV");
	innerDiv.className = prefix + "ttLeftTop";
	cell.appendChild(innerDiv);
	
	row = table.insertRow(-1);
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttRightMiddle";
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttLeftMiddle";
	
	row = table.insertRow(-1);
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttRightBottom";
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttCenterBottom";
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttLeftBottom";
	
	tooltip.appendChild(table);
	
	return tooltip;
}

function createWarningTooltip(field, validationError)
{
	var element = document.getElementById(field.name);
	
	//if a warning tooltip was already created remove it
	if (element.warnTooltip)
		document.getElementsByTagName("body")[0].removeChild(element.warnTooltip);
	
	var prefix= "r";
	
	if (!field.rightTooltip)
		prefix="";
	
	//create a new tooltip element
	var tooltip = document.createElement("DIV");
	tooltip.style.position = "absolute";
	tooltip.id = element.id + "Warn";
	
	var table = document.createElement("TABLE");
	table.cellSpacing = "0px";
	
	var row = table.insertRow(-1);
	
	var cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnRightMiddle";
	var innerDiv = document.createElement("DIV");
	innerDiv.className = prefix + "ttWarnRightTop";
	cell.appendChild(innerDiv);
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnCenterTop";
	cell.rowSpan = "2";
	cell.innerHTML = validationError.text;
	tooltip.textElement = cell;
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnLeftMiddle";
	innerDiv = document.createElement("DIV");
	innerDiv.className = prefix + "ttWarnLeftTop";
	cell.appendChild(innerDiv);
	
	row = table.insertRow(-1);
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnRightMiddle";
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnLeftMiddle";
	
	row = table.insertRow(-1);
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnRightBottom";
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnCenterBottom";
	
	cell = row.insertCell(-1);
	cell.className = prefix + "ttWarnLeftBottom";
	
	tooltip.appendChild(table);
	
	element.warnTooltip = tooltip;
	
	document.getElementsByTagName("body")[0].appendChild(tooltip);
	
	//adjust tooltip position
	adjustTooltipPosition(element, tooltip, field.name + "Line", field.rightTooltip);
}



function validateField(field)
{
	//field = getFieldByName(element.id)
	
	if (field)
	{
		var value = getFieldData(field);
		if (field.mandatory && (value == null || value == ""))
		{	
			createWarningTooltip(field, FieldValidationError(0,"חובה להזין ערך בשדה זה",false));
			return false;
		}
		
		if (field.type == 2 && !validateEmail(value))
		{
			createWarningTooltip(field, FieldValidationError(0,"כתובת המייל שנתת אינה תקינה - יש להקפיד על כתובת מייל תקינה ונכונה שכן אל כתובת זו ישלחו קורות החיים בסיום התהליך.",false));
			return false;
		}
		
		if (field.type == 3)
		{
			var input1 = document.getElementById(field.name);
			var input2 = document.getElementById(field.name + "Again");
			
			if (input1.value != input2.value)
			{
				createWarningTooltip(field, FieldValidationError(0,"הסיסמה אינה זהה - יש להקפיד על כתיבת הסיסמה, פעמיים ובמדוייק.",false));
				return false;
			}
		}
		
		if (field.type == 11 && !validatePhone(value))
		{
			createWarningTooltip(field, FieldValidationError(0,"אנא הזן מספר טלפון בפורמט 03-1111111",false));
			return false;
		}
		
		if (field.type == 14 && !validateURL(value))
		{
			createWarningTooltip(field, FieldValidationError(0,"כתובת האתר אינה תקינה תחבירית - דבר היכול להקשות על הקורא .יש להקפיד על פורמט כדוגמא הבא: http://www.tazooz.co.il",true));
			return true;
		}
		
		if (field.type == 15 && !validatePositiveNumber(value))
		{
			createWarningTooltip(field, FieldValidationError(0,"המספר שגוי - יש להשתמש בספרות בלבד.",false));
			return false;
		}
		
		try
		{
			var validateFunction = eval("customFieldValidation" + field.id);
			var errMsg = validateFunction(value);
			if (errMsg != null)
			{
				createWarningTooltip(field, errMsg);
				return errMsg.isWarning;
			}
		}catch(e){}
	}
	
	hideFieldWarning(document.getElementById(field.name));

	return true;
}

function hideFieldWarning(element)
{
	if (element && element.warnTooltip)
	{
		document.getElementsByTagName("body")[0].removeChild(element.warnTooltip);
		element.warnTooltip = null;
	}
}

function onFieldFocus(element)
{
	hideFieldWarning(element);
	showFieldTooltip(element);
}

function onFieldLostFocus(element)
{
	var field = getFieldByName(element.id);
	if (field)
	{
		if (!(field.type == 3))
			validateField(field);
	}
	else
	{
		//might be dealing with a retype field
		if (element.id.length>4 && element.id.substring(element.id.length-5,element.id.length) == "Again")
		{
			field = getFieldByName(element.id.substring(0,element.id.length-5));
			if (field)
			{
				validateField(field);
			}
		}
	}
}

function onFieldChange(fieldName)
{
	recordChanged = true;
	screenChanged = true;
	var field = getFieldByName(fieldName)
	if (field)
	{
		if (field.type == 4)
		{
			if (getFieldData(field) == "אחר")
			{
				document.getElementById(field.name).value="";
				showOtherDialog(field);
			}
		}
		
		var changedFunction = null;
		try{changedFunction = eval("scr" + scr.id + "Field" + field.id + "Changed");}catch(e){}
	
		if (changedFunction)
		{
			changedFunction(field);
		}
	}
}

function onMotherFieldChange(fieldName, pos)
{
	var element = document.getElementById(fieldName + "mother" + pos);
	if (element)
	{
		if (element.checked)
		{
			document.getElementById(fieldName + "read" + pos).value = "";
			document.getElementById(fieldName + "write" + pos).value = "";
			document.getElementById(fieldName + "speek" + pos).value = "";
		}
	}
}

function onLangFieldChange(fieldName, pos)
{
	var element = document.getElementById(fieldName + "mother" + pos);
	if (element)
		element.checked = false;
		
	onFieldChange(fieldName);
}

function buildSmartTemplatesDialog(field, value)
{
	var mainDiv = document.createElement("DIV");
	
	mainDiv.className = "smartDialogContainer";
	mainDiv.id = "templatesDialog";
	
	//top window elements
	var headerLine = document.createElement("DIV");
	headerLine.className = "smartDialogTop";
	
	var div = document.createElement("DIV");
	div.className = "smartDialogClose";
	div.onclick = function()
	{
		closeTemplateDialog(mainDiv);
	}
	headerLine.appendChild(div);
	
	div = document.createElement("DIV");
	div.className = "smartDialogHeader";
	div.style.backgroundImage = "url(\"images/wizard/smartTemplates/scr" + scr.id + "_top.png\")";
	headerLine.appendChild(div);
	
	mainDiv.appendChild(headerLine);
	
	//text box label
	/*div = document.createElement("DIV");
	div.className = "smartDialogLabel";
	div.innerHTML = field.displayName;
	mainDiv.appendChild(div);*/
	
	//text box
	div = document.createElement("DIV");
	div.id = "smart" + field.name + "Line";
	div.className = "smartDialogTextLine";
	div.style.height = "2px";
	
	var inp = document.createElement("input");
	inp.className = "smartDialogTextBox";
	inp.value = value;
	inp.type = "hidden";
	inp.id = "smart" + field.name;
	div.appendChild(inp);
	mainDiv.textbox = inp;
	
	mainDiv.appendChild(div);
	
	//help me graphics
	div = document.createElement("DIV");
	div.className = "smartDialogHelpMe";
	
	//pro banner
	var banner = document.createElement("DIV");
	banner.className = "smartProBanner";
	banner.id = "smartProBanner";
	banner.style.display = "none";
	
	div.appendChild(banner);
	
	mainDiv.appendChild(div);
	
	//list header
	div = document.createElement("DIV");
	div.className = "smartDialogListHeader";
	div.innerHTML = "הצעות ודוגמאות";
	mainDiv.appendChild(div);	
	
	//actual list
	div = document.createElement("DIV");
	div.className = "smartDialogList";
	
	var listTable = document.createElement("TABLE");
	listTable.cellSpacing = "0px";
	listTable.style.width = "440px";
	listTable.id = "smartTemplatesTable";
	div.appendChild(listTable);
	
	mainDiv.appendChild(div);
	
	//bottom buttons
	var buttonLine = document.createElement("DIV");
	buttonLine.className = "smartDialogButtonsLine";
	
	div = document.createElement("a");
	div.className = "smartDialogCancelButton";
	div.href="javascript:void(0);";
	div.onclick = function()
	{
		closeTemplateDialog(mainDiv);
	}
	buttonLine.appendChild(div);

	div = document.createElement("a");
	div.className = "smartDialogOkButton";
	div.href="javascript:void(0);";
	div.onclick = function()
	{
		confirmTemplateUpdate(mainDiv);
	}
	buttonLine.appendChild(div);
	
	mainDiv.appendChild(buttonLine);
	
	//bottom decoration
	div = document.createElement("DIV");
	div.className = "smartDialogBottom";
	//var tip = document.createElement("IMG");
	//tip.src = "/images/wizard/smartTemplates/tip.png";
	//tip.className = "smartDialogBottomTip";
	//div.appendChild(tip);
	mainDiv.appendChild(div);
	
	return mainDiv;
}

function showSmartTemplatesDialog(fieldName, position)
{
	if (document.getElementById("templatesDialog"))
		return;
	
	var field = getFieldByName(fieldName);
	
	if (field)
	{
		var curValue = getFieldData(field).split('\n');
		
		if(!curValue) curValue = new Array();
		
		if (position < 0 || position > curValue.length - 1)
		{
			curValue = "";
		}
		else
		{
			curValue = curValue[position];
		}
	
		var dialog = buildSmartTemplatesDialog(field, curValue);
		dialog.position = position;
		dialog.field = field;
		
		hideSelectsForExplorer6();
		
		document.getElementsByTagName("body")[0].appendChild(dialog);

		//position the fialog correctly
		
		//right side is aligned with the fields
		/*var titlePanel = document.getElementById("titlePanel");
		dialog.style.left = (getPosition(titlePanel).x - dialog.offsetWidth - 8) + "px";
		
		//top is aligned with the tips box and some (more)
		var tips = document.getElementById("tipsUppper");
		dialog.style.top = (getPosition(tips).y + 2) + "px";*/
		
		var elementToPositionBy = document.getElementById(field.name + "Line" + (position > -1 ? position : "") );
		dialog.style.left = (getPosition(elementToPositionBy).x - 275) + "px";
		dialog.style.top = (getPosition(elementToPositionBy).y - dialog.offsetHeight - (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1 ? -18 : 25)) + "px";
		
		showFieldTooltip(dialog.textbox);
		
		//dialog.textbox.focus();
		
		loadSmartTemplatesData(field);
		
		//dialog.textbox.focus();
	}
}

function loadSmartTemplatesData(field)
{
	var data = {};
	
	if (field.special)
	{
		data.dependsOnValue = getFieldData(getFieldByName(field.special));
		data.screenId = scr.id;
	}
	var postData = JSON.stringify(data);

	jsonRequest("getData?screenId=100&fieldId=" + field.id, smartTemplatesDataLoaded, postData);
}

function smartTemplatesDataLoaded(data)
{
	if (data.exception)
	{
		if (data.exception.code == 200)
		{
			window.location.href = "login.do";
		}
		else
		{
			alert("detected error: " + data.exception.message);
			window.location.href = "/";
		}
	}
	else if (data.screen)
	{
		var templates = data.data;
		var listTable = document.getElementById("smartTemplatesTable");
	
		for (var i=0;i<templates.length;i++)
		{
			var row = listTable.insertRow(-1);
			var cell = row.insertCell(-1);
			
			cell.className = "smartListItem smartTopBorder smartSideBorder " + (i == templates.length-1 ? "smartBottomBorder" : "");
			
			cell.text = (scr_data_raw.gender == 2 ? templates[i].femaleText : templates[i].maleText);
			cell.innerHTML = cell.text;
			
			cell.onclick = function() 
			{
				var table = document.getElementById("smartTemplatesTable");
				
				for (var i=0;i<table.rows.length;i++)
				{
					var ucell = table.rows[i].cells[0];
					ucell.className = ucell.className.replace(" smartSelectedItem","");
				}
				
				this.className = this.className.replace("smartListItem","smartListItem smartSelectedItem");
				
				document.getElementById("templatesDialog").textbox.value = this.text;
			};
			
			cell.ondblclick = function() 
			{
				document.getElementById("templatesDialog").textbox.value = this.text;
				confirmTemplateUpdate(document.getElementById("templatesDialog"));
			};
		}
		
		if (data.moreForPro)
		{
			document.getElementById("smartProBanner").style.display = "";
		}
	}
	//document.getElementById("templatesDialog").textbox.focus();
}

function checkEnter(event, doButton)
{
	if ( (event && event.which ? event.which : event.keyCode) == 13 )
		document.getElementById(doButton).onclick();
}

function buildWorkEnvironmentsDialog(field, value)
{
	var mainDiv = document.createElement("DIV");
	
	mainDiv.className = "workEnvContainer";
	mainDiv.id = "workEnvDialog";
	
	//top window elements
	var headerLine = document.createElement("DIV");
	headerLine.className = "workEnvDialogTop";
	
	var div = document.createElement("DIV");
	div.className = "workEnvDialogClose";
	div.onclick = function()
	{
		document.getElementsByTagName("body")[0].removeChild(mainDiv);
		
	}
	headerLine.appendChild(div);
	
	div = document.createElement("DIV");
	div.className = "workEnvDialogHeader";
	headerLine.appendChild(div);
	
	mainDiv.appendChild(headerLine);
	
	//text box label
	div = document.createElement("DIV");
	div.className = "workEnvDialogLabel";
	div.innerHTML = field.displayName;
	mainDiv.appendChild(div);
	
	
	var categoryContainer = document.createElement("DIV");
	categoryContainer.className = "workEnvCategoryContainer";
	
	//selected box
	div = document.createElement("DIV");
	div.className = "workEnvSelectedContainer";
	
	var innerDiv = document.createElement("DIV");
	innerDiv.className = "workEnvSelectedLabel";
	innerDiv.innerHTML = "נבחרו";
	div.appendChild(innerDiv);
	
	innerDiv = document.createElement("DIV");
	innerDiv.className = "workEnvSelectedContent";
	
	var innerInnerDiv = document.createElement("DIV");
	innerInnerDiv.className = "workEnvSelectedContentInner";
	innerInnerDiv.id = "workEnvSelectedContentInner";
	
	table = document.createElement("table");
	table.id = "workEnvSelectedTable";
	
	innerInnerDiv.appendChild(table);
	innerDiv.appendChild(innerInnerDiv);
	div.appendChild(innerDiv);
	
	categoryContainer.appendChild(div);
	
	//actions
	div = document.createElement("DIV");
	div.className = "workEnvActionsContainer";
	
	var actionButton = document.createElement("a");
	actionButton.href="javascript:void(0);";
	actionButton.className = "arrowLeftButton";
	actionButton.onclick = function() 
	{
		var selectedCell = document.getElementById("workEnvOptionsTable").selectedCell;
		
		if (selectedCell)
		{
			var table = document.getElementById("workEnvSelectedTable");
			
			table.selectedCell = null;
			selectedCell.className = "workEnvCell";
			
			mainDiv.addSelected(selectedCell.text);
		}
	}
	div.appendChild(actionButton);
	
	actionButton = document.createElement("a");
	actionButton.href="javascript:void(0);";
	actionButton.className = "arrowRightButton";
	actionButton.onclick = function() 
	{
		var selectedCell = document.getElementById("workEnvSelectedTable").selectedCell;
		
		if (selectedCell)
		{
			var tr = selectedCell.parentNode;
			
			if (tr)
			{
				while (tr.hasChildNodes())
         			tr.removeChild(tr.lastChild);
         		
         		if (tr.parentNode)
         			tr.parentNode.removeChild(tr); 
			}
		}
	}
	div.appendChild(actionButton);
	
	categoryContainer.appendChild(div);
	
	
	//options box
	div = document.createElement("DIV");
	div.className = "workEnvOptionsContainer";
	
	var innerDiv = document.createElement("DIV");
	innerDiv.className = "workEnvOptionsLabel";
	innerDiv.innerHTML = "קטגוריות";
	
	div.appendChild(innerDiv);
	
	innerDiv = document.createElement("DIV");
	innerDiv.className = "workEnvOptionsContent";
	
	innerInnerDiv = document.createElement("DIV");
	innerInnerDiv.className = "workEnvOptionsContentInner";
	innerInnerDiv.id = "workEnvOptionsContentInner";
	
	table = document.createElement("table");
	table.id = "workEnvOptionsTable";
	
	innerInnerDiv.appendChild(table);
	innerDiv.appendChild(innerInnerDiv);
	
	div.appendChild(innerDiv);
	
	innerDiv = document.createElement("DIV");
	innerDiv.className = "workEnvDialogOtherContainer";
	var otherLabel = document.createElement("DIV");
	otherLabel.innerHTML = "אחר:";
	innerDiv.appendChild(otherLabel);
	
	var otherInput = document.createElement("input");
	otherInput.type = "text";
	otherInput.id = "workEnvDialogOtherText";
	otherInput.onkeypress = function()
	{
  		if(!e) var e=window.event;
  		if((e.keyCode ? e.keyCode : e.which)==13) document.getElementById("workEnvDialogOtherAdd").onclick();
	}
	
	innerDiv.appendChild(otherInput)
	
	var addButton = document.createElement("a");
	addButton.href = "javascript:void(0);";
	addButton.className = "workEnvDialogOtherAdd";
	addButton.id = "workEnvDialogOtherAdd";
	addButton.onclick = function()
	{
		var text  = otherInput.value;
		
		mainDiv.addSelected(text);
		
		otherInput.value = "";
		otherInput.focus();
	}
	
	innerDiv.appendChild(addButton)
	
	div.appendChild(innerDiv);
	
	categoryContainer.appendChild(div);
	
	mainDiv.appendChild(categoryContainer);
	
	
	//bottom buttons
	var buttonLine = document.createElement("DIV");
	buttonLine.className = "workEnvDialogButtonsLine";
	
	div = document.createElement("a");
	div.className = "workEnvDialogOkButton";
	div.href="javascript:void(0);";
	div.onclick = function()
	{
		var value = "";
		var table = document.getElementById("workEnvSelectedTable");
		for (var i=0;i<table.rows.length;i++)
		{
			value += table.rows[i].cells[0].text + (i<table.rows.length-1 ? "," : "");
		}
		
		setFieldData(mainDiv.field, value);
		document.getElementsByTagName("body")[0].removeChild(mainDiv);
	}
	buttonLine.appendChild(div);
	
	mainDiv.appendChild(buttonLine);
	
	//bottom decoration
	div = document.createElement("DIV");
	div.className = "workEnvDialogBottom";
	mainDiv.appendChild(div);
	mainDiv.addSelected = function (text)
	{
		//if (text.length > 17)
		//	text = text.substring(0,17);
			
		var table = document.getElementById("workEnvSelectedTable");
		
		for (var i=0;i<table.rows.length;i++)
		{
			if (table.rows[i].cells[0].text && table.rows[i].cells[0].text == text)
			{
				otherInput.value = "";
				otherInput.focus();
				return;
			}
		}
			
		var row  = table.insertRow(-1);
		var cell = row.insertCell(-1);
			
		cell.className = "workEnvCell";
		cell.innerHTML = text;
		cell.text = text;
		if (!hasHebrew(cell.text))
			cell.style.direction = "ltr";
		cell.onclick = function() 
		{
			var curCell = table.selectedCell;
			if (curCell)
				curCell.className = "workEnvCell"
				
			this.className = "workEnvCellSelected";
			table.selectedCell = this;
		};
	}
	
	return mainDiv;
}


function showWorkEnvironmentsDialog(fieldName)
{
	recordChanged = true;
	screenChanged = true;
	var field = getFieldByName(fieldName);
	
	if (field)
	{
		if (!document.getElementById("workEnvDialog"))
		{
			var curValue = document.getElementById(fieldName).value; //todo: get data?
			
			if(!curValue) curValue = "";
		
			var dialog = buildWorkEnvironmentsDialog(field, curValue);
			dialog.field = field;
			
			document.getElementsByTagName("body")[0].appendChild(dialog);
			
			var curList = curValue.split(",");
			for (var i=0;i<curList.length;i++)
				if (curList[i] != "")
					dialog.addSelected(curList[i]);
			
			//position the dialog correctly
			
			//right side is aligned with the fields
			var titlePanel = document.getElementById("titlePanel");
			dialog.style.left = (getPosition(titlePanel).x - dialog.offsetWidth - 28) + "px";
			
			//top is aligned with the tips box and some (more)
			//var tips = document.getElementById("tipsUppper");
			
			dialog.style.top = (getPosition(document.getElementById(field.name)).y - dialog.offsetHeight - 20) + "px";
			
			loadWorkEnvData();
		}
		
		document.getElementById("workEnvDialogOtherText").focus();
	}
}

function loadWorkEnvData()
{
	jsonRequest("getData?screenId=101", workEnvDataLoaded);
}

function workEnvDataLoaded(data)
{
	if (data.exception)
	{
		if (data.exception.code == 200)
		{
			window.location.href = "/login.do";
		}
		else
		{
			alert("detected error: " + data.exception.message);
			window.location.href = "/";
		}
	}
	else if (data.screen)
	{
		var envs = data.data;
		var listTable = document.getElementById("workEnvOptionsTable");
	
		var curCategory = "";
		for (var i=0;i<envs.length;i++)
		{
			var row;
			var cell;
			
			if (curCategory != envs[i].category)
			{
				row = listTable.insertRow(-1);
				cell = row.insertCell(-1);
				cell.className = "workEnvCategoryCell";
				cell.innerHTML = envs[i].category;
				if (!hasHebrew(envs[i].category))
					cell.style.direction = "ltr";
				curCategory = envs[i].category;
			}
			
			row = listTable.insertRow(-1);
			cell = row.insertCell(-1);
			
			cell.className = "workEnvCell";
			cell.innerHTML = envs[i].text;
			cell.text = envs[i].text;
			if (!hasHebrew(cell.text))
				cell.style.direction = "ltr";
			cell.onclick = function() 
			{
				var curCell = listTable.selectedCell;
				if (curCell)
					curCell.className = "workEnvCell"
					
				this.className = "workEnvCellSelected";
				listTable.selectedCell = this;
			};
			
			cell.ondblclick = function()
			{
				clearSelection();
				
				var curCell = listTable.selectedCell;
				if (curCell)
					curCell.className = "workEnvCell"
					
				this.className = "workEnvCellSelected";
				listTable.selectedCell = this;
				
				document.getElementById("workEnvDialog").addSelected(this.text);
			}
		}
	}
}

function buildTemplatesPreviewDialog(template)
{
	var mainDiv = document.createElement("DIV");
	
	mainDiv.className = "templatePreviewDialogContainer";
	mainDiv.id = "templatePreviewDialog";
	
	//top window elements
	var headerLine = document.createElement("DIV");
	headerLine.className = "templatePreviewDialogTop";
	
	var div = document.createElement("DIV");
	div.className = "templatePreviewDialogClose";
	div.onclick = function()
	{
		closeTemplateDialog(mainDiv);
	}
	headerLine.appendChild(div);
	
	var div = document.createElement("DIV");
	div.className = "templatePreviewDialogHeader";
	headerLine.appendChild(div);

	mainDiv.appendChild(headerLine);
	
	//actual template preview
	div = document.createElement("DIV");
	div.className = "templatePreviewPicture";
	div.style.backgroundImage = "url(\"images/wizard/templates/T" + template.id + "_big.png\")";
	mainDiv.appendChild(div);
	
	//right hand text area
	div = document.createElement("DIV");
	div.className = "templatePreviewTextContent";
	
	var title = document.createElement("DIV");
	title.className = "templatePreviewTitle";
	title.innerHTML = template.name;
	div.appendChild(title);
	
	if (template.pro)
	{
		var pro = document.createElement("DIV");
		pro.className = "templatePreviewPro";
		
		var proImage = document.createElement("IMG");
		proImage.src = "images/wizard/templates/pro.png"
		pro.appendChild(proImage);
		
		div.appendChild(pro);
	}
	
	var text = document.createElement("DIV");
	text.className = "templatePreviewText";
	text.innerHTML = template.text;
	div.appendChild(text);
	
	mainDiv.appendChild(div);
	
	
	if (!scr_data_raw.isProUser && template.pro)
	{
		div = document.createElement("DIV");
		var upgrade = document.createElement("a");
		upgrade.href = "product_pro.do";
		upgrade.target = "_blank";
		upgrade.className = "templatePreviewUpgrade";
		div.appendChild(upgrade);
		mainDiv.appendChild(div);
	}
	else
	{
		div = document.createElement("DIV");
		div.className = "templatePreviewBanner";
		var banner = document.createElement("IMG");
		banner.src = "images/wizard/templates/templates_banner.png";
		div.appendChild(banner);
		mainDiv.appendChild(div);
	}
	
	//bottom decoration
	div = document.createElement("DIV");
	div.className = "templatePreviewDialogBottom";
	mainDiv.appendChild(div);
	
	return mainDiv;
}

function showTemplatesPreviewDialog(templateId)
{
	//var templateId = document.getElementById("template").value;
	
	var template = null;
	
	for (var i=0;i<scr_data_raw.templates.length;i++)
	{
		if (templateId == scr_data_raw.templates[i].id)
		{
			template = scr_data_raw.templates[i];
			break;
		}
	}
	
	if (template)
	{
		var dialog = buildTemplatesPreviewDialog(template);
	
		document.getElementsByTagName("body")[0].appendChild(dialog);
		
		//position the fialog correctly
		
		//right side is aligned with the fields
		var titlePanel = document.getElementById("titlePanel");
		dialog.style.left = (getPosition(titlePanel).x + parseInt((titlePanel.offsetWidth - dialog.offsetWidth)/2,10) ) + "px";
		
		//top is aligned with the tips box and some (more)
		//var tips = document.getElementById("tipsUppper");
		//dialog.style.top = (getPosition(tips).y + 2) + "px";
		
		var scrollTop = 0;
		if (document.documentElement && !document.documentElement.scrollTop)
			scrollTop = 0;
		else if (document.documentElement && document.documentElement.scrollTop)
			scrollTop = parseInt(document.documentElement.scrollTop,10);
		else if (document.body && document.body.scrollTop)
			scrollTop = parseInt(document.body.scrollTop,10);

		dialog.style.top  = (scrollTop + 20) +'px';
	}
}


function deleteSmartTemplateRecord(fieldName, position)
{
	recordChanged = true;
	screenChanged = true;
	var field = getFieldByName(fieldName);
	
	if (field)
	{
		var curValue = document.getElementById(fieldName).value;
		
		if(!curValue) curValue = new Array();
		
		if (position >= 0 && position < curValue.length )
		{
			curValue.splice(position,1);
		}
	
		setFieldData(field, curValue.join("\n"));
	}
}

function selectTemplateRecord(tableId, position)
{
	var table = document.getElementById(tableId);
	
	for (var i=0;i<table.rows.length;i++)
	{
		var cell = table.rows[i].cells[0];
		cell.className = cell.className.replace(" selectedItem","");
		
		if (i==position)
			cell.className += " selectedItem";
	}
}

function confirmTemplateUpdate(dialog)
{
	if (dialog.field)
	{
		recordChanged = true;
		screenChanged = true;
		
		if (dialog.field.type == 18)
		{
			setFieldData(dialog.field, dialog.textbox.value);
			document.getElementById(dialog.field.name).focus();
		}
		else
		{
			var value = getFieldData(dialog.field).split('\n');
			
			if (!value)
				value = new Array();
			
			if (dialog.textbox.value)
			{
				if (dialog.position > -1)
				{
					value[dialog.position] = dialog.textbox.value;
				}
				else
				{
					value[value.length] = dialog.textbox.value;
				}
			}
			
			setFieldData(dialog.field, value.join("\n"));
			document.getElementById(dialog.field.name + "Line" + dialog.position).focus();
		}
	}
	
	closeTemplateDialog(dialog);
}

function closeTemplateDialog(dialog)
{
	showSelectsForExplorer6();
	
	var tooltip = document.getElementById("tooltip");
	if (tooltip)
		document.getElementsByTagName("body")[0].removeChild(tooltip);
	
	document.getElementsByTagName("body")[0].removeChild(dialog);
	
	if (dialog.field.type == 18)
		document.getElementById(dialog.field.name).focus();
	else
		document.getElementById(dialog.field.name + "Line" + dialog.position).focus();
}

function scr3RecordDisplayAs(data)
{
	return data.companyName + " " + data.startDate + " - " + data.endDate;
}

function scr5RecordDisplayAs(data)
{
	return data.institution + (data.startYear && data.endYear ? " - " + data.startYear + " - " + data.endYear : "");
}

function scr6RecordDisplayAs(data)
{
	return (data.institute ? data.institute + " - " : "") + data.course;
}

function hideDialogBox()
{
	var current = document.getElementById("basicDialog");
	if (current)
	{
		document.getElementsByTagName("body")[0].removeChild(current);
	}
	showSelectsForExplorer6();
}

function showDialogBox(type, text, okCallback, cancelCallback)
{
	hideDialogBox();
	
	var mainDiv = document.createElement("DIV");
	mainDiv.id = "basicDialog";
	mainDiv.className = "basicDialog";
	
	var div = document.createElement("DIV");
	div.className = "basicDialogTop";
	mainDiv.appendChild(div);
	
	div = document.createElement("DIV");
	div.className = "basicDialogMid";
	div.innerHTML = text;
	mainDiv.appendChild(div);
	
	div = document.createElement("DIV");
	
	if (type == "ok")
	{
		div.className = "basicDialogButton";
		var button = document.createElement("A");
		button.className = "basicDialogSingleOkButton";
		button.href = "javascript: void(0);";
		button.onclick = function()
		{
			hideDialogBox();
			if (okCallback)
				okCallback();
		}
		div.appendChild(button);
	}
	else if (type == "okcancel")
	{
		div.className = "basicDialogButtons";
		var button = document.createElement("A");
		button.className = "basicDialogOkButton";
		button.href = "javascript: void(0);";
		button.onclick = function()
		{
			hideDialogBox();
			if (okCallback)
				okCallback();
		}
		div.appendChild(button);
		
		button = document.createElement("A");
		button.className = "basicDialogCancelButton";
		button.href = "javascript: void(0);";
		button.onclick = function()
		{
			hideDialogBox();
			if (cancelCallback)
				cancelCallback();
		}
		div.appendChild(button);
	}
	else //(yn)
	{
		div.className = "basicDialogButtons";
		var button = document.createElement("A");
		button.className = "basicDialogYesButton";
		button.href = "javascript: void(0);";
		button.onclick = function()
		{
			hideDialogBox();
			if (okCallback)
				okCallback();
		}
		div.appendChild(button);
		
		button = document.createElement("A");
		button.className = "basicDialogNoButton";
		button.href = "javascript: void(0);";
		button.onclick = function()
		{
			hideDialogBox();
			if (cancelCallback)
				cancelCallback();
		}
		div.appendChild(button);
	}
	mainDiv.appendChild(div);
	
	div = document.createElement("DIV");
	div.className = "basicDialogBottom";
	mainDiv.appendChild(div);
	
	hideSelectsForExplorer6();
	document.getElementsByTagName("body")[0].appendChild(mainDiv);
	
	var container = document.getElementById("container");
	mainDiv.style.left = (getPosition(container).x + parseInt((container.offsetWidth - mainDiv.offsetWidth)/2,10)) + "px";
	
	var scrollTop = 0;
	if (document.documentElement && !document.documentElement.scrollTop)
		scrollTop = document.body.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)
		scrollTop = parseInt(document.documentElement.scrollTop,10);
	else if (document.body && document.body.scrollTop)
		scrollTop = parseInt(document.body.scrollTop,10);
	
	var clientHeight = parseInt(window.innerHeight || document.documentElement.clientHeight,10);

	mainDiv.style.top = (parseInt(clientHeight/2 - mainDiv.offsetHeight/2,10) + scrollTop) + "px";
}

function hideOtherDialog()
{
	showSelectsForExplorer6();
	
	var mainDiv = document.getElementById("otherDialog");
	if (mainDiv)
		document.getElementsByTagName("body")[0].removeChild(mainDiv);
}

function showSelectsForExplorer6()
{
	if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
	{
		var selectElements = document.getElementsByTagName("select");
		for (var i=0;i<selectElements.length;i++)
		{
			selectElements[i].style.display = "";
		}
	}
}

function showOtherDialog(field)
{
	var mainDiv = document.createElement("DIV");
	mainDiv.id = "otherDialog";
	mainDiv.className = "otherDialog";
	mainDiv.field = field;
	
	var div = document.createElement("DIV");
	div.className = "otherDialogTop";
	
	
	var head = document.createElement("DIV");
	head.className = "otherDialogClose";
	head.onclick = function()
	{
		setFieldData(field, "");
		hideOtherDialog();
	}
	div.appendChild(head);
	
	head = document.createElement("DIV");
	head.className = "otherDialogHeader";
	div.appendChild(head);

	mainDiv.appendChild(div);
	
	div = document.createElement("DIV");
	div.className = "otherDialogMid";
	
	var spn = document.createElement("DIV");
	spn.innerHTML = field.displayName;
	div.appendChild(spn);
	
	var inp = document.createElement("input");
	inp.type = "text";
	inp.className = "otherDialogTextbox";
	mainDiv.textbox = inp;
	div.appendChild(inp);
	
	mainDiv.appendChild(div);
	
	var buttons = document.createElement("DIV");
	buttons.className = "otherDialogButtons";
	
	var button = document.createElement("A");
	button.className = "basicDialogOkButton";
	button.href = "javascript: void(0);";
	button.onclick = function()
	{
		if (mainDiv.textbox.value != "")
		{
			setFieldData(field, mainDiv.textbox.value);
		}
		hideOtherDialog();
	}
	buttons.appendChild(button);
	
	button = document.createElement("A");
	button.className = "basicDialogCancelButton";
	button.href = "javascript: void(0);";
	button.onclick = function()
	{
		setFieldData(field, "");
		hideOtherDialog();
	}
	buttons.appendChild(button);
	
	mainDiv.appendChild(buttons);

	
	div = document.createElement("DIV");
	div.className = "otherDialogBottom";
	mainDiv.appendChild(div);
	
	hideSelectsForExplorer6();
	
	document.getElementsByTagName("body")[0].appendChild(mainDiv);
	
	var container = document.getElementById("container");
	mainDiv.style.left = (getPosition(container).x + parseInt((container.offsetWidth - mainDiv.offsetWidth)/2,10)) + "px";
	mainDiv.style.top = (getPosition(container).y + parseInt((container.offsetHeight - mainDiv.offsetHeight)/2,10)) + "px";
	mainDiv.textbox.focus();
}

function hideSelectsForExplorer6()
{
	if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
	{
		var selectElements = document.getElementsByTagName("select");
		for (var i=0;i<selectElements.length;i++)
		{
			selectElements[i].style.display = "none";
		}
	}
}

function hasHebrew(text)
{
	for (var i=0;i<text.length;i++)
		if (text.substring(i,i+1) >= 'א' && text.substring(i,i+1) <= 'ת')
			return true;
	
	return false;
}

function selectProduct(productId)
{
	document.getElementById("chosenProductId").value = productId;
	document.getElementById("mainFrm").submit();
}