﻿function renderWarningControl(control)
{
  //This is not a complete control, just that all control types can have error states
  //returns the chunk of html for the error control.
  
  var html = '<div ';
	if ( control.warnlevel == 1)
	{
	    html += 'class="warn1 warning ui-icon-info ui-icon"><div class="hidden warningtext">'+ control.warntext +'</div></div>';
	} else if (control.warnlevel == 2)
	{
	   html += 'class="warn2 warning ui-icon-alert ui-icon" ><div class="hidden warningtext">'+ control.warntext +'</div></div>';
	} else
	{
	   html += 'class="warning" >  </div>';
	}
	//html +='>  </div>';
  return html;
}

//Blueprint controls are prefixed with ctl - as the numeric only id 
//is not suitable for making into the html id on its own.
function renderTextedit(control) {
	//Render a text edit control, and return it's html.
	var html = '<div id="ctl' + control.id + '" class="bpControl textEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);
	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '" />';
	}
	html += '  </div>';
	if ((control.multiline != undefined) && (control.multiline == 'true'))
	{
	    //New multiline control
		html += '  <textarea class="value" ';
	    if (control.enabled == "false")
	    {
	      html += ' disabled="true" ';
        }	
        html += '>'+control.value+'</textarea> '
	}
	else
	{
	    html += '  <input class="value" type="textbox" value="' + control.value + '" ';
	    if (control.enabled == "false")
	    {
	      html += ' disabled="true" ';
        }	
        html += '> '
    }
	html += '<span class="units">' + control.units + '</span>';
	html += '</div>\n';
	return html;
}

function renderDivider(control) {
	//Render a text edit control, and return it's html.
	var html = '<div id="ctl' + control.id + '" class="bpControl divider '+addInvisible(control)+'">\n';
	if ((control.graphic != undefined) && (control.graphic != ''))
	{ 
	    if ((control.css != undefined) && (control.css != ''))
	    {
	        html += '  <img style="'+ control.css +'" src="/image.aspx?image='+control.graphic+'\"/>';
	    }
	    else
	    {
	        html += '  <img src="/image.aspx?image='+control.graphic+'\"/>';
	    }
	    
	}
	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '"/>';
	}
	html += '  </div>';
	//html += '  <input class="value" type="textbox" value="' + control.value + '">';
	//html += '<span class="units">' + control.units + '</span>';
	html += '</div>\n';
	return html;
}

function renderDropDownList (control) {
	//Render a dropdown. You must pick an item from the list.
	var html = '<div id="ctl' + control.id + '" class="bpControl dropdownEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);

	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '">';
	}
	html += '  </div>';
	html += '<select class="value selectbox"';
	if (control.enabled == "false")
	{
	  html += ' disabled="true" ';
    }	
	html += '>\n';
	for (var itemCounter = 0; itemCounter < control.items.length; itemCounter++) {
		html += '<option value="' + control.items[itemCounter] + '" ';
		if (control.items[itemCounter] == control.value)
		{
		    html += 'selected="Selected" ';
		}
		html += '>' + control.items[itemCounter] +'</option>\n';
	}
	html += '</select>\n</div>';
	return html;
}

function renderDropDown(control) {
	//Render a dropdown. You must pick an item from the list.
	var html = '<div id="ctl' + control.id + '" class="bpControl dropdownlistEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);
	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '">';
	}
	html += '  </div>';	
	
	html += '<select class="selectbox"';
	if (control.enabled == "false")
	{
	  html += ' disabled="true" ';
    }	
	html += '>\n';
	for (var itemCounter = 0; itemCounter < control.items.length; itemCounter++) {
		html += '<option value="' + control.items[itemCounter] + '" ';
		if (control.items[itemCounter] == control.value)
		{
		    html += 'selected="Selected" ';
		}
		html += '>' + control.items[itemCounter] +'</option>\n';
	}
	html += '</select>\n';
	html += '<input type="text" class="dropdownedit value" value="';
	html += control.value + '" '
	if (control.enabled == "false")
	{
	  html += ' disabled="true" ';
    }
	html += ' /></div>';
	return html;
}

function renderOther(control) {
	//Generic render of a control - to be replaced by specific ones.
	var html = '<div id="ctl' + control.id + '" class="bpControl otherEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);

	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption +'  '+ control.type +'</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '">';
	}
	html += '  </div>';
	html += '  <span value class="value">' + control.value + '</span>';
	html += '<span class="units">' + control.units + '</span>';
	html += '</div>\n';
	return html;
}

function renderCheckbox(control) {
	//Generic render of a control - to be replaced by specific ones.
	var html = '<div id="ctl' + control.id + '" class="bpControl checkboxEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);
	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '">';
	}
	html += '  </div>';
	html += '  <input type="checkbox" class="value" '+addDisabledAttr(control);
	if (control.value == "true")
	{
	   html += ' checked="true"';
	}
	html +=' />';
	html += '<span class="units">' + control.units + '</span>';
	html += '</div>\n';
	return html;
}



function renderNumberedit(control) {
	//Render a text edit control, and return it's html.
	var html = '<div id="ctl' + control.id + '" class="bpControl numberEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);
	html += '  <div '+addIndentStyle(control)+'class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '<span class="max">'+control.max+'</span>';
	html += '<span class="min">'+control.min+'</span>';
	html += '<span class="origValue">'+control.value+'</span>';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '">';
	}
	html += '  </div>';
	html += '  <input class="value" type="textbox"'+ addDisabledAttr(control)+' value="' + control.value + '">';
	html += '<span class="units">' + control.units + '</span>';
	html += '</div>\n';
	return html;
}

function renderDateedit(control) {
	//Render a text edit control, and return it's html.
	var html = '<div id="ctl' + control.id + '" class="bpControl dateEdit '+addInvisible(control)+'">\n';
	html += renderWarningControl(control);
	html += '  <div '+addIndentStyle(control)+' class="label'+addDisabledClass(control)+'">';
	html += control.caption + '</div>\n';
	html += '  <div class="qty">';
	if (control.hasqty == 'true') {
		html += '  <input class="quantity" type="textbox" '+addDisabledAttr(control)+' value="' + control.quantity + '">';
	}
	html += '  </div>';
	html += '  <input class="value" type="textbox" value="' + control.value + '">';
	html += '<span class="units">' + control.units + '</span>';
	html += '</div>\n';
	return html;
}

function addDisabledClass(control)
{
  if (control.enabled == "false") 
  {
      return " disabledText ";
  }
  else
  {
      return "";
  }
}

function addDisabledAttr(control)
{
  if (control.enabled == "false") 
  {
      return ' disabled="true" ';
  }
  else
  {
      return "";
  }
}

function addIndentStyle(control)
{
    if ((control.indent != NaN) && (control.indent != undefined))
    {
        var indent = parseInt(control.indent) + 25; 
        return ' style="margin-left:'+indent+'px;" ';
    }
    else
    {
        return ' ';
    }
}

function addInvisible(control)
{
    try
    {
        if(control.visible.toLowerCase() == "false")
        {
            return 'invisible';
        }
        else
        {
            return '';
        }
    }
    catch(e)
    {
        return 'invisible';
    }
}
      

