function changeForm(form_old_name, new_form_name, new_form_action, process_cart_change_name) {
  
  //form
  new_form = document.createElement('form');
  new_form.setAttribute('name', new_form_name);
  new_form.setAttribute('action', new_form_action);
  new_form.setAttribute('method', 'post');

  hidden_hint = document.getElementById('hint');
  hidden_ac = document.getElementById('cart_direct_order_action'); 
  hidden_count_order_rows = document.getElementById('count_order_rows');
  hidden_search_code = document.getElementById('search_code');
  hidden_process_cart_change = document.getElementById('process_cart_changes');
  hidden_container = document.getElementById('form_container');
  hidden_current_url = document.getElementById('current_additional_param');
  
  
  new_hidden_current_url = document.createElement('input'); 
  new_hidden_current_url.setAttribute('type', 'hidden');
  new_hidden_current_url.setAttribute('name', hidden_current_url.name);
  new_hidden_current_url.setAttribute('value', hidden_current_url.value);
  
  new_form.appendChild(new_hidden_current_url);

  //hidden field hint
  new_hidden_hint = document.createElement('input'); 
  new_hidden_hint.setAttribute('type', 'hidden');
  new_hidden_hint.setAttribute('name', hidden_hint.name);
  new_hidden_hint.setAttribute('value', hidden_hint.value);
  
  new_form.appendChild(new_hidden_hint); 
  
  //hidden field ac
  new_hidden_ac = document.createElement('input');
  new_hidden_ac.setAttribute('type', 'hidden');
  new_hidden_ac.setAttribute('name', hidden_ac.name);
  new_hidden_ac.setAttribute('value', 'cart_add');
  
  new_form.appendChild(new_hidden_ac);
  
  //hidden field count_order_rows
  new_hidden_count_order_rows = document.createElement('input');
  new_hidden_count_order_rows.setAttribute('type', 'hidden');
  new_hidden_count_order_rows.setAttribute('name', hidden_count_order_rows.name);
  new_hidden_count_order_rows.setAttribute('value', hidden_count_order_rows.value);
  
  
  new_form.appendChild(new_hidden_count_order_rows);
  
  //hidden field search code
  new_search_code = document.createElement('input');
  new_search_code.setAttribute('type', 'hidden');
  new_search_code.setAttribute('name', hidden_search_code.name);
  new_search_code.setAttribute('value', hidden_search_code.value);
  
  new_form.appendChild(new_search_code);
  
  //hidden field hidden_process_cart_change
  new_hidden_process_cart_change = document.createElement('input');
  new_hidden_process_cart_change.setAttribute('type', 'hidden');
  new_hidden_process_cart_change.setAttribute('name', process_cart_change_name);
  new_hidden_process_cart_change.setAttribute('value', "1");
  
  new_form.appendChild(new_hidden_process_cart_change);

  old_form = document.forms[form_old_name];
  
  for(var i = 0; i < old_form.length; i++) {
    if (old_form.elements[i].type == 'text') {
      new_field_text = document.createElement('input');
      new_field_text.setAttribute('type', 'hidden');
      new_field_text.setAttribute('name', old_form.elements[i].name);
      new_field_text.setAttribute('value', old_form.elements[i].value);
      
      new_form.appendChild(new_field_text);
    } else if (old_form.elements[i].getAttribute('special_select') == 'select') {
    
      new_field_select = document.createElement('input');
      new_field_select.setAttribute('type', 'hidden');
      new_field_select.setAttribute('name', old_form.elements[i].name);
      new_field_select.setAttribute('value', old_form.elements[i].options[old_form.elements[i].selectedIndex].value);
      
      new_form.appendChild(new_field_select);
    }
    
  }
  
  hidden_container.appendChild(new_form);
  new_form.submit();
}

function WebForm_FireDefaultButton(event, target) {
  if (event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
    var defaultButton;

    defaultButton = document.getElementById(target);

    if (defaultButton && typeof(defaultButton.click) != "undefined") {
        defaultButton.click();
        event.cancelBubble = true;
        if (event.stopPropagation) event.stopPropagation();
        return false;
    }
  }
  return true;
}

function SHighLight(tbName){
  var infieldselect=document.getElementById('select_'+tbName);
  var infield=document.getElementById(tbName);
  if(infieldselect==null)
    return;

  for(var i=0;i<infieldselect.options.length;i++){
    if(0==infieldselect.options[i].text.toLowerCase().indexOf(infield.value.toLowerCase())){
      infieldselect.selectedIndex=i
      break
    }
  }
}

function SSelect(tbName){
  var infield=document.getElementById(tbName);
  var infieldselect=document.getElementById('select_'+tbName);
  if(infieldselect==null)
    return;

  infield.value=infieldselect.options[infieldselect.selectedIndex].text
}

function SearchProduct(tbName, blankMessage, lengthMessage, hint, minChars) {
  var action=document.getElementById("cart_direct_order_action");
  action.value="search";

  return CDOSubmit(tbName, blankMessage, lengthMessage, hint, minChars);
}

