var ie=document.all;
var ns6=document.getElementById&&!document.all;

var dragapproved=false;
var z,x,y,temp1,temp2;

function pozition()
{
	if(findPosX(z)<findPosX(document.getElementById('build_area_div')))
	{
	z.style.left=findPosX(document.getElementById('build_area_div'))+'px';
	}
	
	var goriz=findPosX(document.getElementById('build_area_div'))+parseInt(document.getElementById('build_area_div').style.width)

	if(findPosX(z)>goriz)
	{
	z.style.left=findPosX(document.getElementById('build_area_div'))+parseInt(document.getElementById('build_area_div').style.width)-15+'px';
	}

	///////////////////////////////
	
	if(findPosY(z)<findPosY(document.getElementById('build_area_div')))
	{
	z.style.top=findPosY(document.getElementById('build_area_div'))+'px';
	}

  	var vertical=findPosY(document.getElementById('build_area_div'))+parseInt(document.getElementById('build_area_div').style.height)

	if(findPosY(z)>vertical)
	{
		//alert('Yes');
	z.style.top=findPosY(document.getElementById('build_area_div'))+parseInt(document.getElementById('build_area_div').style.height)-15+'px';
	}

}

function onMouseUp(e){
dragapproved=false;
}


function move(e){
	if (dragapproved){
		z.style.left=(ns6? temp1+e.clientX-x: temp1+event.clientX-x)+'px';
		z.style.top=(ns6? temp2+e.clientY-y : temp2+event.clientY-y)+'px';
		pozition();

		return false;
	}
 document.onmouseup = onMouseUp;
}


function drags(e){
	if (!ie&&!ns6) return;
	var firedobj=ns6? e.target : event.srcElement;
	var topelement=ns6? "HTML" : "BODY";
	if ((firedobj.id=="deleteObjectLNK") || (firedobj.id=="bringToTopObjectLNK")){
		return true;
	}



	while (firedobj.tagName!=topelement&&firedobj.className!="drag"){
		firedobj=ns6? firedobj.parentNode : firedobj.parentElement;
	}
	if (firedobj.className=="drag")
	{
	
		dragapproved=true;
		z=firedobj;
		temp1=findPosX(z);
		temp2=findPosY(z);;
		x=ns6? e.clientX: event.clientX;
		y=ns6? e.clientY: event.clientY;
		
		document.onmousemove=move;
		return false;
	}

}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function posit_rej()
{
	
	for(i=1;i<7;i++)
	{
		document.getElementById('dr_im_'+i+'').style.top=parseInt(document.getElementById('dr_im_'+i+'').style.top)+58+'px';
	}
	

}
function _PrepSubmit(){
	//populate
	document.dmMain.auto_url.value=top.location;

	//collect items info
	var item_counter=0;
	var dmsString="";

	//add build area - always first element
	dmArea=document.getElementById("build_area_div");
	//alert(dmsProps);
	dmsString += ""+ findPosX(dmArea) + "**"+ findPosY(dmArea);
 
 for(var i=1; i<=count_el;i++)
 {
 var k_el=i+10000;
 k_el="img_"+k_el;
 	if (dmsProps[k_el]!=0)
 	{
	 	item_counter++;
		dmsString+="##"+dmsProps[k_el].id+"**"+dmsProps[k_el].src+"**"+ findPosX(dmsProps[k_el])+"**"+findPosY(dmsProps[k_el])+"**"+dmsProps[k_el].width+"**"+dmsProps[k_el].height+"";
	}
 
 }

	if (item_counter==0)
	{
		alert('Please add item to the build area.');
		return false;
	}

	
	if(last_image)
	{
	document.getElementById(last_image).style.border ="none";
	}
	
	if(last_text)
	{
	document.getElementById('text_'+last_text+'').style.border ="1px solid silver";
	}

document.getElementById("d1").style.display="";
document.getElementById("d2").style.display="none";
document.getElementById("d3").style.display="none";
document.getElementById("d4").style.display="none";
document.getElementById("d5").style.display="none";
    document.dmMain.it_coun.value=item_counter;
	document.dmMain.dmsString.value=dmsString;
	document.dmMain.im_nim.value=im_nim;
	document.dmMain.al_ar.value=escape(document.getElementById('cont_al').innerHTML);

	//document.getElementById('bannerImage').innerHTML = '<img class="nodrag" src="make-doll.php?buildWidth=' + document.getElementById('buildWidth').value + '&buildHeight=' + document.getElementById('buildHeight').value + '&dmsString='+ encodeURIComponent(document.getElementById('dmsString').value)+'">';
//alert(document.getElementById('cont_al').innerHTML);
	//window.open('make-doll.php?buildWidth=' + document.getElementById('buildWidth').value + '&buildHeight=' + document.getElementById('buildHeight').value + '&dmsString='+ encodeURIComponent(document.getElementById('dmsString').value)+'','render','resizable=no,scrollbars=yes,width=550,height=700');
document.dmMain.submit();

	return false;
}

document.onmousedown=drags;
document.onmouseup=new Function("dragapproved=false");

//create build area
dms_build_written=true;



