function editonmouseover( div )
{
	div.oldClassName=div.className;
	div.className  = div.className + " mouseOver";
}

function editonmouseout( div )
{
	div.className= div.oldClassName;
}

function canceledit( obj )
{
	var div = obj;
	
	div = div.parentNode.parentNode.parentNode.parentNode;

	div.innerHTML= div.oldInnerHTML;
	restoreedit( div );
}


function restoreedit( div )
{
	div.className= div.oldClassName;
	div.style.height = div.oldHeight;
	div.onclick = div.oldOnclick;
	div.onmouseover = div.oldonmouseover;
	div.onmouseout = div.oldonmouseout;

	if ($('fieldhanger')!=null)
		div.removeChild($('fieldhanger'));

	if (div.getAttribute('item')=="cost" && div.innerHTML.substring(0,1)!='$')
		div.innerHTML='$ '+float2moeda(div.innerHTML);
}


function editdiv( div )
{
	if ($(div.id+'_input')) return;
		
	var div_date=div.getAttribute('date');
	var div_navigate=div.getAttribute('navigate');
	var url_ajax=div.getAttribute('editable');
	var div_type=div.getAttribute('type');
	var predictive=div.getAttribute('predictive');	

	var txtdate=''; if(div_date==1) txtdate=' date="1" ';
	var txtnavigate=''; if(div_navigate==1) txtnavigate=' navigate="1" ';

	if(navigator.appName=="Microsoft Internet Explorer")
		div.style.width=div.offsetWidth;

	div.oldInnerHTML = div.innerHTML;
	div.oldHeight = div.style.height;
	div.oldOnclick = div.onclick;
	div.oldonmouseover = div.onmouseover;
	div.oldonmouseout = div.onmouseout;
	
		var height=div.offsetHeight;
		var width=div.offsetWidth;

	div.onclick = function() {};
	div.innerHTML='';
	div.className='inputclear';
	div.onmouseover = '';
	div.onmouseout = '';
	if( height < 24 ) height = 24;
	width += 30;

	if (div_type=='text')
	{
		var txtvalue='<input type="text" style=" width: '+width+';  height: '+height+';" name="value" id="'+div.id+'_input" value="'+div.oldInnerHTML+'" '+txtdate+txtnavigate+' />';
	}
	else if (div_type=='textarea')
	{
		var txtvalue='<textarea style=" width: '+width+'px;  height: '+height+';"  name="value" id="'+div.id+'_input">'+div.oldInnerHTML+'</textarea>';
	}	
	else if (div_type=='select')
	{
			var txtvalue='<input type="text" style=" width: '+width+';  height: '+height+';" name="value" id="'+div.id+'_input" value="'+div.oldInnerHTML+'" predictive="'+predictive+'"  allownew=0  usevalue="1" '+txtdate+txtnavigate+'/>';
	}
	div.innerHTML='<form name="'+div.id+'_form" id="'+div.id+'_form" action="'+url_ajax+'" method=POST errfloat="right"><div style="margin-top: 4px;">'+txtvalue+'</div><div style="margin:0px;overflow:hidden;height:4px"></div><input type="submit" value="SAVE" style="width: 70px;"/><input type="button" value="CANCEL" style="width: 70px;" onmousedown="canceledit(this);"/></form>';
	div.style.height= '';
		/*
		var input = document.createElement('input');  
		input.setAttribute('type', div_type); 
		input.setAttribute('value', a);  
		input.setAttribute('class', 'input'+div_type);  
		input.setAttribute('name', div.id+'_input');  
		input.setAttribute('id', div.id+'_input'); 

		div.appendChild(input); 
		input.style.width=div.clientWidth-input.offsetLeft*2;
		input.style.height=div.clientHeight;
		*/
		//input.style.marginLeft= '-'+input.offsetLeft;
		//input.style.marginTop= '-'+input.offsetTop;
		//input.style.paddingTop= input.style.paddingBottom= 5;
		//input.style.paddingLeft=input.style.paddingRight= 8;
			
		//inputbackground($(div.id+'_input'));

	resultform( null, div );
	$(div.id+'_input').select();
}

