changeFormStyleTarget = null;
var radioImgs = [],
	checkImgs = [];

function changeRadioCheckImg(){
	for ( i = 0; i < radioImgs.length; i++) {
		path =  radioImgs[i].src;
		radioImgs[i].src = THEME_IMG_ROOT + path.substring(path.indexOf("images"), path.length);
	}

	for ( i = 0; i < checkImgs.length; i++) {
		path =  checkImgs[i].src;
		checkImgs[i].src = THEME_IMG_ROOT + path.substring(path.indexOf("images"), path.length);
	}
}

	function replaceButton( buttonNode )
	{
		if( buttonNode.isGenerated )return;

		var configOptions = makeAttrsKeyValue(buttonNode.attributes );
		configOptions["minWidth"] = parseInt( configOptions["minwidth"]);
		configOptions["height"]=20;
		configOptions["text"] = configOptions["value"];
		if( configOptions["text"] == null )
			configOptions["text"] = buttonNode.innerText;
		if( configOptions["minWidth"] == null  ) configOptions["minWidth"] = 80;
		var mySpan =  document.createElement("SPAN");
		buttonNode.parentNode.insertBefore( mySpan, buttonNode );
		configOptions["renderTo"] = mySpan;
	    var newButton = new Ext.Button( configOptions );
		buttonNode.style.display = "none";
		buttonNode._ext_object = newButton;
		buttonNode.isGenerated = true;

		var buttons = mySpan.getElementsByTagName("BUTTON");
		if( buttons.length != 0 )buttons[0].isGenerated = true;

		if( buttonNode.onclick != null )
		{
			newButton.purgeListeners();
			newButton.addListener( "click", buttonNode.onclick, buttonNode, false );
		}
	return newButton;
	}

