var appimagespath = "../includes/template/admin/images/";
var cartimagespath = "../includes/files_products/";

/*
*	Will get content from html editor and place them in textarea called "thecontent"
*	To be used with pinEdit CMS.
 */
function submitCMS() {
	if (document.getElementById("pinEditCMS") && document.getElementById("thecontent")) {
		document.getElementById("thecontent").value = document.getElementById("pinEditCMS").contentWindow.editGetXHtmlBody();
	}
	return true;
}


/* ==================================================== */
// ::: opens product image in a new scaled, centred window
/* ==================================================== */
function popupImage(imageName,imageWidth,imageHeight) {
	var newPopup = window.open("","imagePreview","width="+imageWidth+",height="+imageHeight+",scrollbars=0,resizable=0,left="+((screen.width - imageWidth) / 2)+",screenX="+((screen.width - imageWidth) / 2)+",top="+((screen.height - imageHeight) / 2)+",screenY="+((screen.height - imageHeight) / 2)+"");
	var html = "<html><head><title>Image Preview</title></head><body style=\"margin:0 0 0 0;padding:0 0 0 0;\"><img src=\""+cartimagespath+imageName+"\" width=\""+imageWidth+"\" height=\""+imageHeight+"\" alt=\"Image Preview\" /></body></html>";
	newPopup.document.open()
	newPopup.document.write(html)
	newPopup.document.close()
}


/* ==================================================== */
// ::: clears form field
/* ==================================================== */
function clearfield (thefield,thevalue) {
	if (thefield.value == thevalue) {
		thefield.value = "";
		thefield.style.color = "defaultTextcolor";
	}
}


/* ==================================================== */
// ::: Changes multi-select form elements to a more usable element
// :::
// ::: coded by Kae - kae@verens.com
// ::: I'd appreciate any feedback.
// ::: You have the right to include this in your sites.
// ::: Please retain this notice.
/* ==================================================== */

/* edit these variables to customise the multiselect */ {
 var show_toplinks=true;
}

/* global variables - do not touch */ {
 var isIE=window.attachEvent?true:false;
 var selectDefaults=[];
}
function addEvent(el,ev,fn){
 if(isIE)el.attachEvent('on'+ev,fn);
 else if(el.addEventListener)el.addEventListener(ev,fn,false);
}



function buildMultiselects(){
 do{
  found=0;
  a=document.getElementsByTagName('select');
  for(b=0;b<a.length,!found;b++){
   var ms=a[b];
   if(ms==null)break;
   var name=ms.name.substring(0,ms.name.length-2);
   if(ms.multiple){
    /* common variables */ {
     selectDefaults[name]=[];
     var found=1,disabled=ms.disabled?1:0,width=ms.offsetWidth,height=ms.offsetHeight;
     if(width<150)width=150;
     if(height<60)height=60;
    }
    /* set up wrapper */ {
     var wrapper=document.createElement('span');
     wrapper.style.width=width+"px";
     wrapper.style.height=height+"px";
     wrapper.style.position='relative';
     wrapper.className += "multiselect";
    }
    if(show_toplinks){ /* reset, all, none */
	 wrapper.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','checked');"),'all'));
     wrapper.appendChild(document.createTextNode(', '));
     wrapper.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','');"),'none'));
     wrapper.appendChild(document.createTextNode(', '));
     wrapper.appendChild(newLink("javascript:"+(disabled?"alert('selection disabled')":"multiselect_selectall('"+name+"','reset');"),'reset'));
    }
    /* setup multiselect */ {
     newmultiselect=document.createElement('div');
     //newmultiselect.style.position='absolute';
     newmultiselect.style.top=show_toplinks?'15px':'0';
     newmultiselect.style.left='0';
     newmultiselect.style.overflow='auto';
     newmultiselect.style.width=(isIE?width-4:width)+"px";
     newmultiselect.style.height=show_toplinks?height-(isIE?19:15)+"px":height+'px';
    }
    c=ms.getElementsByTagName('option');
    for(d=0;d<c.length;d++){
     var label=document.createElement('label');
     label.style.display="block";
     label.style.paddingLeft="20px";
     checkbox=document.createElement('input');
     checkbox.type="checkbox";
     if(c[d].selected){
      checkbox.checked="checked";
      checkbox.defaultChecked=true;
     }
     if(c[d].disabled){
      checkbox.disabled='disabled';
      label.style.color='#666';
     }
     selectDefaults[name][d]=c[d].selected?'checked':'';
     if(disabled)checkbox.disabled="disabled";
     checkbox.value=c[d].value;
     checkbox.style.marginLeft="-16px";
     checkbox.style.marginTop="-2px";
     checkbox.name=ms.name;
	 
	 /* ====  Additions by Paulus  ==== */ {
		checkbox.style.margin="0 5px 0 -16px";
		checkbox.style.padding="0";
		checkbox.style.width="auto";
		label.style['float']='none';
		label.style.width="auto";
		label.style.textAlign="left";
	 }

     // escape the label
     var text=c[d].innerHTML;
     text=text.replace(/\&nbsp;?/g,' ');
     text=text.replace(/\&lt;?/g,'<');
     text=text.replace(/\&gt;?/g,'>');

     labelText=document.createTextNode(text);
     label.appendChild(checkbox);
     label.appendChild(labelText);
     newmultiselect.appendChild(label);
    }
   wrapper.appendChild(newmultiselect);
   ms.parentNode.insertBefore(wrapper,ms);
   ms.parentNode.removeChild(ms);
   }
  }
 }while(found);
}

