var selectedPollId;
var cTOId, twid;
var winW = 630, winH = 460;
var obj;
var slideDiv;
var dx, dy;
var whereAmI, language;

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;
}
function focusControl(obj) { document.getElementById(obj).focus() }

function pDiv(strid) {
  var prtContent = document.getElementById(strid);
  var strOldOne = prtContent.innerHTML;
  var WinPrint = window.open("", "printer", "left=3000,top=3000,width=1,height=1,toolbar=0,scrollbars=0,status=0"); WinPrint.document.write(prtContent.innerHTML); WinPrint.document.close(); WinPrint.focus(); WinPrint.print(); WinPrint.close(); prtContent.innerHTML = strOldOne
}

function replaceAll(str, searchTerm, replaceWith, ignoreCase) {
  var regex = "/" + searchTerm + "/g"; if (ignoreCase) regex += "i"; return str.replace(eval(regex), replaceWith);
}

function cimage() { cAr("spnImg", "cImage.aspx", true, "<img src='ext/images/loading.gif' />"); }

function login() {
  var uName = document.getElementById("txtUsername").value;
  var uPwd = document.getElementById("txtPassword").value;
  var url = "eLogin.aspx?uName=" + uName + "&uPwd=" + uPwd;
  cAr("spnLoading", url, true, "Information control process&nbsp;<img src='ext/images/loading.gif' />", "contLogin()");
}
function uMProfile() {
  var oPwd = document.getElementById("txtOPwd").value;
  var rOPwd = document.getElementById("txtUPwd").value;
  if (oPwd != rOPwd) {
    alert("Old password is not correct.");
    return;
  }
  var rGMic = document.getElementById("txtGMic").value;
  var gmic = document.getElementById("txtSCode").value;
  if (rGMic != gmic) {
    alert("Security confirmation code is not correct.");
    return;
  }
  var nPwd = document.getElementById("txtPwd").value;
  var nPwd2 = document.getElementById("txtPwdC").value;
  if (nPwd != nPwd2) { alert("New password confirmation is not same."); return; }
  var url = "uMProfile.aspx?nPwd=" + nPwd;
  cAr("spnUProfile", url, true, "Information control process&nbsp;<img src='ext/images/loading.gif' />", "contUProfile()");
}

function contUProfile() {
  alert("Profile succesfully updated");
  goto("index.aspx");
}

function goto(link) {
  if (navigator.userAgent.indexOf("MSIE") != -1) window.navigate(link);
  else window.location = link;
}


function contLogin() {
  var cVar = document.getElementById("spnLoading").innerHTML;
  document.getElementById("spnLoading").innerHTML = "";
  if (cVar == "Logon failed") {
    alert("Invalid username or password, please try again carefully.");
    countdown("btnLogin", 10);
    disappearLoading();
  }
  else { afterLogin(cVar) }
}

function disappearLoading() {
  document.getElementById("spnLoading").innerHTML = "";
}
function afterLogin(uName) {
  window.location = "welcome.aspx";
}

// Countdown functions begin
var countdownObject, countdownTimer;
function countdown(obj, ctime) { //obj= obj id of disabled, ctime= how much time to be disabled
  countdownObject = document.getElementById(obj);
  countdownObject.value = ctime;
  countdownObject.disabled = true;
  countdownTimer = setInterval("controlCountdown()", 1000);
}
function controlCountdown() {
  var pos = countdownObject.value;
  pos--;
  countdownObject.value = pos;
  if (pos == 0) {
    clearInterval(countdownTimer);
    countdownObject.disabled = false;
    countdownObject.value = "Enter";
  }
}
// Countdown functions end

// poll vote operations starts
var voteDivHtml;
function setOver(val, spnObj, pid) {
  var i, obj, objId;
  voteDivHtml = document.getElementById(spnObj).innerHTML;
  for (i = 0; i < 5; i++) {
    objId = "imgV" + pid + i;
    obj = document.getElementById(objId);
    if (i <= val) obj.src = "ext/images/star1.gif";
    else obj.src = "ext/images/star0.gif";
  }
}

function setOut(spnObj) {
  document.getElementById(spnObj).innerHTML = voteDivHtml;
}

function setClick(pId, val, spnObj) {
  val++;

  cAr(spnObj, "intrapopularity.aspx?pQ=" + val + "&pid=" + pId, true, "<img src='ext/images/loading.gif' />");
}
// poll vote operations ends

// obj position operations starts
function setCenter(obj) {
  var divObj = document.getElementById(obj);
  divObj.style.verticalAlign = "middle";
  divObj.style.textAlign = "center";
}
function setNormal(obj) {
  var divObj = document.getElementById(obj);
  divObj.style.verticalAlign = "top";
  divObj.style.textAlign = "left";
}
// obj position operations ends

