// global variables
var c = 1;
var c2 = 1;
var c3 = 1;
var inviteClick = 0;
var answerClick = 0;
var hideClick = 0;
var cartClick = 0;
var cancelClick = 0;
var blinker = null;
var blinker2 = null;
var blinker3 = null;
var blinker4 = null;
var unlockClick = 0;
var clearBlinker2 = 0;
var clearBlinker3 = 0;
var clearBlinker4 = 0;
var currentUnlockHTML = '';
var currentUnlockHTML2 = '';

function getBaseDomain() {
  var href = document.location.href;
  var parts = href.split("\/");
  var domain = parts[2];
  return domain;
}

function getBaseUrl() {
  return location.protocol + '//' + getBaseDomain();
}

function getRandomVal(valLength) {
  var chars = 'abcdefghijklmnopqrstuvwxyz';
  var charLength = chars.length;
  var val = '';
  while(val.length < valLength) {
    var rIndex = Math.floor(Math.random() * charLength);
    val += chars.charAt(rIndex);
  }
  return val;
}

function RequestGET(url, func, asyncType) {
  var xmlhttp = null;
  if(document.all) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {
	xmlhttp = new XMLHttpRequest();
  }
  // xmlhttp.async=asyncType
  xmlhttp.onreadystatechange = function(){ func(xmlhttp); };
  xmlhttp.open("GET", url, true);
  xmlhttp.send(null); // send document if needed
  return xmlhttp;
}

function handleCancelSubscription(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  var el = document.getElementById('bonu');
	  if(el) { el.innerHTML = 'Unlimited Tokens will end on the following date:'; }
	  el = document.getElementById('bonu2');
	  if(el) { el.innerHTML = ''; }
	  alert("Your request for cancellation has been noted. You will receive a confirmation email within the next few hours noting the cancellation.\n\nThanks for your support!"); 
	}
	cancelClick = 0;
  }
}

function adjustbonus() {
  clearTimeout(blinker3);
  clearTimeout(blinker4);
  var lyr = document.getElementById('unlock_link');
  if(lyr) { lyr.innerHTML = '<small><b>ALL TRACKS ARE AVAILABLE<b></small>'; }
}

function handleUnlockAll(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  var newlyrHTML = '';
	  var newlyrHTML2 = '';
	  if(res == -1) {
	    alert("You don't have enough tokens to unlock all tracks.");
		newlyrHTML = currentUnlockHTML;
		newlyrHTML2 = currentUnlockHTML2;
	    clearBlinker3 = 1;
        var lyr = document.getElementById('unlock_link');
        if(lyr) { lyr.innerHTML = newlyrHTML; }
        lyr = document.getElementById('unlock_link2');
        if(lyr) { lyr.innerHTML = newlyrHTML2; }
	  }
	  else if(res == 1) {
        setTimeout("duz(-1)",500);
		clearBlinker3 = 1;
	  }
	}
	cancelClick = 0;
  }
}

// invite friend
function CancelSubscription(date) {
  if(cancelClick == 0) {
    if(confirm('Are you sure you want to cancel your recurring subscription?\n\nYour Unlimited Tokens will expire on '+date)) {
      var url = getBaseUrl() + '/nosub.php?val=' + getRandomVal(15);
	  cancelClick++;
      RequestGET(url, handleCancelSubscription, true);
	}
  }
}

function doBlinkMsg3(txt) {
  if(!clearBlinker3) {
    var n = txt;
	if(c3 > 6) n = 'Verifying Tokens ...';
    var lyr = document.getElementById('unlock_link');
    var txt = (c3++ % 2 == 0) ? '<BR>' : txt;
    lyr.innerHTML =  txt;  
    blinker3 = setTimeout("doBlinkMsg3('"+n+"')", 500);
  }
  else {
    clearBlinker3 = 0;
  }
}

function updateUnlockLyr() {
  clearTimeout(blinker3);
  doBlinkMsg3('Verifying Tokens ... ');
}