function editsizecolor( div , idBrand, idProduct, type)
{
	if ($('div_edit'+type)!=null) return;
	var url_ajax=div.getAttribute('editable');
	var div_type=div.getAttribute('type');
	
	if (type=='color') var ttype='Color';
	else var ttype='Size';

	var oldinner = div.innerHTML;
	olddata= oldinner.split(', ');

	if(oldinner=='')
		olddata=[];

	div.old_backgroundColor= div.style.backgroundColor;
	div.old_innerHTML= div.innerHTML;
	div.old_height= div.style.height;
	div.old_padding= div.style.padding;
	div.oldOnclick = div.onclick;
	div.oldonmouseover = div.onmouseover;
	div.oldonmouseout = div.onmouseout;
	div.oldoldClassName= div.oldClassName;

	div.onclick = function() {};
	div.innerHTML='';
	div.onmouseover = '';
	div.onmouseout = '';
	
	div.className = '';
	div.style.backgroundColor='#FFF';
	div.style.padding=0;
	div.style.height='auto';
	
	//<div class="plus" onclick="more(\''+type+'\');" style="margin-left: 10px; float: left;">&nbsp;</div>';

	div.innerHTML='<div id="div_edit'+type+'"><div id="aux_'+type+'" style="display: none;"></div><div id="div_'+type+'"></div><form  errfloat="right"><div style="display: table;"><div style="float: left;"><input type="text" style="width: 270px;" id="add'+type+'" name="add'+type+'" predictive="DB/service.php?action=showBrand'+ttype+'&firstrec=0&numrec=0&idBrand='+idBrand+'" allownew=1 onchange="more(\''+type+'\');">&nbsp</div><input type="button" onclick="more(\''+type+'\');" style="float: right;" value="ADD"/></div><div style="padding-top: 4px;"><input type="button" value="SAVE" style="width: 70px;" onclick="save_sizecolor(\''+type+'\', \''+idProduct+'\',\''+div.id+'\');"/><input type="button" value="CANCEL" style="width: 70px;" onmousedown="cancel_edit(\''+div.id+'\',1);"/></div></form></div>';
	formformat($('div_edit'+type));

	var divnew = $('div_'+type);
	divnew.opt=olddata;
	for( var i = 0 ; i < divnew.opt.length ; i++ )
	{
		var a = ce( 'A' );
		a.className = 'item_sizecolor';
		a.innerHTML=divnew.opt[i];
		a.i=i;
		a.type=type;
		a.onclick= function(){ remove(this, type); }
		divnew.appendChild(a);

		if (i<divnew.opt.length-1)
		{
			var span = ce( 'SPAN' );
			span.innerHTML=", ";
			span.className = 'item_sizecolor';
			divnew.appendChild(span);
		}
	}
	divnew.style.paddingTop=5;
	divnew.style.paddingBottom=5;
	divnew.style.width=300;
	div.style.height='auto';
	divnew.style.display='block';
}
/*
function save_sizecolor( type , idProduct)
{
	var divnew = $('div_'+type);
	if(type=='color')
	{
		var action='changeColor';
	}
	else
	{
		var action= 'changeSize';
	}

	cancel(divnew.parentNode.parentNode.id, 0);
	
	var inner='';
	var div = (divnew.parentNode.parentNode);	
	
	for( var i = 0 ; i < divnew.opt.length ; i++ )
	{
		if (i<(divnew.opt.length-1))
			txt= divnew.opt[i]+", ";
		else
			txt=divnew.opt[i];	
		inner+=txt;
	}

	Arr=serialize(divnew.opt);
	var url='DB/service.php?action='+action+'&idProduct='+idProduct+'&Array='+Arr;
	ajaxrequest( nocache(url),a=function(){}, $('aux_'+type), 1 );
	div.innerHTML=inner;
}
*/
function cancel_edit(div, flag)
{
	div=$(div);
	div.style.backgroundColor = div.old_backgroundColor;
	if (flag) div.innerHTML = div.old_innerHTML;
	div.style.height = div.old_height;
	div.style.padding = div.old_padding;
	div.onclick	= div.oldOnclick;
	div.onmouseover = div.oldonmouseover;
	div.onmouseout = div.oldonmouseout;
	div.className= div.oldoldClassName;

}
function addValue( type, val )
{
	if ($('div_'+type)==null) return;
		var div = $('div_'+type);
		div.innerHTML='';
		if( !isdefined(div.opt) ) div.opt = [];
	
		flag = 1;
		for( var i = 0 ; i < div.opt.length ; i++ )
			if(div.opt[i]==val)
				flag=0
		if(flag)
			div.opt.push( val );

		for( var i = 0 ; i < div.opt.length ; i++ )
		{
			var a = ce( 'A' );
			a.className = 'item_sizecolor';
			a.innerHTML=div.opt[i];
			a.i=i;
			a.type=type;
			a.onclick= function(){ remove(this, type); }
			div.appendChild(a);

			if (i<div.opt.length-1)
			{
				var span = ce( 'SPAN' );
				span.innerHTML=", ";
				span.className = 'item_sizecolor';
				div.appendChild(span);
			}
			
		}
		//div.innerHTML=out;
		

		if (div.opt.length > 0)
		{
			div.style.paddingTop=5;
			div.style.paddingBottom=5;
			div.style.width=300;
			div.style.height='auto';
			div.style.display='block';
		}
		else
		{
			div.style.display='none';
		}

		if(div.opt.length >0)
			$(type).value=serialize(div.opt);
		else
			$(type).value='';
}
function more( type )
{
	txt = $('add'+type).value;

	if($('add'+type).getAttribute('helper')!='' && $('add'+type).getAttribute('helper')==txt)
		txt = '';

	if(txt!='')
	{
		arr = txt.split(',');

		for(var i=0; i<arr.length; i++)
			addValue( type, arr[i]);

		$('add'+type).value='';
	}
}
function remove( obj , type)
{
	i=obj.i;
	abubble("DB\/bubble.php?action=changeProduct&i="+i+'&type='+type, getTop(obj)+5 ,getLeft(obj)+(obj.clientWidth/2),400,80);
}