function expand(a, b, c) {
  var mode;
  mode = document.getElementById(b).style.display == "block" ? 1 : 0;

  if (mode == 0) {
    document.getElementById(a).src = "ext/images/minus.gif";
    document.getElementById(b).style.display = "block";
    cAr(b, c, true, "<img src='ext/images/loading.gif' />&nbsp;loading..", "resetPage()");
  }
  else {
    document.getElementById(a).src = "ext/images/plus.gif";
    document.getElementById(b).style.display = "none";
    resetPage();
  }

}
function resetPage() {
  document.getElementById("contentImg").style.height = document.getElementById("contentTbl").offsetHeight + 5;
}

// user poll controls
function cPoll(id) {
  cAr("spn_mp", "closePoll.aspx?pid=" + id, true, "Closing the poll to voting..&nbsp;<img src='ext/images/loading.gif' />", "contCPoll()");
}
function oPoll(id) {
  cAr("spn_mp", "openPoll.aspx?pid=" + id, true, "Opening the poll to voting..&nbsp;<img src='ext/images/loading.gif' />", "contCPoll()");
}
function rPoll(id) {
  cAr("spn_mp", "resetPoll.aspx?pid=" + id, true, "Changing the poll default values..&nbsp;<img src='ext/images/loading.gif' />", "contCPoll()");
}
function contCPoll() {
  var obj = document.getElementById("spn_mp");
  if (obj.innerHTML == "false") {
    obj.innerHTML = "Problem occured.";
  }
  else {
    obj.innerHTML = "Process completed.";
  }
}

function dPoll(id) {
  cAr("spn_mp", "deletePoll.aspx?pid=" + id, true, "Erasing the poll..&nbsp;<img src='ext/images/loading.gif' />", "contDPoll()");
}
function contDPoll() {
  var obj = document.getElementById("spn_mp");
  if (obj.innerHTML == "false") {
    obj.innerHTML = "Problem occured, deleting the poll."; /// <reference path="../../pollapoll.aspx.cs" />

  }
  else {
    obj.innerHTML = "Poll deleted.";
  }
}
function hideContents() {
  var obj = document.getElementById("overLayer");
  obj.style.opacity = "0.5";
  obj.style.MozOpacity = "0.5";
  obj.style.KhtmlOpacity = "0.5";
  obj.style.filter = "alpha(opacity=50)";
  document.getElementById("overLayer").style.width = document.body.scrollWidth;
  document.getElementById("overLayer").style.height = document.body.scrollHeight;
  document.getElementById("overLayer").style.display = "block";
}

function setLightBox() {
  var lBHeight, lBWidth;
  lBHeight = document.getElementById("lightboxHeight").offsetHeight;
  lBWidth = document.getElementById("lightboxWidth").offsetWidth;
  document.getElementById("imgLBTop").style.width = lBWidth;
  document.getElementById("imgLBBottom").style.width = lBWidth;
  document.getElementById("imgLBLeft").style.height = lBHeight;
  document.getElementById("imgLBRight").style.height = lBHeight;
}

function expand2(a, b) {
  var slideDiv = document.getElementById("tblLightbox").style;
  slideDiv.top = document.body.scrollTop + 30;
  document.getElementById("tblLightbox").style.display = "block";
  document.getElementById("flshObj").style.display = "none";
  hideContents();
  var tmpFunc = "";
  if (a == "userdetails.aspx") tmpFunc = ";cimage()";
  cAr(b, a, true, "<img src='ext/images/loading.gif' style='width:320;height:250' />", "setLightBox();resetPage();resetShare()" + tmpFunc);
}

function closeDiv(div_obj) {
  document.getElementById(div_obj).style.display = "none";
  document.getElementById("overLayer").style.display = "none";
  document.getElementById("flshObj").style.display = "block";
  resetPage();
}
function gotoCurrCoor(obj) {
  document.body.scrollTop = document.getElementById(obj).offsetTop - 17;
}

function sWCode(id) {
  cAr("spn_mp", "sendwidgetcode.aspx?pid=" + id, true, "Sending widget code..&nbsp;<img src='ext/images/loading.gif' />", "contWCPoll()");
}
function contWCPoll() {
  var obj = document.getElementById("spn_mp");
  if (obj.innerHTML == "false") {
    obj.innerHTML = "Problem occured, sending mail.";
  }
  else {
    obj.innerHTML = "The Poll widget code was sent to your e-mail.";
  }
}

function resetShare() {
  setCenter("tdShareIt");
  cAr("tdShareIt", "sharee.aspx", true, "<img src='ext/images/loading.gif' /><br /><br />loading..", "setNormal('tdShareIt')");
}
function share(url) {
  var x;
  var y;
  x = (screen.width - 600) / 2;
  y = (screen.height - 600) / 2;
  window.open(url, "", "toolbar=no;location=no,left=" + x + ",top=" + y + ",width=600,height=600,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no")
}
function add2Favorites(url) {
  var bookmarkurl = url;
  var bookmarktitle = document.title;
  var nonie = 'Works both Mozilla Firefox and Internet Explorer.';

  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
  } else if (document.all) { // IE Favourites
    window.external.AddFavorite(bookmarkurl, bookmarktitle);
  } else {
    alert(nonie);
  }
}