function changeFormStyle(el, text, select, radio, checkbox, ta, bool_, button) {

	var selects,
		inputs,
		radios = [],
		checkboxes = [],
		hovers = [],
		buttons = [],
		textFields = [],
		selectText = Framework.Lang.pleaseSelect,
		selectInfo ,
		target = el;
		bool = false;
 		if(bool != null) bool = bool_;
		if (text) replaceTextFields();
		if (checkbox) replaceCheckboxes();
		if (ta) replaceTextAreas();
		if (radio) replaceRadios();
		if (select) replaceSelects();
		if (button) replaceButtons();

	changeFormStyleTarget = el;

	function replaceButtons(){
		var inputs = target.getElementsByTagName('input');
		for(var i=0; i < inputs.length; i++){
			if (inputs[i].type == 'button' && inputs[i].getAttribute("nice") != 'true')
				replaceButton( inputs[i] );
		}

		var buttons = target.getElementsByTagName('button');
		for(var i=0; i < buttons.length; i++)
			replaceButton( buttons[i] );
	}




	function replaceTextFields(){
		var inputs = target.getElementsByTagName('input');
		var j = 0;
		for(var i=0; i < inputs.length; i++) {
			if(inputs[i].type == "text" || inputs[i].type == "password" )
			{
					if( inputs[i].isGenerated )continue;

					var configOptions = makeAttrsKeyValue(inputs[i].attributes );
					var className = 'x-form-text x-form-field ';
					if(inputs[i].className)
						className += inputs[i].className; 
					configOptions["height"] = "";
					var ExtFormObj = Ext.form.TextField;
					switch( configOptions.vtype )
					{
						case "num":{
							 ExtFormObj = Ext.form.NumberField; configOptions.vtype = "";
							 configOptions[ "maxValue" ] = parseInt(configOptions.maxvalue);
							  break;
						}
						case "date":ExtFormObj = Ext.form.DateField; configOptions.vtype = "";  break;
						default:
						{
							configOptions[ "allowBlank" ] = parseBool(configOptions.allowblank);
							if( configOptions[ "allowBlank" ] == null ) configOptions[ "allowBlank" ] = true;
							configOptions[ "emptyText" ] = configOptions.emptytext;
						}
					}
					configOptions[ "allowDecimals" ] = parseBool(configOptions.allowdecimals);
					if( configOptions[ "allowDecimals" ] == null ) configOptions[ "allowDecimals" ] = true;
					
					configOptions[ "width" ] = ( inputs[i].style.width != null ? inputs[i].style.width : 150 );
					
					var alpha = new ExtFormObj( configOptions );
					alpha.applyToMarkup( inputs[i] ); 
					inputs[i]._ext_object = alpha;
					inputs[i].isGenerated = true;
					inputs[i].className=className;
			}
		}
	}



	function replaceTextAreas(){

		var ta = target.getElementsByTagName('textarea');
		var j = 0;
		for(var i=0; i < ta.length; i++) {

			var configOptions = makeAttrsKeyValue(ta[i].attributes );
			if (configOptions["width"]){
				configOptions["width"] = parseInt(configOptions["width"]);
			}
			configOptions[ "allowBlank" ] = parseBool(configOptions.allowblank);
		    var textarea = new Ext.form.TextArea(configOptions);
		    textarea.applyToMarkup(ta[i]);
			ta[i]._ext_object = textarea;
			ta[i].className="x-form-textarea x-form-field " + ta[i].className;
		}

	}


	function replaceRadios() {
		var inputs = target.getElementsByTagName('input');
		var j = 0;
		for(var i=0; i < inputs.length; i++) {
			if(inputs[i].type=='radio') {
				radios[j] = inputs[i];
				radios[j].changeValue = radioClick;
				radios[j].radioDisabled = radioDisabled;
				++j;
			}
		}

		for(var i=0; i <radios.length; i++) {
			radios[i].style.display = "none";
			var radioImg = document.createElement('span');
			radioImg.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;";
			
			if(radios[i].checked) 
				radioImg.className = "checkbox icons16x16_1 niceform_radio_checked "+ radios[i].className;
			else radioImg.className = "checkbox icons16x16_1 niceform_radio " + radios[i].className;
			
			if (radios[i].disabled && radios[i].checked) 
				radioImg.className = "checkbox icons16x16_1 niceform_radio_checked_disabled ";
			else if(radios[i].disabled) 
				radioImg.className = "checkbox icons16x16_1 niceform_radio_disabled ";
			
			
			radioImg.id = 'myRadio_'+i;

			radioImg.onclick = checkRadio;

			if (radios[i].nextSibling)
				 radios[i].parentNode.insertBefore(radioImg, radios[i].nextSibling);
			else 	radios[i].parentNode.appendChild(radioImg);

			radioImgs[radioImgs.length] = radioImg;

			radios[i].setCheck = function(bool_){
				this.checked = bool_;
				this.nextSibling.className = "checkbox icons16x16_1 " + ((bool_) ? " niceform_radio_checked " : "niceform_radio") ;
			}

		}
	}
	function replaceCheckboxes() {
		var inputs = target.getElementsByTagName('input');

		var j = 0;
		for (var i2=0; i2 < inputs.length; i2++) {
			if(inputs[i2].type == 'checkbox' && inputs[i2].getAttribute("nice") != "true") {
				checkboxes[j] = inputs[i2];
				checkboxes[j].changeValue = checkboxClick;
				checkboxes[j].changeDisabled = checkboxDisabled;
				inputs[i2].setAttribute("nice", "true");
				++j;
			}
		}

		for(var i2=0; i2 < checkboxes.length; i2++) {
			checkboxes[i2].style.display="none";
			var checkboxImg = document.createElement('span');
			checkboxImg.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;";
			
			if(checkboxes[i2].checked) 
				checkboxImg.className = "checkbox icons16x16_1 niceform_checkbox_checked ";
			else 
				checkboxImg.className = "checkbox icons16x16_1 niceform_checkbox " ;
			
			
			if (checkboxes[i2].disabled && checkboxes[i2].checked) 
				checkboxImg.className = "checkbox icons16x16_1 niceform_checkbox_checked_disabled ";
				
			else if(checkboxes[i2].disabled) 
				checkboxImg.className = "checkbox icons16x16_1 niceform_checkbox_disabled ";
			
			checkboxImg.id = 'myCheck_'+i2;
			checkboxes[i2].val = 'myCheck_'+i2;
			Ext.EventManager.addListener(checkboxImg, "mousedown", checkCheck);

			if (checkboxes[i2].nextSibling)
				 checkboxes[i2].parentNode.insertBefore(checkboxImg, checkboxes[i2].nextSibling);
			else checkboxes[i2].parentNode.appendChild(checkboxImg);

			checkImgs[checkImgs.length] = checkboxImg;

		}
	}
	function checkboxDisabled(boolType){

		if(this.disabled != boolType){
			var img = this.nextSibling;
			if(boolType){
				img.className = "checkbox icons16x16_1 " + ((this.checked) ? "niceform_checkbox_checked_disabled" : "niceform_checkbox_disabled");
			} else{
				img.className = "checkbox icons16x16_1 " + ((this.checked) ? "niceform_checkbox_checked" : "niceform_checkbox") ;
			}
			this.disabled = boolType;
		}
	}

	function radioDisabled(boolType){

		if(this.disabled != boolType){
			var img = this.nextSibling;
			if(boolType){
				img.className = "checkbox icons16x16_1 " + ((this.checked) ? "niceform_radio_checked_disabled" : "niceform_radio_disabled");
			} else{
				img.className = "checkbox icons16x16_1 " + ((this.checked) ? "niceform_radio_checked" : "niceform_radio");
			}
			this.disabled = boolType;
		}
	}
	
	function radioClick(boolType){
		this.checked = boolType;
		var img = this.nextSibling;
		
		for (i = 0; i < radios.length; i++){
	  		if(radios[i].checked) {
	  			if(radios[i].disabled)$id("myRadio_"+i).className =  "checkbox icons16x16_1 niceform_radio_checked_disabled ";
				else $id("myRadio_"+i).className = "checkbox icons16x16_1 niceform_radio_checked ";
			 } else {
			 	if(radios[i].disabled)$id("myRadio_"+i).className =  "checkbox icons16x16_1 niceform_radio_disabled ";
				else $id("myRadio_"+i).className = "checkbox icons16x16_1 niceform_radio ";
			}
		}
	}

	function checkboxClick(boolType){
		if(this.checked != boolType){

			this.checked = boolType;
			img = this.nextSibling;
			if(boolType){
				if(this.disabled) img.className = "checkbox icons16x16_1 niceform_checkbox_checked_disabled" ;
				else img.className = "checkbox icons16x16_1 niceform_checkbox_checked" ;
			}
			else {
				if(this.disabled)img.className = "checkbox icons16x16_1 niceform_checkbox_disabled" ;
				else img.className = "checkbox icons16x16_1 niceform_checkbox" ;
			}
		}
	}

	function checkRadio(g) {
		id = this.id.substring(this.id.indexOf("_")+1, this.id.length);
	    var g = parseInt(id);
	    if(radios[g].disabled) return;
		
		radios[g].checked = true;
		if(radios[g].onchange)	radios[g].onchange();
		if(radios[g].onclick)radios[g].onclick();
	 	
		

	    
	  
	    for (i = 0; i < radios.length; i++){
	  		if(radios[i].checked && radios[i].nextSibling) {
	  			if(radios[i].disabled) radios[i].nextSibling.className = "checkbox icons16x16_1 niceform_radio_checked_disabled ";
				radios[i].nextSibling.className = "checkbox icons16x16_1 niceform_radio_checked ";
			 } else if(radios[i].nextSibling){
			 	if(radios[i].disabled) radios[i].nextSibling.className = "checkbox icons16x16_1 niceform_radio_disabled ";
				radios[i].nextSibling.className ="checkbox icons16x16_1 niceform_radio ";
			}
		}
	}

	function checkCheck(event) {
		var obj = event.target || event.srcElement;
	    id = obj.id.substring(obj.id.indexOf("_")+1, obj.id.length);
	    var g = parseInt(id);
	   if(checkboxes[g].disabled) return;

	   if(checkboxes[g].checked) checkboxes[g].checked = false;
	   else checkboxes[g].checked = true;

		if(checkboxes[g].onchange){
			checkboxes[g].onchange();
		}
		if(checkboxes[g].onclick){
			checkboxes[g].onclick();
		}
	  	if(checkboxes[g].checked) {
			obj.className = "checkbox icons16x16_1 niceform_checkbox_checked ";
		} else {
			obj.className = "checkbox icons16x16_1 niceform_checkbox ";
		}
		event.cancelBubble = true;
	}

function replaceSelects() {
   var  selects = target.getElementsByTagName('select');

   var converted;

   for( var i=0; i < selects.length; i++) {
	
		var isEditable = selects[i].getAttribute("editable");
		if(isEditable == null)isEditable = false;
		var emptyText = selects[i].getAttribute("emptyText");
		if(emptyText == null)emptyText = Framework.Lang.select;
		var forceSelection = selects[i].getAttribute("forceSelection");
		if(forceSelection == null)forceSelection = true;
		else forceSelection = parseBool(forceSelection);
		var width = parseInt(selects[i].getAttribute("width"));
		if(width == null || isNaN(width)){
			width = 147;
			if(isVisible(selects[i])){
				width += 17;
			}
		}
				
		var listWidth = parseInt(selects[i].getAttribute("listWidth"));
		if (!listWidth || isNaN(listWidth)){
			listWidth = width;
		}
			
		var id = selects[i].id;
		var onchangeHandler = selects[i].onchange;
		var onExpandHandler = selects[i].getAttribute("onExpand");
		var onCollapseHandler = selects[i].getAttribute("onCollapse");

		var conf  = { 
			listWidth: listWidth,
			width: width,
			maxHeight:150,
		    typeAhead: true,
		    triggerAction: 'all',
		    transform:selects[i],
			blankText: selects[i].getAttribute("blankText"),
			editable: true,
			disabled: parseBool(selects[i].getAttribute("disabled")),
			emptyText: emptyText,
			blankText: emptyText,
		    forceSelection:forceSelection,
		    deferredRender: true  
		}
		converted = new Ext.form.ComboBox(conf);
		
		if(Browser.isIE)
			converted.el.dom.nextSibling.style.top = '0px';
		
		if( onchangeHandler != null ){
			//converted.purgeListeners();
			converted.addListener( "select", onchangeHandler, converted, false );
		}

		onchangeHandler = null;
		if( onExpandHandler != null ){
			converted.onExpandHandler = onExpandHandler;
			converted.on( 'expand', comboExpand, this, { single: false });
		}
		onExpandHandler = null;
		if( onCollapseHandler != null ){
			converted.onCollapseHandler = onCollapseHandler;
			converted.on( 'collapse', comboCollapse, this, { single: false });
		}
		onCollapseHandler = null;
		
		//var cont = converted.container.dom.childNodes[0];
		var cont = converted.el.dom.parentNode;
		if( id != null )cont.id = id;
		cont._ext_object = converted;
		i--;
   }
}
function comboExpand(comboBox){
	eval( comboBox.onExpandHandler + "()" );
}
function comboCollapse(comboBox){
	eval( comboBox.onCollapseHandler + "()" );
}
function changeComboValue(value,boolValue){
	var divs = this.parentNode.getElementsByTagName("div");
	var textDiv,text;
	for(var i =0; i < divs.length; i++){
		if(this.val == divs[i].id){
			textDiv = divs[i];break;
		}
	}

	for(var k = 0; k < this.options.length; k++) {
		if(this.options[k].value == value) {
			this.options[k].selected = "selected";
			text = this.options[k].text;
		}
		else {
			this.options[k].selected = "";
		}
	}

	if(boolValue == null){
		if(this.onchange) this.onchange();
	}

	if(text && text.length > Math.floor(textDiv.offsetWidth/9)){
			text = text.substring(0,Math.floor(textDiv.offsetWidth/9))+'...';
	}

	var newText = document.createTextNode(text);
	if(!text || text == "") newText = document.createTextNode( Framework.Lang.emptyTestName );
	textDiv.replaceChild(newText, textDiv.childNodes[0]);

}

function removeOptionDiv(){

	var elems = target.getElementsByTagName("div");
		for(var k = 0; k < elems.length; k++){
			if(elems[k].id && elems[k].id.substring(0,10) == "optionsDiv")
				{target.removeChild(elems[k]);}
		}
}

function showOptions(ev) {

		var  selects = target.getElementsByTagName('select');

		var x,y;
		var i = parseInt(this.value);
		var width = selectInfo[i].width;
		var bool = selectInfo[i].bool;
		var t = target;
		var popUp = target;

		if(!bool){
			while(t.parentNode.nodeName != "body"){
				var id = "";
				if(t.id) id = t.id.substring(t.id.length - 4,t.id.length);

				if(t.nodeName == "DIV" && id == "-dlg"){
					popUp = $id(t.id);
					break;
				}else{
					t = t.parentNode;
				}
			}
		}

		if(!bool){
				x = findPosX(selects[i].nextSibling) -  findPosX(popUp) - 1;
		 		y = findPosY(selects[i].nextSibling)- findPosY(popUp) + 50;
		}else if(bool){
				x= findPosX(selects[i].parentNode) - findPosX(target) - 1;
				y = findPosY(selects[i].parentNode) - findPosY(target) +36 ;
		}

		var elem = null;
		if(!bool) target = popUp;
		var elems = target.getElementsByTagName("div");
		for(var k = 0; k < elems.length; k++){
			if(elems[k].id && elems[k].id.substring(0,10) == "optionsDiv" && elems[k].id != "optionsDiv"+i)
				{target.removeChild(elems[k]);}
		}
		for(var k = 0; k < elems.length; k++){
			if(elems[k].id == "optionsDiv"+i)
				{elem = elems[k];break;}
		}

		if(!elem || elem.className=="optionsDivInvisible") {

			if(elem)target.removeChild(elem);
			var optionsDiv = document.createElement('div');
			optionsDiv.style.minWidth = (width - 27 ) + "px";
			if(Browser.isIE) optionsDiv.style.width = (width - 27 ) + "px";

			optionsDiv.className = "optionsDivVisible";
			optionsDiv.id = "optionsDiv"+i;
			optionsDiv.style.left = x +"px";
			optionsDiv.style.top = y + "px";

			var textVar;
		for(var j=0; j < selects[i].options.length; j++) {
			var optionHolder = document.createElement('p');
			var optionLink = document.createElement('span');
			optionLink.className = "outOver";
			optionLink.value = j;
			optionLink.onclick = selectMe;
			optionLink.onmouseover = styleOver;
			optionLink.onmouseout = styleOut;
			var optionTxt = document.createTextNode(selects[i].options[j].text);
			if(!selects[i].options[j].text || selects[i].options[j].value == "" )
				optionTxt = document.createTextNode( Framework.Lang.emptyTestName );
			optionLink.appendChild(optionTxt);
			optionHolder.appendChild(optionLink);
			optionsDiv.appendChild(optionHolder);
		}

		if(!bool){
			popUp.appendChild(optionsDiv);
			var allowHeight = parseInt(popUp.offsetHeight) - parseInt(optionsDiv.style.top);
			if(parseInt(optionsDiv.offsetHeight) >= allowHeight){
				optionsDiv.style.height = (allowHeight-10)+"px";
				if(Ext.isSafari)optionsDiv.style.overflow = "auto";
				else optionsDiv.style.overflowY = "auto";
			}

		}else target.appendChild(optionsDiv);



		}
		else if(elem.className=="optionsDivVisible") {
			elem.className = "optionsDivInvisible";
		}
		ev.cancelBubble = true;
		return false;
}
changeFormStyle.prototype.showOptions = showOptions;

function styleOver(ev){
	this.className = "hover";
}

function styleOut(ev){
	this.className = "outOver";
}

function 	selectMe(ev) {
	var  selects = target.getElementsByTagName('select');
	var linkNo = parseInt(this.value);
	var selectNo = parseInt(this.parentNode.parentNode.id.substring(10));
	var selectField = selects[selectNo];


	for(var k = 0; k < selectField.options.length; k++) {
		if(k==linkNo) {
			selectField.options[k].selected = "selected";
		}
		else {
			selectField.options[k].selected = "";
		}
	}

	var textVar;
	var t = selects[selectNo].parentNode.getElementsByTagName("div");
	for(var k = 0; k < t.length; k++){
		if(t[k].id == "mySelectText"+selectNo)
			{textVar = t[k];break;}
	}

	var text = selects[selectNo].options[selects[selectNo].selectedIndex].text;
	if(!text) text = Framework.Lang.emptyTestName;
		if(text.length > Math.floor(textVar.offsetWidth/9)){
			text = text.substring(0,Math.floor(textVar.offsetWidth/9))+'...';
		}

	var newText = document.createTextNode(text);
	textVar.replaceChild(newText, textVar.childNodes[0]);
	this.parentNode.parentNode.className = "optionsDivInvisible";

	if(selectField.getAttribute("onchange"))
		selectField.onchange();
	else if(selectField.onchange)
		selectField.onchange();
}



}