function editimgcolor( div , idBrand, idProduct, idImg, idColor)
{
	if ($('div_edit'+type)!=null) return;
	var url_ajax=div.getAttribute('editable');
	var div_type=div.getAttribute('type');
	
	var oldinner = div.innerHTML;
	var type='imgcolor';

	div.old_backgroundColor= div.style.backgroundColor;
	div.old_innerHTML= div.innerHTML;
	div.old_height= div.style.height;
	div.old_padding= div.style.padding;
	div.oldOnclick = div.onclick;
	div.oldonmouseover = div.onmouseover;
	div.oldonmouseout = div.onmouseout;
	div.oldoldClassName= div.oldClassName;

	div.onclick = function() {};
	div.innerHTML='';
	div.onmouseover = '';
	div.onmouseout = '';
		
	div.style.backgroundColor='#FFF';
	div.className = '';
	div.style.height='auto';
	div.innerHTML='<div id="div_edit'+type+'" style="padding-left: 10px; padding-top: 10px;"><form errfloat="right"><div><input type="text" style="width: 300px;" id="add'+type+'" name="add'+type+'" predictive="DB/service.php?action=showProductColor&firstrec=0&numrec=0&idProduct='+idProduct+'" allownew=0  value="'+div.old_innerHTML+'"></div><div style="padding-top: 10px;"><input type="button" value="SAVE" style="width: 70px;" onmousedown="save_imgcolor(\''+div.id+'\', \''+idImg+'\', \''+idProduct+'\');"/><input type="button" value="CANCEL" style="width: 70px;" onmousedown="cancel_edit(\''+div.id+'\',1);"/></div></form></div>';
	formformat($('div_edit'+type));
}
function save_sizecolor( type , idProduct, divid)
{
	var divnew = $('div_'+type);
	if(type=='color')
	{
		var action='changeColor';
	}
	else
	{
		var action= 'changeSize';
	}

	cancel_edit(divnew.parentNode.parentNode.id, 0);
	
	var inner='';
	var div = (divnew.parentNode.parentNode);	
	
	for( var i = 0 ; i < divnew.opt.length ; i++ )
	{
		if (i<(divnew.opt.length-1))
			txt= divnew.opt[i]+", ";
		else
			txt=divnew.opt[i];	
		inner+=txt;
	}
	Arr=serialize(divnew.opt);
	var url='DB/service.php?action='+action+'&idProduct='+idProduct+'&Array='+Arr;

	//added
	if($(divid)!=null) $(divid).old_innerHTML = inner;

	ajaxrequest( nocache(url),function(){ cancel_edit(divid,1); }, $('aux_'+type), 1 );
	div.innerHTML=inner;
}

function save_show( idBrand, divid, type)
{
	if($('div_'+type)==null)
		return;
	else
		divnew = $('div_'+type);
		
	inner = '';
	for( var i = 0 ; i < divnew.opt.length ; i++ )
	{
		if (i<(divnew.opt.length-1))
			txt= divnew.opt[i]+", ";
		else
			txt=divnew.opt[i];	
		inner+=txt;
	}
	Arr=serialize(divnew.opt);
	
	obj = divnew.parentNode.parentNode;
	cancel_edit(divid, 1);
	obj.innerHTML = inner;

	var url='DB/service.php?action=add'+type+'Arr&idBrand='+idBrand+'&arr='+Arr; 
	ajaxrequest( nocache(url),a=function(){}, null, 1 );
}