function votediv(id, pDId) {
  var i, j, k;
  var optionId, questionCount, optionCount, questionType, strRetVal;
  twid = document.getElementById("twid").value;
  strRetVal = id + "^";
  questionCount = document.getElementById("hiddenQuestionCount").value;
  k = 0;
  for (i = 1; i <= questionCount; i++) {
    questionType = document.getElementById("hiddenQuestionType" + i).value;
    optionCount = document.getElementById("hiddenOptionCount" + i).value;
    for (j = 0; j <= optionCount-1; j++) {
      if (questionType == "True") {
        optionId = "cb" + i + j;
        if (document.getElementById(optionId).checked) {
          k = document.getElementById(optionId).value;
          if (IsNumeric(k))
            strRetVal += k + "~";
          else {
            optionId = "txt" + i + j;
            strRetVal += document.getElementById(optionId).value + "~";
          }
        }
      }
      else {
        optionId = "rb" + i;
        if (document.all(optionId)[j].checked) {
          k = document.all(optionId)[j].value;
          if (IsNumeric(k))
            strRetVal += k + "~";
          else {
            optionId = "txt" + i;
            strRetVal += document.getElementById(optionId).value + "~";
          }
        }
      }
    }
    strRetVal += "^";
  }
  cAr(pDId, "intravote.aspx?vD=" + strRetVal + "&wid=" + twid, true, "<img src='ext/images/loading.gif' style='width:320;height:250' />", "resetPage();");
}


function opacity(el, opacStart, opacEnd, millisec) {
  //speed for each frame
  var speed = Math.round(millisec / 100);
  var timer = 0;
  obj = el;
  obj.style.layerBackgroundColor = "#808080";
  //determine the direction for the blending, if start and end are the same nothing happens
  if (opacStart > opacEnd) {
    for (i = opacStart; i >= opacEnd; i--) {
      setTimeout("changeOpac(" + i + ")", (timer * speed));
      timer++;
    }
  } else if (opacStart < opacEnd) {
    for (i = opacStart; i <= opacEnd; i++) {
      setTimeout("changeOpac(" + i + ")", (timer * speed));
      timer++;
    }
  }
}

//change the opacity for different browsers
function changeOpac(opacity) {
  obj.style.opacity = (opacity / 100);
  obj.style.MozOpacity = (opacity / 100);
  obj.style.KhtmlOpacity = (opacity / 100);
  obj.style.filter = "alpha(opacity=" + opacity + ")";
}
function getPage() {
  switch (whereAmI) {
    case 1:
      {
        if (language == 0)
          window.location = "newpolltr.aspx";
        else
          window.location = "newpoll.aspx";
        break;
      }
    case 2:
      {
        if (language == 0)
          window.location = "newusertr.aspx";
        else
          window.location = "newuser.aspx";
        break;
      }
    case 3:
      {
        if (language == 0)
          window.location = "pollapolltr.aspx";
        else
          window.location = "pollapoll.aspx";
        break;
      }
    case 4:
      {
        if (language == 0)
          window.location = "reverifytr.aspx";
        else
          window.location = "reverify.aspx";
        break;
      }
    case 5:
      {
        if (language == 0)
          window.location = "verifytr.aspx";
        else
          window.location = "verify.aspx";
        break;
      }
    case 6:
      {
        if (language == 0)
          window.location = "welcometr.aspx";
        else
          window.location = "welcome.aspx";
        break;
      }
    case 7:
      {
        if (language == 0)
          window.location = "fslastaddedpollstr.aspx";
        else
          window.location = "fslastaddedpolls.aspx";
        break;
      }
    case 8:
      {
        if (language == 0)
          window.location = "fsmemberpollstr.aspx";
        else
          window.location = "fsmemberpolls.aspx";
        break;
      }
    case 9:
      {
        if (language == 0)
          window.location = "fsmostpopularpollstr.aspx";
        else
          window.location = "fsmostpopularpolls.aspx";
        break;
      }
    case 10:
      {
        if (language == 0)
          window.location = "fsmostvotedpollstr.aspx";
        else
          window.location = "fsmostvotedpolls.aspx";
        break;
      }
    case 11:
      {
        if (language == 0)
          window.location = "fsmostvisitedpollstr.aspx";
        else
          window.location = "fsmostvisitedpolls.aspx";
        break;
      }
    default:
      {
        if (language == 0)
          window.location = "indextr.aspx";
        else
          window.location = "index.aspx";
        break;
      }
  }
}
function controlOpenEnded(obj, questionNumber) {
  if (obj == "nop") obj.checked = false;
  if (obj.checked) {
    showDocDiv("spnOpenEndedTBox" + questionNumber);
    hideDocDiv("spnOpenEndedLabel" + questionNumber);
  }
  else {
    showDocDiv("spnOpenEndedLabel" + questionNumber);
    hideDocDiv("spnOpenEndedTBox" + questionNumber);
  }
}
function hideDocDiv(objId) { if (objId) document.getElementById(objId).style.display = "none"; }
function showDocDiv(objId) { if (objId) document.getElementById(objId).style.display = "block"; }