function doBlinkMsg4(txt,x) {
  if(!clearBlinker3) {
    var n = txt;
    var nn = x;
    var lyr = document.getElementById('unlock_link_'+x);
    var txt = (c3++ % 2 == 0) ? '<BR>' : txt;
    lyr.innerHTML =  txt;
    blinker3 = setTimeout("doBlinkMsg4('"+n+"','"+nn+"')", 500);
  }
  else { clearBlinker3 = 0; }
}

function updateUnlockLyr2(n) {
  clearTimeout(blinker3);
  doBlinkMsg4('Verifying Tokens ... ',n);
}

function buildURLDataString(data) {
  var urlArray = [];
  var index = 0;
  for(var x in data) {
    urlArray[index++] = x+'=' + encodeURIComponent(data[x]);
  }
  return urlArray.join('&');
}

function handleSubmitReview(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  if(res == 1) {
	    //alert("Review Submitted!");
	  }
	  else if(res == 0) {
	    alert("There was an error submitting your review. Please try again later.");
	  }
	  else {
	    alert("ERROR NUMBER: e0000-"+res);
	  }
	}
  }
}

// submit review
function SubmitReview(data) {
  var dataStr = buildURLDataString(data);
  var url = getBaseUrl() + '/comment.php?'+dataStr+'&val=' + getRandomVal(15);
  RequestGET(url, handleSubmitReview, true);
}

// unlock all
function UnlockAll(hid,z) {
  var c = false;
  if(unlockClick == 0) {
    unlockClick++;
    if(!li) {
      alert("You must be logged in to unlock this content.");
    }
    else if(!ALLUNLOCKED&&mt<umtt) {
      var n = umtt - mt; var s = (n == 1) ? '' : 's';
	  c = confirm("You do not have enough tokens to unlock.\nYou currently need "+n+" more token"+s+" to unlock this content.\n\nClick OK to buy more tokens now."); 
    }
    else if(!ALLUNLOCKED) {
      if(!z) { showProductDetail(0); }
      var lyr = document.getElementById('unlock_link');
	  if(lyr) { currentUnlockHTML = lyr.innerHTML; }
	  lyr = document.getElementById('unlock_link2');
	  if(lyr) { currentUnlockHTML2 = lyr.innerHTML; lyr.innerHTML = ''; }
	  doBlinkMsg3('Connecting To Download Server ...');
      var url = getBaseUrl() + '/unlocktrackgroup.php?hid=' + encodeURIComponent(hid) + '&val=' + getRandomVal(15);
      RequestGET(url, handleUnlockAll, true);
	}
  }
  unlockClick = 0;
  if(c) { window.location="http://www.mixtapepass.com/buy/"; }
}

function handleInviteFriend(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  var el = document.getElementById('invitesleft');
	  var msg = '';
	  if(res.indexOf('|') > -1) {
	    var parts = res.split(/\|/);
	    var invites = parts[0];
	    msg = parts[1];
		if(blinker != null) { clearTimeout(blinker); }
	    if(el) { el.innerHTML = invites + ' Invites Left Today'; }
        var form = document.forms['friend'];
        form.elements['EmailAddress'].value = '';
	  }
	  else {
	    msg = res; 
	  }
	  alert(msg);
	  if(msg == 'You are no longer logged in!') {
	    window.location = 'https://www.mixtapepass.com/login.php';
	  }	  
	}
	else {
	  if(blinker != null) { clearTimeout(blinker); }
	  alert('There was a problem with this feature.\nThe webmaster has been notified.');
	}
	inviteClick = 0;
  }
}

// invite friend
function InviteFriend() {
  if(inviteClick == 0) {
    var form = document.forms['friend'];
    var email = form.elements['EmailAddress'].value;
    if(email == '') { alert('The email address you entered is invalid.'); return; }
    var url = getBaseUrl() + '/refer_friend.php?type=referfriend&EmailAddress=' + encodeURIComponent(email) + '&val=' + getRandomVal(15);
	inviteClick++;
    doBlinkMsg();
    RequestGET(url, handleInviteFriend, true);
  }
}

