var debug = true;

// Object for cart usage ( new ElementObj(imageeID, templateID) ) // 
function ElementObj(imageid, templateID){
		this.imgID = imageid;
		this.tempID = templateID;
}

// Select option from a select box where value special usage ( selectOption( target_selctbox , value ) )
function selectOption(targ, val){
	for (var i=0; i<targ.options.length; i++){
		if(targ.options[i].value==val){
			targ.options[i].selected = true;
		}
	}
}

// Function translate selected template value to an image and execute CartHandler usage ( templateSelected( target_select_box , imageID ) )//
function templateSelected( tar , id ){
	var selectedValue = tar.options[tar.selectedIndex].value;
	DoCartAction( "settemplate" , id , selectedValue );
}

function popup(src , w , h , sca, scr){
	
	var win, height, widht, scale, scroll, winwidth, winheight, y, x;
	
	winheight = screen.height;
	winwidth = screen.width;	
	height = h;
	width = w;
	
	x = ( winwidth / 2 ) - ( width / 2 );
	y = ( winheight / 2 ) - ( height / 2 );
	
	scale = (sca)?",scrollbars=yes":",scrollbars=no";
	scroll = (scr)?",resizable=yes":",resizable=no";
	
	var command = "width="+width+",height="+height+",top="+y+",left="+x+scale+scroll;
	
	win = window.open(src,"popup",command);
	
}

function refreshandclose()
{			
	window.self.location.reload();
}

function move_item_between_boxes(sourceSelect, destenationSelect)
{
	// Get and delete the selected item from the source destination box
	var	itemText = sourceSelect.options[sourceSelect.selectedIndex].text;
	var	itemValue = sourceSelect.options[sourceSelect.selectedIndex].value;
	sourceSelect.options[sourceSelect.selectedIndex] = null;
	// Insert selected item into the destenation box
	destenationSelect.options[destenationSelect.length]= new Option(itemText,itemValue, false, false);
}
		
function createNewOption( name , value , listName )
{
	var detect = new Analyse();
	list = detect.getElement(listName, window); 
			
	if (typeof(list)=="object")
	{
		if (!inList(list, value))
		{
			list.options[list.options.length] = new Option( name , value );
		}
		else
		{
			alert('"' + name + '" is already in the list');
		}
	}
	else
	{
		if(debug)
		{
			alert("JavaScript kan ikke finde Listen.. Check din code");
		}
	}
}

function inList(list, value)
{
	var result = false;
	var arr = list.options;
	for (var i = 0; i < arr.length; i++)
	{
		if (arr[i].value == value)
		{
			result = true;
			break;
		}
	}
	return result;
	
}

function removeSelectedOption(list)
{
	list.options[list.selectedIndex] = null;
}

function selectAllOptions(list)
{
	var arr = list.options;
	for (var i = 0; i < arr.length; i++)
	{
		arr[i].selected = true;
	}
}

function refreshWindow(url, keepRefreshing)
{
	if (keepRefreshing)
	{
		window.location.href = url;
		
	}
}

function DoCartAction( method, image, temp )
{

	var oAnalyse = new Analyse();
	
	var form = oAnalyse.getElement("CartHandlerForm", window);
	
	var methodElement = form.method;
	
	var imageElement = form.image;
	
	var templateElement = form.tempid;
		
	methodElement.value = method;
	
	imageElement.value = image;
	
	templateElement.value = temp;
	
	form.submit();

}

function stringReplace(string,text,by)
{
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;

	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength))) return string;
	if (i == -1) return string;

	var newstr = string.substring(0,i) + by;

	if (i+txtLength < strLength)
	{
		newstr += stringReplace(string.substring(i+txtLength,strLength),text,by);
	}

	return newstr;
}

function displayThumbnail( id, ref )
{
	var so = new SWFObject("app/thumbnail.swf?selfReference="+ ref +"&id="+id, "thumb_"+id, "120", "120", "6", "#FFFFFF");
	so.write("thumbnail_display_"+id);
}

function displayPreview( id, ref, orgFile, zoomserver, sharepoint)
{
	var appstr = "app/preview.swf?orgfile=" + orgFile + "&id=" + id +"&sharepoint="+ sharepoint +"&zoomserver="+ zoomserver +"&selfReference="+ ref;
	var so = new SWFObject(appstr, "prev_"+id, "490", "460", "8", "#FFFFFF");
	so.write("preview_display_"+id);
}