function removeOptionsDiv(){
	if(!$id("common-dlg")) return;

	var target = $id("common-dlg");

	var elems = target.getElementsByTagName("div");

	for(var k = 0; k < elems.length; k++){
		if(elems[k].id && elems[k].id.substring(0,10) == "optionsDiv"){
			elems[k].parentNode.removeChild(elems[k]);k=k-1;
		}
	}

	target = $id("tag-cloud-dlg");
	elems = target.getElementsByTagName("div");
		for(var k = 0; k < elems.length; k++){
			if(elems[k].id && elems[k].id.substring(0,10) == "optionsDiv"){
				elems[k].parentNode.removeChild(elems[k]);k=k-1;
			}
		}
	target = $id("signUp-dlg");
	if( target != null )
	{
		elems = target.getElementsByTagName("div");
			for(var k = 0; k < elems.length; k++){
				if(elems[k].id && elems[k].id.substring(0,10) == "optionsDiv"){
					elems[k].parentNode.removeChild(elems[k]);k=k-1;
				}
			}
	}

}
function createNiceButton(value, float_, callback, style){
	/*var width = "40px";
	if(value.length >= 4)
		width = value.length*10 + 20 + "px";
	if(typeof callback=='undefined'){
		callback=null;
	}
	if (style==null) style="";
	return "<table onclick='"+callback+"' style='float:"+float_+";margin-right:12px;width:"+width+ ";"+style+"' cellspacing=0 cellpadding=0 onmouseover='this.className=\"x-btn-over\"' onmouseout='this.className=\"\"'><tbody>"+
		   		"<tr>"+
					"<td class=x-btn-left></td>"+
				   	"<td class='x-btn-center text'>"+value+"</td>"+
				   	"<td class=x-btn-right></td>"+
		   		"</tr>"+
		   "</tbody></table>";*/
		  
	return '<input type="button" value="' + value + '" style="width:70px" minwidth="70px" onclick="' + callback + '" />';
}