function handleUpdateTokens(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  var parts = res.split(/\|/);
	  mt = parts[0];
	  var points = parts[1];
	  var ul = parts[2];
	  //if(!ut) {
	    var dmt = (ul == 1) ? 'UNLIMITED' : mt;
	    var tokenlyr = document.getElementById('member_tokens');
	    if(tokenlyr) { tokenlyr.innerHTML = '<b>'+dmt+'</b>'; }
	  //}
	  var pointlyr = document.getElementById('member_points');
	  if(pointlyr) { pointlyr.innerHTML = '<b>'+points+'</b>'; }
	}
  }
}

function handleHideInstructions(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  var parts = res.split(/\|/);
	  alert("You will no longer be bothered by the Download Instructions.\nYou can enable them at anytime by editing your profile settings.");
	  var instructionsLyr = document.getElementById('mcc');
	  instructionsLyr.style.display = 'none';
	}
  }
}

function hideInstructions() {
  if(hideClick == 0) {
    var url = getBaseUrl() + '/nomoreinstructions.php?val=' + getRandomVal(20);
    RequestGET(url, handleHideInstructions, true);
	hideClick++;
  }
}

// do cart
function updateTokens() {
  downloadClick = 0;
  var url = getBaseUrl() + '/get_tokens.php?val=' + getRandomVal(20);
  RequestGET(url, handleUpdateTokens, true);
}

function doBlinkMsg() {
  var lyr = document.getElementById('invitesleft');
  var txt = (c++ % 2 == 0) ? '<BR>' : '<i><font color="#CC0000">Sending Invite...</font></i>';
  lyr.innerHTML =  txt;
  blinker = setTimeout("doBlinkMsg()", 500);
}

function doBlinkMsg2() {
  var lyr = document.getElementById('surveyform');
  var txt = (c2++ % 2 == 0) ? '<BR><BR><BR><BR><BR>' : '<i><font color="#CC0000">Placing Your Vote...<BR><BR><BR><BR><BR></font></i>';
  lyr.innerHTML =  txt;
  blinker2 = setTimeout("doBlinkMsg2()", 500);
}

function handleDoSurvey(xmlhttp) {
  var status = xmlhttp.readyState;
  if(status == 4) {
    if(xmlhttp.status == 200) {
      var res = xmlhttp.responseText;
	  var parts = res.split(/\|/);
	  var memberpoints = parts[0];
	  var msg = parts[1];
	  if(blinker2 != null) { clearTimeout(blinker2); }
	  if(memberpoints == -1) {
	    window.location="http://www.mixtapepass.com/login.php";
		return;
	  }
	  else if(memberpoints != '0') {
	    var memberptslyr = document.getElementById('member_points');
		if(memberptslyr) {
		  memberptslyr.innerHTML = memberpoints;
		}
	  }
	  var surveyform = document.getElementById('surveyform');
	  if(surveyform) {
	    surveyform.innerHTML = msg;
	  }
	}
	answerClick = 0;
  }
}

// do survey
function doSurvey(form) {
  if(answerClick == 0) {
    var answerVal = '';
    var answer = form.elements['Answer'];
	if(answer.length) {
	  for(x=0; x<answer.length; x++) {
	    if(answer[x].checked) {
		  answerVal = answer[x].value;
		  break;
		}
	  }
	}
	else {
	  answerVal = answer.value;
	}
	var sid = form.elements['sid'].value;
    if(answerVal == '') { alert('You must select an answer before voting!.'); return; }
    var url = getBaseUrl() + '/survey.php?type=dosurvey&SurveyId=' + sid + '&Answer=' + encodeURIComponent(answerVal) + '&val=' + getRandomVal(15);
	answerClick++;
    doBlinkMsg2();
    RequestGET(url, handleDoSurvey, true);
  }
}
