
function nav(url, id) {
  //alert(id + " - " + url);
  urlCall('WebLinkClick.ashx?nav=' + encodeURI(url) + '&id=' + id + '&url=' + encodeURI(document.location.href));
  return true;
}

// Helper function to verify if value,text is present in selected list.
// This function is used by one2two() for verifying if the value being
// moved is already present in the destination.
function checkForPresenceInSelect2(optVal, selList) {
 for (i = 0; i < selList.length; i++) {
   if (selList.options[i].value == optVal) return true;
 }
 return false;
}

// Logic for moving a multi-select from element1 to element2
function one2two(element1, element2) {
 var memberList = document.getElementById(element1);
 var selectedList = document.getElementById(element2);
 var len = memberList.length;
 // Ignore any selections that are made to "------"
 for (var i = 0; i < len; ++i) {
   var currentOption = memberList.options[i];
   if (currentOption.selected == true) {
     currentOption.selected = false;
     if (currentOption.text == "------") {
       continue;
     }
     if (checkForPresenceInSelect2(currentOption.value, selectedList) == false) {
       selectedList.options[selectedList.length] = new Option(currentOption.text, currentOption.value);
     }
   }
 }
}

// moving a multi-select from element2 to element1.
// Actually this assumes that the values being moved are
// already present in element1 and ends up deleting
// the selected values in element2.
function two2one(element1, element2) {
 var selectedList = document.getElementById(element2);
 for (i = selectedList.length -1; i >= 0; i--) {
   if (selectedList.options[i].selected == true) {
     selectedList.options[i] = null;
   }
 }
}

function addAll(element1, element2) {
 var memberList = document.getElementById(element1);
 var selectedList = document.getElementById(element2);
  // clear list 2
  for (i = selectedList.length -1; i >= 0; i--) {
    selectedList.options[i] = null;
  }
  
  // hash table
  var alreadyAdded = new Object();
  
  // copy all
  for (var i = 0; i < memberList.length; ++i) {
   var currentOption = memberList.options[i];
   if (currentOption.text == "------") {
     continue;
   }
   // if (checkForPresenceInSelect2(currentOption.value, selectedList) == false) {
   if (alreadyAdded[currentOption.value] == null) {
     selectedList.options[selectedList.length] = new Option(currentOption.text, currentOption.value);
     alreadyAdded[currentOption.value] = currentOption.value;
   }
 }
}

function removeAll(element) {
  var selectedList = document.getElementById(element);
  // clear list 2
  for (i = selectedList.length -1; i >= 0; i--) {
    selectedList.options[i] = null;
  }
}

function selectCountry(lstCountryList, lstCountrySelection) {
 var countriesList = document.getElementById(lstCountryList);
 var countriesSel = document.getElementById(lstCountrySelection);

 for (var k = 0; k < countriesList.length; k++) {
   countriesList.options[k].selected = false;
 }
 for (var i = 0; i < countriesSel.length; i++) {
   var currentValue = countriesSel.options[i].value;
   for (var j = 0; j < countriesList.length; j++) {
     var currentItem = countriesList.options[j];
     if (currentItem.value == currentValue) {
       currentItem.selected = true;
       break;
     }
   }
 }
 return true;
}

function toggleCountrySelection(lstCountryList, lstCountrySelection, chkAllCountry) {
  updateUI(lstCountryList, lstCountrySelection, chkAllCountry);
}

function updateUI(lstCountryList, lstCountrySelection, chkAllCountry) {
  var allCountrySelected = document.getElementById(chkAllCountry).checked;
  var countriesList = document.getElementById(lstCountryList);
  var countriesSel = document.getElementById(lstCountrySelection);
  var cmdAdd = document.getElementById("cmdAdd");
  var cmdRemove = document.getElementById("cmdRemove");
  countriesList.disabled = allCountrySelected;
  countriesSel.disabled = allCountrySelected;
  cmdAdd.disabled = allCountrySelected;
  cmdRemove.disabled = allCountrySelected;
}


function expandcollapse(img, IdCell) {
	var cell = document.getElementById(IdCell);
	if (cell.className == 'linkbodycollapse') {
		cell.className = 'linkbodyexpand';
		img.src = '/images/minus.gif';
	} else {
		cell.className = 'linkbodycollapse';
		img.src = '/images/plus.gif';
	}
}
function voteOver(img) {
  if (img.className == '') {
    img.src = img.src.replace('happy', 'happy_h');
  }
}
function voteOut(img) {
  if (img.className == '') {
    img.src = img.src.replace('happy_h', 'happy');
  }
}

function vote(imgVote, idimgNotVote, voteValue, idLink) {
  if (imgVote.className == '') {
    // set the image hilighted
    imgVote.className = 'voted';
    var imgNotVote = document.getElementById(idimgNotVote);
    imgNotVote.className = '';
    voteOut(imgNotVote);
    voteSave(idLink, voteValue);
  } else {
    // remove the vote
    imgVote.className = '';
    voteOut(imgVote);
    voteSave(idLink, 0);
  }
}

// AJAX request
function voteSave(idLink, voteValue) {
  var url = "weblinkvote.ashx?idLink=" + idLink + "&value=" + voteValue;
  window.status = 'Voting...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.onreadystatechange = function() {voteDone();};
    req.open("GET", url, true);
    req.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.onreadystatechange = function() {voteDone();};
      req.open("GET", url, true);
      req.send();
    }
  }
} 

// AJAX request callback
function voteDone() {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) {
       window.status = req.responseText;
    } else {
       window.status= "Error while voting: " + req.statusText;
    }
  }
} 


// request with no callback
function urlCall(url) {
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.open("GET", url, false);
    req.send(null);
    // IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.open("GET", url, false);
      req.send();
    }
  }
} 

function LoginRedirect(url, action) {
  if (url == null) {
    url = window.location.href;
  }
  
  if (action == null) {
    action = 'This action';
  }
  
  if (confirm(action + ' required you to login to be performed.')) {
    window.location.href = 'http://support.genopro.com/logon.aspx?Page=' + encodeURI(url);
    return false;
  } else {
    return true;
  }
}