function getURLVar ( Var, URL )
{
 var URL = URL == null ? window.location.href : URL;
 var re = new RegExp ( ".*[\?|&]" + Var + "=([^&]+).*", "g" );
 if ( URL.match ( re ) )
 {
 Val = URL.replace ( re, "$1" );
 return Val;
 }
 return;
}

var picW = parseInt(getURLVar("picW"));
var picH = parseInt(getURLVar("picH"));

if(picW>0 && picH>0)
{  
	if(picW > picH)
	{
		picH = 153 * picH / picW;
		//picH = (picH<=2) ? 2 : picH;
		picW = 153;
	}
	else
	{
		picW = 120 * picW / picH;
		//picW = (picW<=2) ? 2 : picW;
		picH = 120;
	}
}

var coAction = getURLVar("action");
var uploadpic;
var objsubmitpic = document.getElementById('submitpic');
if(coAction == "addtobag"){
	uploadpic = getURLVar("uploadpic");
	pre_load_image(uploadpic);
	document.getElementById('custompic').value = uploadpic;
	document.getElementById('uploadpic').src = uploadpic;
	document.getElementById('uploadpic').style.width = picW + 'px';
	document.getElementById('uploadpic').style.height = picH + 'px';
	document.getElementById('uaction').value = coAction;
	objsubmitpic.style.cursor="pointer";
	objsubmitpic.style.border="0";
	objsubmitpic.style.marginLeft="26px";
	objsubmitpic.style.width = "132px";
	objsubmitpic.style.height = "34px";
	objsubmitpic.style.background = "url(http://cdn.cloudfiles.mosso.com/c46592/images/nav08/prod2.gif) -406px -99px no-repeat"; 
	objsubmitpic.onmouseover = function () {objsubmitpic.style.backgroundPosition = "-406px -134px";}
	objsubmitpic.onmouseout = function () {objsubmitpic.style.backgroundPosition = "-406px -99px";}
	objsubmitpic.onmousedown = function () {objsubmitpic.style.backgroundPosition = "-406px -167px";}
	objsubmitpic.style.display = "block";
	objsubmitpic.title = "add to cart";
	//document.getElementById('filename').style.display = "block";
	document.getElementById('resize').style.display = "block";
	document.getElementById('reupload').style.display = "block";
	document.getElementById('upbrowse').style.display = "none";
	document.getElementById('proqty').style.display = "block";
}else{
	document.getElementById('proqty').style.display = "none";
	document.getElementById('filename').style.display = "none";
	objsubmitpic.style.display = "none";
	objsubmitpic.style.width = "127px";
	objsubmitpic.style.height = "24px";
	objsubmitpic.style.background = "url(http://cdn.cloudfiles.mosso.com/c46592/images/nav08/custom-photo_03.gif) 0 -76px no-repeat"; 
	objsubmitpic.onmouseover = function () {objsubmitpic.style.backgroundPosition = "0 -100px";}
	objsubmitpic.onmouseout = function () {objsubmitpic.style.backgroundPosition = "0 -76px";}
	objsubmitpic.onmousedown = function () {objsubmitpic.style.backgroundPosition = "0 -124px";}

}

function checkupdate(oform)
{
	oform.action = "/upload/uploadPic.php";
	if(getURLVar("action") != "addtobag" ) 
	{
		if(document.getElementById('imagefile').value=="")
		{
			alert("No Image file!");
			document.getElementById('imagefile').focus();
			return false;
		}
	}
	document.getElementById('uaction').value = getURLVar("action");
}

function pre_load_image ( img )
{
	imgArr = new Array();
	imgArr[0] = img;
 var preLoads = new Array ();
 for ( var i = 0; i < imgArr.length; i ++ )
 {
 preLoads[i] = new Image ();
 preLoads[i].src = imgArr[i];
 }
}

//Firefox add "px"
function getPos(objRef,sProp) {
	var iPos = 0
	while (objRef!=null) {
		iPos+=objRef["offset" + sProp]
		objRef = objRef.offsetParent
	}
	return iPos
}

// override funcs defined within the Lib
function my_PickFunc()
{

}

function my_DragFunc()
{
    if (dd.obj.name == 'thumb')
    {
		var num = parseInt(dd.elements.thumb.x-dd.elements.thumb.defx)+51;
        var moveradio = (num==1) ? 1 : (num*(4/picW-0.02)+1);
		dd.elements.uploadpic.resizeTo( picW * moveradio, picH * moveradio );
		
		document.getElementById('picWidth').value = dd.elements.uploadpic.w;
		document.getElementById('picHeight').value = dd.elements.uploadpic.h;
    }
	if (dd.obj.name == 'uploadpic')
	{
		var cuttop = getPos(document.getElementById('cut'),"Top");
		var cutleft = getPos(document.getElementById('cut'),"Left");
		document.getElementById('picX').value = dd.elements.uploadpic.x-cutleft;
		document.getElementById('picY').value = dd.elements.uploadpic.y-cuttop;
	}
	//dd.elements.uploadpic.zindex = "-1000";
}

function my_DropFunc()
{
	dd.elements.uploadpic.setZ(1);
}


if(coAction == "addtobag"){
	SET_DHTML(CURSOR_MOVE, RESIZABLE, NO_ALT, SCROLL, "uploadpic"+TRANSPARENT, "thumb"+HORIZONTAL+MAXOFFLEFT+50+MAXOFFRIGHT+50, "track"+NO_DRAG, "tryit", "upleft");

	dd.elements.thumb.moveTo(dd.elements.track.x+0, dd.elements.track.y);
	dd.elements.track.setZ(3);
	dd.elements.thumb.setZ(dd.elements.track.z+1);
	dd.elements.track.addChild('thumb');
	dd.elements.thumb.defx = dd.elements.track.x+38;
	dd.elements.uploadpic.resizeTo( picW , picH  );

	document.getElementById('picWidth').value = picW;
	document.getElementById('picHeight').value = picH;		
	dd.elements.uploadpic.swapImage( uploadpic );
	dd.elements.thumb.resizeTo(16,16);
	dd.elements.track.resizeTo(104,16);
}else{
	
	//dd.elements.uploadpic.setDraggable(false);
	//dd.elements.thumb.setDraggable(false);
	//dd.elements.thumb.hide();
	//dd.elements.track.hide();
}
//Set_Cookie( name, value );

function checkfiletype(obj)
{
	var filestrings = obj.value.split(".");
	var lastnum = filestrings.length-1;
	var filetype = filestrings[lastnum];
	if(filetype=='gif'||filetype=='jpeg'||filetype=='jpg'||filetype=='GIF'||filetype=='JPEG'||filetype=='JPG')
	{
		document.getElementById('txtFile').value = obj.value;
		objsubmitpic.style.display = "block";
		
	}
	else
	{
		objsubmitpic.style.display = "none";
		alert('Image file type error');
	}
}