
var IE4 = (document.all) ? true : false;
var NS4 = (document.layers) ? true : false;
var weaselPos = 0;

function openAWindow( pageToLoad, winName, width, height, center) {
    xposition=100; yposition=100;
    if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
        xposition = (screen.width - width) / 2;
        yposition = (screen.height - height) / 2;
    }
    args = "width=" + width + ","
    + "height=" + height + ","
    + "location=0,"
    + "menubar=0,"
    + "resizable=1,"
    + "scrollbars=1,"
    + "status=0,"
    + "titlebar=0,"
    + "toolbar=0,"
    + "hotkeys=0,"
    + "screenx=" + xposition + ","  //NN Only
    + "screeny=" + yposition + ","  //NN Only
    + "left=" + xposition + ","     //IE Only
    + "top=" + yposition;           //IE Only

    window.open( pageToLoad,winName,args );
}

function calculateCharge() {
   total = document.chargeform.credits.value * 0.75;
   total = total.toFixed(2);
   document.chargeform.total.value = '$' + total;
}

function toggleall() {
  if (document.queueform.toggle.checked == true) {
     newset = true;
  }
  else {
     newset = false;
  }
  for (var i=0; i < document.queueform.length; i++) {
     if (document.queueform.elements[i].name.substr(0, 4) == 'zip_' || document.queueform.elements[i].name.substr(0, 10) == 'album_zip_') {
        document.queueform.elements[i].checked = newset;
     }
  } 
}

function togglealbum(idx) {
  ffield = eval('document.queueform.albumtoggle_' + idx);
  if (ffield.checked == true) {
     newset = true;
  }
  else {
     newset = false;
  }
  for (var i=0; i < document.queueform.length; i++) {
     if (document.queueform.elements[i].name.substr(0, 10) == 'album_zip_' && document.queueform.elements[i].name.indexOf(idx) == 10) {
        document.queueform.elements[i].checked = newset;
     }
  } 
}

function togglealbumlink(idx) {
  eval('document.queueform.albumtoggle_' + idx + '.checked = ! document.queueform.albumtoggle_' + idx + '.checked');
  togglealbum(idx);
}

function togglealllink() {
  document.queueform.toggle.checked = ! document.queueform.toggle.checked;
  toggleall();
}

function preparezip(credits) {
   document.queueform.action = 'downloadlauncher.php'; 
   var selectedsongs = 0;
   var albumsongs = 0;
   var albumarray = new Array();
   for (var i=0; i < document.queueform.length; i++) {
     //alert(document.queueform.elements[i].name.substr(0, 4));
     if (document.queueform.elements[i].name.substr(0, 4) == 'zip_' && document.queueform.elements[i].checked) {
        selectedsongs++;
     }
     else if (document.queueform.elements[i].name.substr(0, 10) == 'album_zip_' && document.queueform.elements[i].checked) {
        var parts = document.queueform.elements[i].name.split('_');
        if (albumarray[parts[2]] != 1) {
           selectedsongs += parseInt(parts[3]);
           albumarray[parts[2]] = 1;
        }
        albumsongs ++;
     }
   } 
   if (selectedsongs > credits) {
      alert("You don't have enough credits to download that many songs.");
   }
   else if (selectedsongs == 0 && albumsongs == 0) {
      alert("no songs selected");
   }
   else { 
      document.queueform.submit();
   }
}

function setqueue(idx) {
   eval("document.queue_" + idx + ".src = 'queuemanager.php?idx=" + idx + "&kicker=" + Math.random() + "'");
}

function removeFromQueue(idx) {
   if (confirm("are you certain you want to delete this?")) {
      document.queueform.songidx.value = idx;
      document.queueform.specialcommand.value = "delete";
      document.queueform.submit();
   }
}

function verifyCharge() {
   if (!document.chargeform.first.value || !document.chargeform.ccnum.value || !document.chargeform.cvv2.value ||
        !document.chargeform.last.value || !document.chargeform.address.value || !document.chargeform.city.value || 
        !document.chargeform.state.value || !document.chargeform.zip.value) {
      alert("Please fill out all the fields.");
   }
   else if (document.chargeform.credits.value < 6) {
      alert("the minimum order is 6 credits");
   }
   else {
      total = document.chargeform.credits.value * 0.75;
      total = total.toFixed(2);
      if (confirm("Do you want your credit card to be charged $" + total + "?")) {
         document.chargeform.chargebutton.value = "Processing...";
         document.chargeform.submit();
      }
   }
}

function verifyUpdate() {
   if (!document.signupform.oldpassword.value ||!document.signupform.first.value || 
        !document.signupform.last.value || !document.signupform.address.value || !document.signupform.city.value || 
        !document.signupform.state.value || !document.signupform.zip.value ||  !document.signupform.email.value) {
      alert("Please fill out all the fields.");
   }
   else if (document.signupform.email.value.indexOf('@') < 2 || document.signupform.email.value.length < 9) {
      alert("please enter a valid email address");
   }
   else if (document.signupform.oldpassword.value.length > 16 || document.signupform.oldpassword.value.length < 4 ||
             (document.signupform.newpassword.value.length > 0 && (document.signupform.newpassword.value.length > 16 || document.signupform.newpassword.value.length < 4))) {
      alert("login and password must be more than 4 and less than 16 characters");
   }
   else {
      document.signupform.submit();
   }
}

function verifySignup() {
   if (!document.signupform.userid.value || !document.signupform.password.value ||!document.signupform.first.value || 
        !document.signupform.last.value || !document.signupform.address.value || !document.signupform.city.value || 
        !document.signupform.state.value || !document.signupform.zip.value ||  !document.signupform.email.value) {
      alert("Please fill out all the fields.");
   }
   else if (document.signupform.email.value.indexOf('@') < 2 || document.signupform.email.value.length < 9) {
      alert("please enter a valid email address");
   }
   else if (document.signupform.userid.value.length > 16 || document.signupform.userid.value.length < 4 ||
             document.signupform.password.value.length > 16 || document.signupform.password.value.length < 4) {
      alert("login and password must be more than 4 and less than 16 characters");
   }
   else {
      document.signupform.submit();
   }
}