function multiselect_selectall(name,val){
 var els=document.getElementsByTagName('input'),found=0;
 for(var i=0;i<els.length;++i){
  if(els[i].name==name+'[]' || els[i].name==name)els[i].checked=val=='reset'?selectDefaults[name][found++]:val;
 }
}
function newLink(href,text){
 var e=document.createElement('a');e.href=href;e.appendChild(document.createTextNode(text));return e;
}

addEvent(window,'load',buildMultiselects);


/* ==================================================== */
// ::: IE fix from www.alistapart.com
// ::: modified to hook into <tbody class="hover">
/* ==================================================== */
startList = function() {
	if (document.all&&document.getElementsByTagName("TBODY")) {
		var navRoot = document.getElementsByTagName("TBODY");
		for (var x=0; x<navRoot.length; x++) {
			if (navRoot[x].className.indexOf('hover') > -1) {
				for (var i=0; i<navRoot[x].childNodes.length; i++) {
					var htr = navRoot[x].childNodes[i];
					htr.onmouseover=function() {
						this.className+=" over";
					}
					htr.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
}
window.onload=startList;


/* ==================================================== */
// ::: Progress bar functions (when sending emails)
/* ==================================================== */
var pb;
function init_progbar () {
	
	// progress bar wrap
	var pw = document.createElement("DIV");
	pw.id = "progressbarwrap";
	
	// progress bar container
	var pc = document.createElement("DIV");
	pc.style.border = "1px solid #DDD";
	pc.style.marginLeft = "2em";
	pc.style.marginRight = "2em";
	pc.style.marginBottom = "2em";
	pc.style.padding = "0";
	pc.id = "progressbarcontainer";
	
	// progress bar
	pb = document.createElement("DIV");
	pb.style.background = "#FC9";
	pb.style.width = "0%";
	pb.style.height = "1em";
	pb.style.textAlign = "center";
	pb.style.padding = "0.2em";
	pb.id = "progressbar";
	
	// text, as empty element will (should) not display
	var pt = document.createTextNode("Sending mail...");
	
	pw.appendChild(pt); // place text in wrap
	pc.appendChild(pb); // place bar in container
	pw.appendChild(pc); // place container in wrap
	document.getElementById("content").appendChild(pw); // place wrap on page
	
}
function update_progbar (wval) {
	document.getElementById("progressbar").style.width = wval; // change bar width
}
function close_progbar () {
	document.getElementById("content").removeChild( document.getElementById("progressbarwrap") ); // remove wrap (all)
}


/* ==================================================== */
// ::: Cart functions
/* ==================================================== */
// var theform = document.forms['newproduct']; // defines the form to be worked on
var optionClassCount = 0;
var optionCount = new Array();
optionCount[0] = 0; // initiate total for orphans
var fileCount = 1;
var selectCount = 1;

function addOptionClass() {
	
	// assign new value
	var newClassValue = theform.temp_option_class.value; 
	
	// validate
	if (newClassValue == "") {
		alert("Please complete the option-class-name field");
		theform.temp_option_class.focus();
		return false;
	}
	
	optionClassCount++;
	
	// set option count array
	eval("optionCount["+optionClassCount+"] = 0;");
	
	// clear field
	theform.temp_option_class.value = "";
	
	// create new option
	theform.temp_option_classref.options[optionClassCount] = new Option(newClassValue,optionClassCount);
	
	var optarget = document.getElementById('currentoptions');
	
	var oc = document.createElement("DIV");
	oc.style.marginLeft = "1em";
	oc.id = "optionclass_" + optionClassCount;
	oc.name = "optionclass_" + optionClassCount;
	
	var ocstrong = document.createElement("DIV");
	ocstrong.style.borderTop = "1px solid #999";
	ocstrong.style.fontWeight = "bold";
	var octext = document.createTextNode(newClassValue);
	ocstrong.appendChild(octext);
	optarget.appendChild(ocstrong);
	
	var ocinput = document.createElement("INPUT");
	ocinput.type = "hidden";
	ocinput.name = "optionclass_" + optionClassCount;
	ocinput.value = newClassValue;
	oc.appendChild(ocinput);
	
	optarget.appendChild(oc);
	
}

function addOption() {
	
	// assign values
	var optionClass = theform.temp_option_classref.value; 
	var optionValue = theform.temp_option_name.value;
	var optionCost = theform.temp_option_cost_1.value + "." + theform.temp_option_cost_2.value;
	optionClass = optionClass * 1; // convert to integer
	
	// validate
	if (optionValue == "") {
		alert("Please complete option-name field.");
		theform.temp_option_name.focus();
		return false;
	}
	
	// increment option count for this class
	optionCount[optionClass] ++;
	
	// output
	var otarget = document.getElementById('optionclass_'+optionClass);
	
	var otext = document.createTextNode(optionCost + " " + optionValue);
	otarget.appendChild(otext);
	
	var oinput = document.createElement("INPUT");
	oinput.type = "hidden";
	oinput.name = "option_" + optionClass + "_" + optionCount[optionClass];
	oinput.value = optionValue;
	otarget.appendChild(oinput);
	
	var ocost = document.createElement("INPUT");
	ocost.type = "hidden";
	ocost.name = "optioncost_" + optionClass + "_" + optionCount[optionClass];
	ocost.value = optionCost;
	otarget.appendChild(ocost);
	
	var obr = document.createElement("BR");
	otarget.appendChild(obr);
	
	// clear fields
	theform.temp_option_name.value = "";
	theform.temp_option_cost_1.value = "0";
	theform.temp_option_cost_2.value = "00";
	
}

// prevent form from being submitted when enter key is pressed [only on applied fields]
function entsub(event,func) {
	if ((event && event.which == 13) || (window.event && window.event.keyCode == 13)) {
		eval(func);
		return false;
	}
}

function generateFileField(obj) {
	if (obj.value != "") {
	
		fileCount ++;
		
		// don't let this element generate another field
		obj.onchange = "";
		
		var ftarget = document.getElementById('fileuploads');
		
		// break-space
		var br = document.createElement("BR");
		ftarget.appendChild(br);
		
		// generate new file input
		var newInput = document.createElement("INPUT");
		newInput.id = "filetitle_"+fileCount;
		newInput.name = "filetitle_"+fileCount;
		newInput.type = "text";
		newInput.value = "[file title here]";
		newInput.onfocus = function () { clearfield(this,'[file title here]') }
		ftarget.appendChild(newInput);
		
		var sometext = document.createTextNode(" ");
		ftarget.appendChild(sometext);
			
		newInput = document.createElement("INPUT");
		newInput.id = "file_"+fileCount;
		newInput.name = "file_"+fileCount;
		newInput.type = "file";
		newInput.size = "30";
		newInput.title = "New file upload";
		newInput.onchange = function () { generateFileField(this) }
		ftarget.appendChild(newInput);
	}
}

function deleteFile(fileID) {
	document.getElementById('currentfile_'+fileID).style.display = 'none';

	var newInput = document.createElement("INPUT");
	newInput.id = "filedelete_"+fileID;
	newInput.name = "filedelete_"+fileID;
	newInput.type = "hidden";
	newInput.value = 1;
	document.getElementById('currentfiles').appendChild(newInput);
}

function deleteOptionClass(optionClassID,optionClassName) {
	if (confirm('Are you sure you want to delete option class: '+optionClassName)) {
		
		eval("var optionClassDBID = theform.optionclassid_"+optionClassID+".value;");
		document.getElementById('optionclass_'+optionClassID).style.display = 'none';
		document.getElementById('optionclasstitle_'+optionClassID).style.display = 'none';
	
		var newInput = document.createElement("INPUT");
		newInput.id = "optionclassdelete_"+optionClassDBID;
		newInput.name = "optionclassdelete_"+optionClassDBID;
		newInput.type = "hidden";
		newInput.value = 1;
		document.getElementById('currentoptions').appendChild(newInput);
	}
}

function deleteOption(optionNum,optionClass,optionName) {
	if (confirm('Are you sure you want to delete option: '+optionName)) {
		
		eval("var optionID = theform.optionid_"+optionClass+"_"+optionNum+".value;");
		document.getElementById('option_'+optionID).style.display = 'none';
		
		var newInput = document.createElement("INPUT");
		newInput.id = "optiondelete_"+optionID;
		newInput.name = "optiondelete_"+optionID;
		newInput.type = "hidden";
		newInput.value = 1;
		document.getElementById('currentoptions').appendChild(newInput);
	}
}


/* ==================================================== */
// ::: CMS menu management functions
/* ==================================================== */

// count menu items
var indexcount = 0;

// padding increment (ems)
var padinc = 2;

// insert menu item
function inserti(linkobj, label, cmsid, type) {
	
	// increment count
	indexcount++;
	
	// hide link
	if (linkobj)
		linkobj.style.display = "none";
	
	// assign row-number
	var rownum = indexcount;
	
	// assign class (colour code)
	var rowclass = "menuitems";
	if (type == "link")
		rowclass += " cmslink";
	else if (type == "static")
		rowclass += " cmsstatictitle";
		
	// menu item container
	var d = document.createElement('DIV');
	d.id = 'menuitem_'+rownum;
	d.className = rowclass;
	
	// cmsid form element
	var i = document.createElement('INPUT');
	i.id = "cmsid_"+rownum;
	i.name = "cmsid_"+rownum;
	i.type = "hidden";
	i.value = cmsid;
	
	// indent form element
	var j = document.createElement('INPUT');
	j.id = "indent_"+rownum;
	j.name = "indent_"+rownum;
	j.type = "hidden";
	j.value = 0;
	
	// indent right button
	var rarrow = document.createElement('IMG');
	rarrow.src = appimagespath+"button_right.gif";
	rarrow.alt = "Increase indent";
	rarrow.id = "rightindent_"+rownum;
	rarrow.width = "12";
	rarrow.height = "12";
	rarrow.title = "Increase indent";
	rarrow.className += " button";
	rarrow.onmousedown = function () { indentright(rownum); }
	
	// label
	var t = document.createTextNode(label);
	
	// menu buttons container
	var e = document.createElement('DIV');
	e.id = 'menuitembuttons_'+rownum;
	e.className = 'menubuttons';
	
	// move-up button
	var uparrow = document.createElement('IMG');
	uparrow.src = appimagespath+"button_up.gif";
	uparrow.alt = "Move Up";
	uparrow.id = "upbutton_"+rownum;
	uparrow.width = "12";
	uparrow.height = "12";
	uparrow.title = "Move item up";
	uparrow.className += " button";
	uparrow.onmousedown = function () { moveup(rownum); }
	e.appendChild(uparrow);
	
	// move-down button
	var darrow = document.createElement('IMG');
	darrow.src = appimagespath+"button_down.gif";
	darrow.alt = "Move Down";
	darrow.id = "downbutton_"+rownum;
	darrow.width = "12";
	darrow.height = "12";
	darrow.title = "Move item down";
	darrow.className += " button";
	darrow.onmousedown = function () { movedown(rownum); }
	e.appendChild(darrow);
	
	// delete button
	var delarrow = document.createElement('IMG');
	delarrow.src = appimagespath+"button_delete.gif";
	delarrow.alt = "Remove";
	delarrow.id = "delbutton_"+rownum;
	delarrow.width = "12";
	delarrow.height = "12";
	delarrow.title = "Remove this item from the menu";
	delarrow.onmousedown = function () { delrow(rownum); }
	e.appendChild(delarrow);
	
	// output everything
	d.appendChild(e);
	d.appendChild(i);
	d.appendChild(j);
	d.appendChild(rarrow);
	d.appendChild(t);
	document.getElementById("menulist").appendChild(d);
}

// indent right
function indentright(ic) {
	
	// assign menu item
	var d = document.getElementById("menuitem_"+ic);
	
	// indent form value
	var indent = document.getElementById("indent_"+ic);
	
	// previous (parent) indent value
	var previndentid = "indent_" + (ic-1);
	var previndent = document.getElementById(previndentid);
	
	// if is indentable (equal or lesser indentations from parent/previous)
	if (indent.value <= previndent.value) {
		
		// get indent value -> convert to INT -> increment
		var i = indent.value * 1;
		i++;
		indent.value = i;
		
		// set padding
		var ipad = i * padinc;
		d.style.marginLeft = ipad+"em";
	
		// define right-arrow
		var rightarrow = document.getElementById("rightindent_"+ic);
		
		// if increased from 0 -> 1 then insert left-indent-arrow
		if (indent.value == 1) {
			var larrow = document.createElement('IMG');
			larrow.src = appimagespath+"button_left.gif";
			larrow.title = "Decrease indent";
			larrow.id = "leftindent_"+ic;
			larrow.width = "12";
			larrow.height = "12";
			larrow.onmousedown = function () { indentleft(ic) };
			d.insertBefore(larrow, rightarrow);
		}
	}
}

function indentleft(ic) {
	
	var rownum = ic;
	var nextrownum = ic + 1;
	
	// define elements
	var d = document.getElementById("menuitem_"+rownum);
	var e = document.getElementById("menuitem_"+nextrownum);
	var indent = document.getElementById("indent_"+rownum);
	var nextindent = document.getElementById("indent_"+nextrownum);
	
	// define indent value -> convert to INT and increment
	var i = indent.value * 1;
	i--;
	indent.value = i;
	
	// define padding
	var ipad = i * padinc;
	d.style.marginLeft = ipad+"em";
	
	// if indent is now decremented to 0 -> remove left-indent-button
	if (i == 0) {
		var leftarrow = document.getElementById("leftindent_"+ic);
		d.removeChild(leftarrow);	
	}
	
	// if the item beneath this is indented further
	if (nextindent.value > (i+1))
		indentleft(nextrownum);
}

function moveup(rn) {
	
	// if not top row
	if(rn != 1) {
		
		// define previous and next rows
		var pp = (rn - 2);
		var pc = (rn - 1);
		var nc = (rn + 1);
		
		// define elements being moved
		var movemenu = document.getElementById('menuitem_' + rn);
		var movecmsid = document.getElementById('cmsid_' + rn);
		var moveindent = document.getElementById('indent_' + rn);
		var moveupbutton = document.getElementById('upbutton_' + rn);
		var movedownbutton = document.getElementById('downbutton_' + rn);
		var moverightbutton = document.getElementById('rightindent_' + rn);
		if (moveindent.value > 0) {
			var moveleftbutton = document.getElementById('leftindent_' + rn);
		}
		var movedelbutton = document.getElementById('delbutton_' + rn);
		var movebuttondiv = document.getElementById('menuitembuttons_' + rn);
		
		// define elements being swapped
		var prevmenu = document.getElementById('menuitem_' + pc);
		var prevcmsid = document.getElementById('cmsid_' + pc);
		var previndent = document.getElementById('indent_' + pc);
		var prevupbutton = document.getElementById('upbutton_' + pc);
		var prevdownbutton = document.getElementById('downbutton_' + pc);
		var prevrightbutton = document.getElementById('rightindent_' + pc);
		if (previndent.value > 0) {
			var prevleftbutton = document.getElementById('leftindent_' + pc);
		}
		var prevdelbutton = document.getElementById('delbutton_' + pc);
		var prevbuttondiv = document.getElementById('menuitembuttons_' + pc);
		
		// define elements: old child of moved item
		var ncindentelement = document.getElementById('indent_' + nc);
		if (ncindentelement)
			var ncindent = ncindentelement.value * 1;
		
		// define elements: new parent of moved item
		var ppindent = document.getElementById('indent_' + pp).value * 1;
		
		var menudiv = document.getElementById('menulist');
		
		menudiv.removeChild(movemenu);
		
		movemenu.id = 'menuitem_' + pc;
		movecmsid.id = 'cmsid_' + pc;
		movecmsid.name = 'cmsid_' + pc;
		moveindent.id = 'indent_' + pc;
		moveindent.name = 'indent_' + pc;
		moveupbutton.id = 'upbutton_' + pc;
		moveupbutton.onmousedown = function () { moveup(pc); }
		movedownbutton.id = 'downbutton_' + pc;
		movedownbutton.onmousedown = function () { movedown(pc); }
		moverightbutton.id = 'rightindent_' + pc;
		moverightbutton.onmousedown = function () { indentright(pc); }
		if (moveindent.value > 0) {
			moveleftbutton.id = 'leftindent_' + pc;
			moveleftbutton.onmousedown = function () { indentleft(pc); }
		}
		movedelbutton.id = 'delbutton_' + pc;
		movedelbutton.onmousedown = function () { delrow(pc); }
		movebuttondiv.id = 'menuitembuttons_' + pc;
		
		prevmenu.id = 'menuitem_' + rn;
		prevcmsid.id = 'cmsid_' + rn;
		prevcmsid.name = 'cmsid_' + rn;
		previndent.id = 'indent_' + rn;
		previndent.name = 'indent_'+rn;
		prevupbutton.id = 'upbutton_' + rn;
		prevupbutton.onmousedown = function () { moveup(rn); }
		prevdownbutton.id = 'downbutton_' + rn;
		prevdownbutton.onmousedown = function () { movedown(rn); }
		prevrightbutton.id = 'rightindent_' + rn;
		prevrightbutton.onmousedown = function () { indentright(rn); }
		if (previndent.value > 0) {
			prevleftbutton.id = 'leftindent_' + rn;
			prevleftbutton.onmousedown = function () { indentleft(rn); }
		}
		prevdelbutton.id = 'delbutton_' + rn;
		prevdelbutton.onmousedown = function () { delrow(rn); }
		prevbuttondiv.id = 'menuitembuttons_' + rn;
		
		menudiv.insertBefore(movemenu, prevmenu);
		
		// decrease indent of moved item if required			
		if(moveindent.value > (ppindent + 1))
			indentleft(pc);
		
		// decrease indent of moved item's old child
		if (ncindent > ((previndent.value * 1) + 1))
			indentleft(nc);
		
	} // end if not top row
} // end function

function movedown(rn) {
	if(rn != indexcount) {
		
		var movemenu = document.getElementById('menuitem_' + rn);
		var pc = (rn + 1);
		var tc = (rn + 2);
		var prevmenu = document.getElementById('menuitem_' + pc);
		var twodown = document.getElementById('menuitem_' + tc);
		

		//input fields
		var movecmsid = document.getElementById('cmsid_' + rn);
		var moveindent = document.getElementById('indent_' + rn);
		
		var prevcmsid = document.getElementById('cmsid_' + pc);
		var previndent = document.getElementById('indent_' + pc);
		
		// buttons
		var moveupbutton = document.getElementById('upbutton_'+rn);
		var movedownbutton = document.getElementById('downbutton_'+rn);
		var moverightbutton = document.getElementById('rightindent_'+rn);
		if (moveindent.value > 0) {
			var moveleftbutton = document.getElementById('leftindent_'+rn);
		}
		var movedelbutton = document.getElementById('delbutton_'+rn);
		
		var prevupbutton = document.getElementById('upbutton_'+pc);
		var prevdownbutton = document.getElementById('downbutton_'+pc);
		var prevrightbutton = document.getElementById('rightindent_'+pc);
		if (previndent.value > 0) {
			var prevleftbutton = document.getElementById('leftindent_'+pc);
		}
		var prevdelbutton = document.getElementById('delbutton_'+pc);
		
		// divs
		var movebuttondiv = document.getElementById('menuitembuttons_'+rn);
		var prevbuttondiv = document.getElementById('menuitembuttons_'+pc);
		
		var menudiv = document.getElementById('menulist');
		
		menudiv.removeChild(movemenu);
		
		movemenu.id = 'menuitem_'+pc;
		movecmsid.id = 'cmsid_'+pc;
		movecmsid.name = 'cmsid_'+pc;
		moveindent.id = 'indent_'+pc;
		moveindent.name = 'indent_'+pc;
		moveupbutton.id = 'upbutton_'+pc;
		moveupbutton.onmousedown = function () { moveup(pc); }
		movedownbutton.id = 'downbutton_'+pc;
		movedownbutton.onmousedown = function () { movedown(pc); }
		moverightbutton.id = 'rightindent_'+pc;
		moverightbutton.onmousedown = function () { indentright(pc); }
		if (moveindent.value > 0) {
			moveleftbutton.id = 'leftindent_'+pc;
			moveleftbutton.onmousedown = function () { indentleft(pc); }
		}
		movedelbutton.id = 'delbutton_'+pc;
		movedelbutton.onmousedown = function () { delrow(pc); }
		movebuttondiv.id = 'menuitembuttons_'+pc;
		
		prevmenu.id = 'menuitem_'+rn;
		prevcmsid.id = 'cmsid_'+rn;
		prevcmsid.name = 'cmsid_'+rn;
		previndent.id = 'indent_'+rn;
		previndent.name = 'indent_'+rn;
		prevupbutton.id = 'upbutton_'+rn;
		prevupbutton.onmousedown = function () { moveup(rn); }
		prevdownbutton.id = 'downbutton_'+rn;
		prevdownbutton.onmousedown = function () { movedown(rn); }
		prevrightbutton.id = 'rightindent_'+rn;
		prevrightbutton.onmousedown = function () { indentright(rn); }
		if (previndent.value > 0) {
			prevleftbutton.id = 'leftindent_'+rn;
			prevleftbutton.onmousedown = function () { indentleft(rn); }
		}
		prevdelbutton.id = 'delbutton_'+rn;
		prevdelbutton.onmousedown = function () { delrow(rn); }
		prevbuttondiv.id = 'menuitembuttons_'+rn;
		
		menudiv.insertBefore(movemenu, twodown);
		
	}
}

function delrow(rn) {
	
	var menudiv = document.getElementById('menulist');
	var rowcount = 1;
	
	for (var i = 1; i <= indexcount; i++) {
	
		// divs
		var chmenu = document.getElementById('menuitem_' + i);
		var chbuttondiv = document.getElementById('menuitembuttons_'+ i);
		
		//input fields
		var chcmsid = document.getElementById('cmsid_' + i);
		var chindent = document.getElementById('indent_' + i);
		
		
		// buttons
		var chupbutton = document.getElementById('upbutton_'+i);
		var chdownbutton = document.getElementById('downbutton_'+i);
		var chrightbutton = document.getElementById('rightindent_'+i);
		if (chindent.value > 0) {
			var chleftbutton = document.getElementById('leftindent_'+i);
		}
		var chdelbutton = document.getElementById('delbutton_'+i);
		
		if (i == rn){
			menudiv.removeChild(chmenu);
			
			var ori = chcmsid.value;
			
			if (IsNumeric(chcmsid)) {
				var showobj = document.getElementById('originalid_' + ori);
				showobj.style.display = "block";
			}
			
		}else {
			
			chmenu.id = 'menuitem_'+rowcount;
			chcmsid.id = 'cmsid_'+rowcount;
			chcmsid.name = 'cmsid_'+rowcount;
			chindent.id = 'indent_'+rowcount;
			chindent.name = 'indent_'+rowcount;
			chupbutton.id = 'upbutton_'+rowcount;
			chupbutton.title = rowcount;
			chupbutton.onmousedown = function () { moveup(this.title); }
			chdownbutton.id = 'downbutton_'+rowcount;
			chdownbutton.title = rowcount;
			chdownbutton.onmousedown = function () { movedown(this.title); }
			chrightbutton.id = 'rightindent_'+rowcount;
			chrightbutton.title = rowcount;
			chrightbutton.onmousedown = function () { indentright(this.title); }
			if (chindent.value > 0) {
				chleftbutton.id = 'leftindent_'+rowcount;
				chleftbutton.title = rowcount;
				chleftbutton.onmousedown = function () { indentleft(this.title); }
			}
			chdelbutton.id = 'delbutton_'+rowcount;
			chdelbutton.title = rowcount;
			chdelbutton.onmousedown = function () { delrow(this.title); }
			chbuttondiv.id = 'menuitembuttons_'+rowcount;
			
			rowcount++;
		}
	}
	indexcount--;	
}


/* ==================================================== */
// ::: IsNumeric function from:
// ::: http://www.codetoad.com/javascript/isnumeric.asp
/* ==================================================== */
function IsNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

/* ==================================================== */
// ::: CRM import customers functions
/* ==================================================== */
	
	var importcount = 0;  // number of fields to be imported
	
	// insert import field to list
	function insertimport(linkobj, label) {
	
		importcount++;  // incrememt count
		
		if (linkobj)
			linkobj.style.display = "none"; // hide link in right menu
			
		var rownum = importcount; // assign row number
		
		// create import row container
		var d = document.createElement('DIV');
		d.id = "importitem_"+rownum;
		
		// create import hidden field
		var i = document.createElement('INPUT');
		i.id = "import_"+rownum;
		i.name = "import_"+rownum; 
		i.type = "hidden";
		i.value = label;
		
		// create label
		var t = document.createTextNode(label);
		
		// remove button and container div
		var e = document.createElement('DIV');
		e.id = "removeimportfield_"+rownum;
		e.className = "removeimportfield";
		
		var del = document.createElement('IMG');
		del.src = appimagespath+"button_delete.gif";
		del.alt = "Remove";
		del.id = "delbutton_"+rownum;
		del.width = "12";
		del.height = "12";
		del.title = "Remove this import field from the list";
		del.onmousedown = function() { delcrmrow(rownum); }
		
		// add remove img to container div
		e.appendChild(del);
		
		// output rows
		d.appendChild(e); // remove button
		d.appendChild(i); // hidden field
		d.appendChild(t); // text label
		
		document.getElementById("importlist").appendChild(d) // add row to import list box
		
	}
	
	function delcrmrow(rn) {
	
		var importlist = document.getElementById('importlist');
		var rc = 1; // new row number
		
		for (var i=1; i <= importcount; i++) {
						
			var er = document.getElementById('importitem_'+i); // entire row
			var bd = document.getElementById('removeimportfield_'+i); // buuton div
			var hf = document.getElementById('import_'+i); // hidden field
			var rb = document.getElementById('delbutton_'+i); // remove button
			
			// remove row
			if (i == rn) {
				
				// remove item from import list
				importlist.removeChild(er);
				// show item again in rightside list
				var orj = hf.value;
				var showobj = document.getElementById('import_'+orj);
				showobj.style.display = "block";
				
			} else {
			
				// update row numbers
				er.id = "importitem_"+rc;
				hf.id = "import_"+rc;
				hf.name = "import_"+rc;
				bd.id = "removeimportfield_"+rc;
				rb.id = "delbutton_"+rc;
				rb.title = rc;
				rb.onmousedown = function() { delcrmrow(this.title); }
				rc++; // increment for next row	
			
			}
		}
		importcount--;	
	}
	
	
	// when field is submitted, check that firstname, lastname and email address 
	// have been entered into the list for importing
	function checkimport() {
	
		var firstname = 0;
		var lastname = 0;
		var email = 0;
		
		for (var j=1; j <= importcount; j++) {
			
			hf = document.getElementById("import_"+j);
			
			if (hf.value == 'firstname')
				firstname = hf.id;
			if (hf.value == 'lastname')
				lastname = hf.id;
			if (hf.value == 'email')
				email = hf.id;
		}

		if (firstname == 0 || lastname == 0 || email == 0 || importcount == 0) {
			alert("You need to have firstname, lastname and email fields to import customers");
			return false;
		} else {
			return true;
		}
	}