function AddProductToCart(tbName, blankMessage, lengthMessage, hint, minChars) {
  var action=document.getElementById("cart_direct_order_action");
  action.value="cart_add";

  return CDOSubmit(tbName, blankMessage, lengthMessage, hint, minChars);
}

function CDOSubmit(tbName, blankMessage, lengthMessage, hint, minChars) {
  
  for(var i=0; i<100; i++) {
    var infield=document.getElementById(tbName+'['+i+']');

    if (infield!=null) {
      if ((infield.value == '') || (infield.value == hint)) {
        alert(blankMessage);
        infield.focus();
        return false;
      }
      else {
        if (infield.value.length < minChars) {
          alert(lengthMessage);
          infield.focus();
          return false;
        }        
      }
    }
    else {
      break;
    }  
  }
  
  return true;
}

function directOrderCode_onFocus(control, hint) {
  if(control.value==hint) {
    control.value='';
  }
}

function directOrderCode_onBlur(control, hint) {
  if(control.value=='') {
    control.value=hint;
  }
}

function incrementProductQuantity(tbQuantityName) {
  document.getElementById(tbQuantityName).value++;
}

function decrementProductQuantity(tbQuantityName) {

  var tb=document.getElementById(tbQuantityName);

  if (tb.value > 1) {
    tb.value--;
  }
}

function getNextControlIndex(tbName) {
  for(var i=0; i<100; i++) {
    var infield=document.getElementById(tbName+'['+i+']');
    if (infield==null) {
      return i;
    }      
  }

  return -1;
}



function addOrderRows(form_id) {
  
  var orderForm = document.getElementById(form_id);
  var input     = document.createElement('input');
  var fieldId   = getNextControlIndex('direct_order_code');
  hint      = document.getElementById('hint').value;
  
  var divQty = document.createElement('div');
  var divQtyIn = document.createElement('div');
  divQty.className = 'changeQty';
  var div = document.createElement('div');
  div.className="modelRow";  
  var count_field = document.getElementById('count_order_rows');
  if (count_field) {
    count_field.value = ((count_field.value*1)+1);
    div.setAttribute('id', 'div_'+count_field.value);
  }
  
  input.setAttribute('type', 'text');
  input.className = 'producthummer';
  input.setAttribute('name', 'direct_order_code[' + fieldId + ']');
  input.setAttribute('value', hint);
  input.setAttribute('id', 'direct_order_code[' + fieldId + ']');
  input.onfocus = function() {
    if(this.value == hint) {
      this.value = '';
    }
  }
  input.onblur = function() {
    if(this.value == '') {
      this.value = hint;
    }
  }
  div.appendChild(input);
  
  var inputQty = document.createElement('input');
  inputQty.setAttribute('type', 'text');
  inputQty.className ='qty';
  inputQty.setAttribute('name', 'direct_order_quantity[' + fieldId + ']');
  inputQty.setAttribute('id', 'direct_order_quantity[' + fieldId + ']');
  inputQty.setAttribute('value', '1');
  divQtyIn.appendChild(inputQty);
  
  var inputQtyPlus = document.createElement('input');
  inputQtyPlus.setAttribute('type', 'image');
  inputQtyPlus.className = 'qtyPlus';
  inputQtyPlus.setAttribute('src', 'templates/paper_palace/images/buttons/buttonQty_plus.gif');
  inputQtyPlus.onclick = function() {
  	 document.getElementById('direct_order_quantity[' + fieldId + ']').value = parseInt(document.getElementById('direct_order_quantity[' + fieldId + ']').value)+1;
  	 return false;
  }
  divQtyIn.appendChild(inputQtyPlus);
  
  var inputQtyMinus = document.createElement('input');
  inputQtyMinus.setAttribute('type', 'image');
  inputQtyMinus.className = 'qtyMinus';
  inputQtyMinus.setAttribute('src', 'templates/paper_palace/images/buttons/buttonQty_minus.gif');
  inputQtyMinus.onclick = function() {
  	 if (document.getElementById('direct_order_quantity[' + fieldId + ']').value > 1) {
  	   document.getElementById('direct_order_quantity[' + fieldId + ']').value = parseInt(document.getElementById('direct_order_quantity[' + fieldId + ']').value)-1;
  	 }
  	 return false;
  }
  divQtyIn.appendChild(inputQtyMinus);
  
  divQty.appendChild(divQtyIn);
  div.appendChild(divQty);
  
  orderForm.appendChild(div);
  document.getElementById("boxCartDirectOrderInputs").appendChild(div);
}

function removeOrderRows(form_id) {
  var count_field = document.getElementById('count_order_rows');
  if (count_field) {
    old_number = count_field.value;
    if(old_number > 1) {
      var removeDiv = document.getElementById('div_'+old_number);
      if(removeDiv) {
        document.getElementById("boxCartDirectOrderInputs").removeChild(removeDiv);
        count_field.value = ((count_field.value*1)-1);
      }
    }    
  }
}