function save_imgcolor( divid , idImg, idProduct)
{
	var divnew = $('addimgcolor');

	if(divnew!=null)
	{
		cancel_edit(divid, 1);
		$(divid).innerHTML=divnew.value;

		var url='DB/service.php?action=setImgColor&idProduct='+idProduct+'&idImg='+idImg+'&color='+divnew.value; 
		ajaxrequest( nocache(url),function(){}, null, 1 );
	}
}



function editshowroom( div , idBrand, type)
{
	if ($('div_edit_')!=null) return;
	var url_ajax=div.getAttribute('editable');
	var div_type=div.getAttribute('type');
	

	var oldinner = div.innerHTML;
	olddata= oldinner.split(', ');

	if(oldinner=='')
		olddata=[];

	newpredictive = div.getAttribute('editpredictive');
	div.old_backgroundColor= div.style.backgroundColor;
	div.old_innerHTML= div.innerHTML;
	div.old_height= div.style.height;
	div.old_padding= div.style.padding;
	div.oldOnclick = div.onclick;
	div.oldonmouseover = div.onmouseover;
	div.oldonmouseout = div.onmouseout;
	div.oldoldClassName= div.oldClassName;

	div.onclick = function() {};
	div.innerHTML='';
	div.onmouseover = '';
	div.onmouseout = '';
	
	div.style.backgroundColor='#FFF';
	div.style.padding=0;
	div.className = '';
	div.style.height='auto';

	txttype = type;
	if(txttype=='tradeshow')
		txttype= 'trade show';

	//div.innerHTML='<div id="div_edit'+type+'" style="padding-left: 6px; padding-bottom: 6px;"><div id="aux_'+type+'" style="display: none;"></div><div id="div_'+type+'"></div><form><input type="text" style="width: 280px; height: 31px;" id="add'+type+'" name="add'+type+'" predictive="'+newpredictive+'" allownew=1>&nbsp;&nbsp;<input type="button" onclick="more(\''+type+'\');" value="+" style=" font-size: 20px;"/><input type="button" value="Save" style="width: 70px;" onmousedown="save_show(\''+idBrand+'\',\''+div.id+'\', \''+type+'\');"/><input type="button" value="Cancel" style="width: 70px;" onmousedown="cancel_edit(\''+div.id+'\',1);"/></form></div>';

	//<div class="plus" onclick="more(\''+type+'\');" style="float: left;margin-left: 10px;">&nbsp;</div>

	div.innerHTML='<div id="div_edit'+type+'" style="padding-left: 6px; padding-bottom: 6px;"><div id="aux_'+type+'" style="display: none;"></div><div id="div_'+type+'"></div><form errfloat="right"><div style=" display: table;"><div style="float: left;"><input type="text" style="width: 265px;" id="add'+type+'" name="add'+type+'" predictive="'+newpredictive+'" allownew=1 hhelper="(Type in a '+txttype+' and click the + to save and add more)" onchange="more(\''+type+'\');">&nbsp</div><input type="button" onclick="more(\''+type+'\');" style="float: right;" value="ADD"/></div><div style="margin-top: 4px;"><div style="float: left;"><input type="button" value="SAVE" style="width: 70px;" onmousedown="save_show(\''+idBrand+'\',\''+div.id+'\', \''+type+'\');"/></div><div style="float: left;"><input type="button" value="CANCEL" style="width: 70px;" onmousedown="cancel_edit(\''+div.id+'\',1);"/></div><div style="float: right;">&nbsp</div></form></div>';
	
	formformat($('div_edit'+type));

	var divnew = $('div_'+type);
	divnew.opt=olddata;
	for( var i = 0 ; i < divnew.opt.length ; i++ )
	{
		var a = ce( 'A' );
		a.className = 'item_sizecolor';
		a.innerHTML=divnew.opt[i];
		a.i=i;
		a.type=type;
		a.onclick= function(){ remove(this, type); }
		divnew.appendChild(a);

		if (i<divnew.opt.length-1)
		{
			var span = ce( 'SPAN' );
			span.innerHTML=", ";
			span.className = 'item_sizecolor';
			divnew.appendChild(span);
		}
	}
	divnew.style.paddingTop=5;
	divnew.style.paddingBottom=5;
	divnew.style.width=300;
	div.style.height='auto';
	divnew.style.display='block';
}