function createNiceButtonObj(value, float_, style){
	var table = document.createElement("table");
	if(value.length < 4)
	width = "40px";
	else
	width = value.length*10 +  20  + "px";
	if(style==null) style="";
	table.setAttribute("style", "float:"+float_+";width:"+width+";"+style);
	table.cellSpacing = "0";
	table.cellPadding = "0";
	table.onmouseover = "this.className='x-btn-over'";
	table.onmouseout = "this.className=''";
	var tr = table.insertRow(0);
	var td0 = tr.insertCell(0);
	var td1 = tr.insertCell(1);
	var td2 = tr.insertCell(2);
	td0.className = "x-btn-left";
	td1.className = "x-btn-center text";
	td2.className = "x-btn-right";
	td1.innerHTML = value;
	table.setText = function(val){
	    this.rows[0].cells[1].innerHTML = val;
		var wd = "40px";
		if(val.length >= 4)
			 wd = (val.length*10 +5)+ "px";
	    this.setAttribute("style", "float:"+float_+";width:"+wd);
 	}
 	table.setDisabled = function(bool_){
 		if(bool_) {
 			this.setAttribute("disabled","true");
 			this.rows[0].cells[1].style.color = "gray";
 		} else {
 			this.setAttribute("disabled","false");
 			this.rows[0].cells[1].style.color = "black";
 		}
 	}
	return table;
}



function makeAttrsKeyValue( attrs )
{
	var newArray = {};
	for( var i=0; i<attrs.length; i++ )
		newArray[ attrs[i].nodeName ] = attrs[i].nodeValue;
	return newArray;
}
