function moveItem(bDir,f)
{
 var el = findSelected(f);
 if (el!=null)
 {
 var idx = el.selectedIndex;
 if ((idx!=-1) && checkDisable(el)) {
 var new_idx = idx + ( bDir? -1 : 1);
 if (new_idx<0) {
 moveItemToEnd(el);
 }else if (new_idx>=el.length) {
 moveItemToBeginning(el);
 }else{
 //switch.
 var oldVal = el[idx].value;
 var oldText = el[idx].text;
 el[idx].value = el[new_idx].value;
 el[idx].text = el[new_idx].text;
 el[new_idx].value = oldVal;
 el[new_idx].text = oldText;
 el.selectedIndex = new_idx;
 }
 }
 }
}
function moveItemToBeginning(el){
 var maxVal = el.length-1;
 var oldValue = el[maxVal].value;
 var oldText = el[maxVal].text;
 for(var i = maxVal; i > 0; i--){
 el[i].text = el[i-1].text;
 el[i].value = el[i-1].value;
 }
 el[0].text = oldText;
 el[0].value = oldValue;
 el.selectedIndex = 0;
}
function moveItemToEnd(el){
 var maxVal = el.length-1;
 var oldValue = el[0].value;
 var oldText = el[0].text;
 for(var i = 0; (i+1) <= maxVal; i++){
 el[i].text = el[i+1].text;
 el[i].value = el[i+1].value;
 }
 el[maxVal].text = oldText;
 el[maxVal].value = oldValue;
 el.selectedIndex = maxVal;
}
function findSelected(el)
{
 var elSelected = null;
 for (var i=0;i<el.form.length;i++)
 {
 if (el.form[i].type=="select-one" && el.form[i].selectedIndex!=-1)
 elSelected = el.form[i];
 }
 return elSelected;
}
function FixBreaks(s)
{
 // Hack to fix escaped breaks;
 while (s.indexOf("\\n")!=-1)
 s = s.replace("\\n","\n");
 return s;
}
function DeleteSpace(sURL,sAlias,sDelete)
{
 if (confirm(FixBreaks(sDelete)))
 window.location = "/spacedelete.aspx?sAlias=" + sAlias + "&" + sURL;
 return false;
}
function selectPartType(el)
{
 var f = document.forms["layout"];
 if (!el.checked) { // Remove It
 for (var i=0;i<f.length;i++)
 {
 if ("select-one"==f[i].type)
 {
 for (var j=0;j<f[i].options.length;j++)
 if (f[i].options[j].value==el.value) {
 f[i].options[j] = null;
 i=1000;
 break;
 }
 }
 }
 }
 else // Add It
 {
 var i = 0;
 while (i<f.length && f[i].type!="select-one")
 i++;
 if (f[i]!=null && f[i].type=="select-one")
 {
 var opt = new Option(el.name,el.value);
 f[i].options[f[i].options.length] = opt;
 deselectColumn(f[i]);
 f[i].selectedIndex = f[i].options.length-1;
 }
 }
}
function moveColumn(el,bDir)
{
 var elFrom = findSelected(el)
 if (elFrom!=null)
 {
 var elTo = null;
 var bFound = false;
 var bWrap = false;
 if (bDir) {
 var i = 0;
 while (!bFound && i<elFrom.form.length)
 {
 if (checkDisable(elFrom.form[i])) {
 if (elTo==null && elFrom.form[i]==elFrom)
 bWrap = true;
 if (!bWrap && elFrom.form[i]==elFrom)
 bFound = true;
 if (elFrom.form[i].type=="select-one" && elFrom.form[i]!=elFrom)
 elTo = elFrom.form[i];
 }
 i++;
 }
 }
 else {
 i = elFrom.form.length-1;
 while (!bFound && i>=0)
 {
 if (checkDisable(elFrom.form[i])) {
 if (elTo==null && elFrom.form[i]==elFrom)
 bWrap = true;
 if (elFrom.form[i].type=="select-one" && elFrom.form[i]!=elFrom)
 elTo = elFrom.form[i];
 if (!bWrap && elFrom.form[i]==elFrom)
 bFound = true;
 }
 i--;
 }
 }
 if (null==elTo) return;
 if (bWrap && elTo!=null)
 bFound= true;
 idx = elFrom.selectedIndex;
 if ((idx!=-1) && checkDisable(elFrom) && checkDisable(elTo) && bFound) {
 var el = new Option(elFrom[idx].text,elFrom[idx].value);
 elTo.options[elTo.length] = el;
 elTo.selectedIndex=elTo.length-1;
 elFrom.options[idx]=null;
 elFrom.selectedIndex = -1;
 }
 }
}
function checkDisable(elSource)
{
 return (elSource.disabled==null || !elSource.disabled);
}
function deselectColumn(col)
{
 for (var i=0;i<col.form.length;i++)
 {
 if (col.form[i]!=col && col.form[i].type=="select-one")
 col.form[i].selectedIndex=-1;
 }
}
function redoLayout1(name)
{
if (oForm!=null) {
 oForm.method = "post";
 oForm.actionType.value = "relayout";
 oForm.action += "#layoutSection";
 //oForm.action = "supb.aspx";
 if (prepareLayoutForServer(false))
 oForm.submit();
}
}
function setupSave()
{
 if (document.getElementById) {
// document.getElementById("subButton").disabled = true;
// document.getElementById("cancelButton").disabled = true;
 }
//if (document.body && document.body.style)
 //document.body.style.cursor = "wait";
}
function saveLayout()
{
 setupSave();
 prepareLayoutForServer(true);
 oForm.submit();
}
function previewStyle(s1)
{
 var ah = screen.availHeight;
 var height = 425;
 var y = (ah - height) / 2;
 window.open("/ThemePreview.aspx?style=" + s1, "_blank", "top=" + y + ",width=710,height=" + height +",directories=no,status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=no")
}
function getSelectedValue(elList)
{
 var i=0;
 while (i<elList.length && !elList[i].checked)
 i++;
 if (elList[i]!=null)
 return elList[i].value
 else
 return "";
}
function getRowCount(v)
 {
 var rowCount=0;
 
  switch(v.toLowerCase())
   {
    case 'onecol':
      rowCount=1;
      break;
     case 'symmetrictwocol':
      rowCount=2;
      break;
     case 'classictwocol':
     rowCount=2;
      break;
     case 'backwardstwocol':
     rowCount=2;
     break;
     case 'classicthreecol':
     rowCount=3;
     break;
      case 'rightheavythreecol':
     rowCount=3;
    break;
   }
   return rowCount;
 }
function prepareLayoutForServer(force)
{
// oForm.rLayout.value = getSelectedValue(document.forms.layoutType.rLayout);
 //if (!force && oForm.rLayout.value == UILayoutTypeName) // Current Layout - don't reload
 //return false;
 var s = '<L T="' + oForm.rLayout.value + '" S="' + oForm.rColor.value + '">';
 s += '<Rs>';
 var formLayout = document.forms["layout"];
 var numColumns = formLayout.length;
 numColumns =getRowCount(oForm.rLayout.value)

 var ix, iy, numLists, partOrder;
 var currentColumn;
 for(ix = 0; ix < numColumns; ix++)
 {
 currentColumn = formLayout[ix];
 if ("select-one"==currentColumn.type) {
 s += '<R T="' + currentColumn.name + '">' + '<Ps>';
 numLists = currentColumn.options.length;
 for(iy =0; iy < numLists; iy++)
 s += '<P T="' + currentColumn.options[iy].value + '" Seq="' + iy + '"></P>';
 s += '</Ps></R>';
 }
 }
 oForm.homePageLayout.value = s + '</Rs></L>';
 return true;
}
