function targetLink(link) {
  var url = (link.getAttribute) ? link.getAttribute('href') : link.href;

  if (!url) return true;

  targetWin=open(url,"popup1","width=550,height=400,scrollbars=yes,resizable=yes");

  if (targetWin) {
    targetWin.resizeTo(550,400);
    targetWin.focus();
    return false;
  }

  return true;
}

function targetLink3(link) {
  var url = (link.getAttribute) ? link.getAttribute('href') : link.href;

  if (!url) return true;

  targetWin=open(url,"popup3","width=840,height=500,scrollbars=yes,resizable=yes");

  if (targetWin) {
    targetWin.resizeTo(840,500);
    targetWin.focus();
    return false;
  }

  return true;
}

function targetLinkPayment(link) {
  var url = (link.getAttribute) ? link.getAttribute('href') : link.href;
  if (!url) return true;
  targetWin=open(url,"popupLinkPayment","width=840,height=760,scrollbars=yes,resizable=yes");

  if (targetWin) {
    targetWin.resizeTo(840,760);
    targetWin.focus();
    return false;
  }

  return true;
}

function targetPopup(url) {
  opener.open(url,"popup1","width=550,height=400,scrollbars");
}

var global=0;
var flag=0;

/**
 * Checks or unchecks all checkboxes with the given name depending on the
 * checked status of the triggering checkbox.
 *
 * @param formId
 *            the id of the form that contains the checkboxes.
 * @param checkboxName
 *            the name of the checkboxes that should be checked.
 * @param checkallBox
 *            the checkbox object that triggered this method.
 */

function checkAll(formId, checkboxName, checkAllBox) {
  var form = $(formId);
  var checkboxes = form.getInputs('checkbox', checkboxName);
  var checked = $(checkAllBox).checked;

  for (i = 0, n = checkboxes.length; i < n; i++) {
    checkboxes[i].checked = checked;
  }

}

function uncheckCheckAllBox(checkbox, checkAllBox) {

  if (!checkbox.checked)
    $(checkAllBox).checked = false;

}

function toggle(formId, checkboxClassName) {

  if (global == 0)
    global = 1;
  else
    global = 0;

  checkAll(document.getElementById(formId), checkboxClassName, global);
}

function aToggle(formId, checkboxClassName) {
  checkStatus(document.getElementById(formId), checkboxClassName);

   if(flag == 0) {
     document.getElementById(formId).checkall.checked=false;
     global = 0;
   }
   else {
     document.getElementById(formId).checkall.checked=true;
     global = 1;
   }

}

function checkStatus(form, cName) {

   for (i = 0, n = form.elements.length; i < n; i++) {

     if (form.elements[i].className.indexOf(cName) !=-1) {

       if(form.elements[i].checked == true) {
         flag=1;
       }
       else {
         flag=0;
         return;
       }

     }

  }

}

function dynamicSelect(id1, id2) {
	// Browser and feature tests to see if there is enough W3C DOM support
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);

	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options
		var clonedOptions = clone.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options
		// in the dynamic select box
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display
		// the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		};

	}

}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	while (sel2.options.length) {
		sel2.remove(0);
	}

	// Create regular expression objects for "select" and the value of the
	// selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");

	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals
		// the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the
			// dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}

	}

}

  /**
   * shows or hides the shortdescription with the given id.
   */

  function toggleDescription(trId, descriptionId, colorActive, colorInActive) {
    $(descriptionId).toggle();

    if($(descriptionId).visible()) {
      $(trId).setStyle({'background': colorActive});
    }
    else {
      $(trId).setStyle({'background': colorInActive});
    }

  }


  /**
	 * set the bgcolor of the given given element
	 */

  function setListRowColor(trId, color) {
      $(trId).setStyle({'background': color});
  }


  /**
	 * reset the bgcolor of the given given element
	 */

  function resetListRowColor(trId, decriptionId, color) {

	currentRfqId = trId.slice(3, trId.indexOf('c'));
	if(typeof(rfqIdChildrenList) != "undefined") {
		 for (var i = 0, len = rfqIdChildrenList.length; i < len; i++) {
		 	 if(rfqIdChildrenList[i] == currentRfqId){
		 	 	color="#cee2ec";
		 	 }
		 }
	}

    if($(decriptionId) && $(decriptionId).visible() == false) {
      $(trId).setStyle({'background': color});
    }
  }

  function resetListRowColorAccount(trId, color) {
 		$(trId).setStyle({'background': color});
  }

  function popupBack(url){

	  	if(url == "_self"){
	  		window.opener.location.href = window.opener.location.href;
	  	}
	  	else{
	  		window.opener.location.href = url;
	  	}
		self.close();
	}

/* ******************************************************************************** */
/* changing the background color when mouse over, method will add a new class hover */
/* *************   in all td classes in the given row context ********************* */

function setBackgroundColor(row) {
  $(row).select('td[class]').each(function(element) {
    element.addClassName('list-hover');
  });
}


/* ******************************************************************** */
/* changing the background color when mouse out, remove the class hover */

function unsetBackgroundColor(row) {
  $(row).select('td[class]').each(function(element) {
    element.removeClassName('list-hover');
  });
}

/**
 * If the given event was a key event the char code of the used
 * key is returned.
 * @param Event evt
 * @return int
 */
function getCharCode(evt) {

  if (!evt) {
    return -1;
  }

  return (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
}

