var strHref = window.location.href;
var JSsitepath = strHref.substr(0,strHref.indexOf(".com")+4).toLowerCase();
var doSubmit = false;
function OpenOnlineRadio (plist,regtype){
	window.open ("/radio/radio_mmp3g.html?thecategoryid=" + plist +"&requesttype=" + regtype, "OnlineRadio", "width=600,height=200")
}
//====================================================================================//
// Function : notLogged 
// Purpose  : Can't access the link if not logged.
//====================================================================================//	
function notLogged(){
	alert("You must activate your account in to use the EZ-Tracks website!\nPlease submit the form below and click YES! to continue.");
}

//====================================================================================//
// Function : clearValue 
// Purpose  : Clear the value already in the Field
//====================================================================================//		
var myflag=0;
function clearValue(myField){
	if(myflag==0){
		document.loginform[myField].value="";
		myflag=1;
	}	
}

//====================================================================================//
// Function : LoginRegister 
// Purpose  : Opens a window to let the User Login or Register
//====================================================================================//		
function LoginRegister(){
	w = 300;
	h = 340;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = "height="+h+",width="+w+",top="+TopPosition+",left="+LeftPosition+",scrollbars=1,resizable=1"
	window.open( "/login/index.html", "LoginRegister", settings );		
}

function checkloginbox(domainid)
{
	
	if(domainid==48)
	{
		myheading="Cell Number";
		myheading2="Pin";
	}
	else if(domainid==19) 
	{
		myheading="Phone Number";
		myheading2="Password";
	}
	else
	{
		myheading="Email";
		myheading2="Password";
	}
	
	if (document.loginform.email.value=="")
	{
		alert("Please enter your "+myheading+".")
		return false;
	}
	
	if (document.loginform.password.value=="")
	{
		alert("Please enter your "+myheading2+".")
		return false;
	}
}

function doKeyCheck(el) {
	if(el.value.length == 3) {
		if(el.name == "cell1") {
			document.loginform.cell2.focus();
		} else if(el.name == "cell2") {
			document.loginform.cell3.focus();
		}
	}
}

function fetchmultipleDDselection(thedd) { 
	myddIDs="";
	for (i=0; i<document.getElementById(thedd).options.length; i++) { 
		if (document.getElementById(thedd).options[i].selected == true) 
		{ 
			if (myddIDs=="")
				myddIDs=document.getElementById(thedd).options[i].value
			else
				myddIDs=myddIDs+","+document.getElementById(thedd).options[i].value	
		} 
	} 
	return myddIDs
} 

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function hideElement(el) {
	document.getElementById(el).style.display = "none";
	return false;
}

// function for ringtones lightbox
function yesSendRingtone(url) {
	CallAhah();
	return false;
}

function requestObject(){
	try{
		var http_request = new XMLHttpRequest(); /* e.g. Firefox */		
	}
	catch(e){
		try {
			var http_request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		} 
		catch (e){
			try{
				var http_request = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
			} 
			catch (e){
				try{
					var http_request = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
				} 
				catch (E){
					var http_request = false;
				}
			}
		}
	}
	return http_request;
}
function artistBiographyExpand(artistid){
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/includes/getBio.cfm', true);
	if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml; charset=iso-8859-1');
	} 
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	 
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4){
			if(http_request.status == 200){
				if(trim(http_request.responseText) != ""){
					document.getElementById("artistBiography").innerHTML=trim(http_request.responseText);
				}
			}
		}
	}
	http_request.send('artistid='+artistid);
}
function getCatChart(page, objvalue){
	if (objvalue!=0){
		window.location=page + "-" + objvalue + ".html";
	}
	else
		window.location=page + ".html";
}
function getSubCatChart(page,objvalue1,objvalue2){
	if (objvalue1!=0 && objvalue2!=0){
		window.location=page + "-" + objvalue1 + "-" + objvalue2;
	}
	else
		window.location=page + ".html";
}
function UpdateOnPage(NewUserName, MyGendre, MyArtist, AvatarID){
	if(doSubmit){
		document.getElementById('userName').innerHTML=NewUserName;	
		if(document.getElementById('profileUserName')) {
			document.getElementById('profileUserName').innerHTML=NewUserName;
			var http_request = requestObject();
			http_request.open('POST', JSsitepath+'/UserProfileData.cfm', true);
			http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http_request.onreadystatechange = function(){
				if (http_request.readyState == 4){
					if(http_request.status == 200){
						var trimedHttp_request = trim(http_request.responseText)
						if(trim(http_request.responseText) != ""){
							var responseParams = trimedHttp_request.split( "|" )
							document.getElementById('FavGenre').innerHTML=responseParams[0];
							document.getElementById('MyAvatar').src="http://images.massivemp3giveaway.com/avatar/133/" + responseParams[1];
							document.getElementById('FavArtist').innerHTML=MyArtist;
						}
					}
				}
			}
			http_request.send('CategoryIdList=' +MyGendre + '&AvatarID=' +AvatarID);
		}
		hideLightBox();
		return false;
	} else {
		if(trim(document.getElementById('lookupresult').innerHTML) == "Please pick a Username that is not your phone number.") {
			alert("Please pick a Username that is not your phone number.");
			return false;
		} else {
			alert("The username you have chosen is already in use.\n Please choose a new one.");
			return false;
		}
	}
}

function dochangeavatar(Avatarvalue)
{
	var myavatarvalue = Avatarvalue.split("|");
	document.getElementById("imgavatar").src="http://images.massivemp3giveaway.com/avatar/133/"+myavatarvalue[1];
	return false;
}

function fetchavatarid()
{
	mystring=document.getElementById("avatarID")[document.getElementById("avatarID").selectedIndex].value;
	var myavatarvalue = mystring.split("|");
	return myavatarvalue[0];
}

function CallAhah(url, method, dest, cursr) {	// url = page you want to call, method = GET or POST, dest = id of element you want the response to be in
	var ajaxPost;
	
	if(method == null || method == "") {
		method = "GET";
	}
	
	if(dest == "") {
		dest = null;
	}
	
	try	{
		// Initialize all current browsers
		ajaxPost = new XMLHttpRequest();
	}
	catch(tryms) {
		try	{
			// Initialize Internet Explorer
			ajaxPost = new ActiveXObject('msxml2.XMLHTTP');
		}
		catch(tryotherms) {
			try {
				// Initialize older Internet Explorer
				ajaxPost = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(failed){
				alert('XmlHttp not supported');
				return false;
			}
		}
	}
	
	if (url.indexOf("?") >= 0) {
		url = url + "&rand=" + getRandom();
	} else {
		url = url + "?rand=" + getRandom();
	}
	
	var ReqParams = new String("");
	
	if (url.indexOf("?") >= 0 && method.toUpperCase() == "POST") {
		ReqParams = url.substring(url.indexOf("?") + 1, url.length);
		url = url.substring(0, url.indexOf("?"));
	}
	
	ajaxPost.onreadystatechange=function(){
		if(ajaxPost.readyState == 4){
			if(ajaxPost.status == 200){
				if(dest != null) {
					document.getElementById(dest).innerHTML = unescape(ajaxPost.responseText);
					if(dest == "LightBox") {
						showLightBox();	
					}
				} else {
					eval(ajaxPost.responseText);
				}
				if(cursr != null && cursr != "") {
					document.body.style.cursor = cursr;
				}
			} else {
				if(dest != null) {
					alert("Ajax Error!");
				}
			}
		}
	}
	
	ajaxPost.open(method.toUpperCase(), url, true);
	if (method.toUpperCase() == "POST") {
		ajaxPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
	ajaxPost.send(ReqParams);
	
	return true;
}

function getRandom() {	// this is used to break the caching issues in IE
   	var minutes = 1000*60;
	var hours = minutes*60;
	var days = hours*24;
	var years = days*365;
	var date = new Date();
	var t = date.getTime();
	var myrandom = Math.round(t/years);

	var d = new Date()
	var ranNum= (Math.round((Math.random()* myrandom)+1)) + d.getTime();
    return ranNum;
}

function SortSongList(ArtId, SortId){
	if (document.body.style.cursor != 'wait'){
		document.body.style.cursor = 'wait';
		document.getElementById('SortBy').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...Loading...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		CallAhah('/ahah/SongsList.html?artistId=' + ArtId + '&Sort=' + SortId, 'get', 'SongListing', 'default');
	}
	return false;
}

function validateLoginAJAX(e, password, country ,gotolink){
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/login/lightboxvalidate.cfm', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.onreadystatechange = function(){
		//alert(http_request.readyState)
		if (http_request.readyState == 4){
			//alert(http_request.status)
			if(http_request.status == 200){
				if(trim(http_request.responseText) == "success"){
					window.location=gotolink;
				} else {
					document.getElementById("errormessage").innerHTML=trim(http_request.responseText);
				}
			}
		}
	}
	//alert(country);
	if(country=="")
	{
		http_request.send('e=' +e + '&password=' +password );
	}
	else
	{
		http_request.send('e=' +e + '&password=' +password +'&cpcountry= '+country );
	}

}



function validateSendPinAJAX(cellphone,country){
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/login/forgotpin.cfm', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.onreadystatechange = function(){
		
		if (http_request.readyState == 4){
			if(http_request.status == 200){				
				document.getElementById("errormessage").innerHTML=http_request.responseText;
			}
		}
	}
	if(country=="")
	{
		http_request.send('cellphone=' + cellphone);
	}
	else{
		http_request.send('cellphone=' + cellphone+'&cpcountry='+country);
	}
}

function doMoreCategory(moreOjb,btnMsg,btnMsg2){	
	if (moreOjb==1){		
		document.getElementById("CategoryListing").style.height="auto";
		document.getElementById("ViewMoreCategory").innerHTML='<a href="" class="more" onclick="doMoreCategory(0,\''+ btnMsg +'\', \''+ btnMsg2 +'\'); return false;">' + btnMsg + '</a>';
	}
	else{
		document.getElementById("CategoryListing").style.height="300px";
		document.getElementById("ViewMoreCategory").innerHTML='<a href="" class="more" onclick="doMoreCategory(1,\''+ btnMsg +'\',\''+ btnMsg2 +'\'); return false;">' + btnMsg2 + '</a>';
	}
}

//******** TOGGLE ON/OFF MOBILE ALERT ************
function ToggleWebAlert(userID, changestatus, dontalert){	
	//window.open("/musicalert.cfm?userID="+userID)	
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/musicalert.cfm', true);	
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.onreadystatechange = function(){		
		if (http_request.readyState == 4){
			if(http_request.status == 200){			
				
				if (trim(http_request.responseText) == "OFF"){
					if (dontalert == 0){
						alert("You will NOT receive music alerts anymore.");
					}
					document.getElementById("start-stop").className="ONstate";					
				}	
				else if (trim(http_request.responseText) == "ON"){
					if (dontalert == 0){
						alert("You will now receive music alerts on your cell phone.");
					}
					document.getElementById("start-stop").className="OFFstate";
				}
				else {
					if (dontalert == 0){
						alert(http_request.responseText);	
					}					
					document.getElementById("start-stop").className="NONEstate";
				}				
			}
		}		
	}	
	http_request.send('userID=' + userID + '&changeStatus=' + changestatus)
}
function ToggleTriviaService(userID, changestatus, dontalert){	
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/toggletrivia.cfm', true);	
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.onreadystatechange = function(){		
		if (http_request.readyState == 4){
			if(http_request.status == 200){			
				if (trim(http_request.responseText) == "OFF"){
					if (dontalert == 0){
						alert("You will NOT receive anymore trivia on your cellphone.");
					}
					document.getElementById("start-stop-trivia").className="triviaON";					
				}	
				else if (trim(http_request.responseText) == "ON"){
					if (dontalert == 0){
						alert("You will now receive music trivia on your cell phone.");
					}
					document.getElementById("start-stop-trivia").className="triviaOFF";
				}
				else {
					if (dontalert == 0){
						alert(http_request.responseText);	
					}					
					document.getElementById("start-stop-trivia").className="triviaNONE";
				}				
			}
		}		
	}	
	http_request.send('userID=' + userID + '&changeStatus=' + changestatus)
}
/******************************************************************/
// light box functions
function doLightBox(url, dl, dl_url) { // pass the url and if its a download, flag the dl to true (dl = true), pass the download url to the function if we are downloading
	// create grayed out div for underneath lightbox
	lbop = document.createElement("div");
	lbop.id = "OpacityLB";
	lbop.className = "grayOpacity";
	
	// create the lightboxcont layer
	lbc = document.createElement("div");
	lbc.id = "LightBoxCont";
	lbc.className = "lightBoxCont";
	
	// create the lightbox layer
	lb = document.createElement("div");
	lb.id = "LightBox";
	lb.className = "lightBox";
	lb.style.display = "none";
	
	//add the lightbox to lightboxcont layer
	lbc.appendChild(lb);
	// add the opacity to the document
	document.body.appendChild(lbop);
	// add lightbox to the document
	document.body.appendChild(lbc);
	// call the page that will populate the light box trough an AHAH call
	CallAhah(url, "GET", "LightBox");
	// do the call to show the lightbox
	//showLightBox();
	// are we downloading a song?
	if (dl != null && dl == true) {
		CallAhah(dl_url, "POST");
	}
	return false;
}

function showLightBox() {
	var scrollTop = 0;
	
	// resize the gray opacity layer to the full width and height that is scollable in the browser
	lbop = document.getElementById("OpacityLB");
	if (lbop != null) {
		lbop.style.width = document.documentElement.scrollWidth + "px";
		lbop.style.height = document.documentElement.scrollHeight + "px";
	}
	
	// resize the lightbox layer to the width and height to the client area that is viewable/visible
	lbc = document.getElementById("LightBoxCont");
	if (lbc != null) {
		lbc.style.width = document.documentElement.clientWidth + "px";
		lbc.style.height = document.documentElement.clientHeight + "px";
		// move the lightbox layer off the client viewable area
		lbc.style.left = document.documentElement.clientWidth + "px";
	}
	
	if (lbop != null) {
		// display the gray opacity layer on the page
		lbop.style.display = "block";
	}
	
	if (lbc != null) {
		// move the lightbox layer at the top left corner, change its size, and then display it
		lbc.style.left = "0px";
		lbc.style.display = "block";
	}
	
	lb = document.getElementById("LightBox");
	if (lb != null) {
		lb.style.display = "block";
		if (self.pageYOffset) {
			scrollTop = window.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			scrollTop = document.documentElement.scrollTop;
		} else if (document.body) {
			scrollTop = document.body.scrollTop;
		}
		
		if (lb.offsetHeight <= document.documentElement.clientHeight) {
			lb.style.top = ((document.documentElement.clientHeight - lb.offsetHeight) / 2) + parseInt(scrollTop) + "px";
		} else {
			lb.style.top = parseInt(scrollTop) +"px";
		}
	}
	
	lastScrollTop = scrollTop;
	
	// add events to window
	if (window.addEventListener) { // FF
		window.addEventListener("scroll", scrolled, false);
	} else if (window.attachEvent) { // IE
		window.attachEvent("onscroll", scrolled);
	}
	
	return false;
}

var lastScrollTop = 0;

function scrolled() {
	var scrollTop = 0;
	var moveLightbox = true;
	var lb = null;
	
	// get the lightbox
	lb = document.getElementById("LightBox");
	if (lb != null) {
		if (window.pageYOffset) {
			scrollTop = window.pageYOffset;
		} else {
			scrollTop = document.documentElement.scrollTop;
		}
		if (parseInt(scrollTop) > parseInt(lastScrollTop)) {
			// Scrolling down...
			if (lb.offsetHeight <= parseInt(document.documentElement.clientHeight)) {
				// center vertically
				position = ((document.documentElement.clientHeight - lb.offsetHeight) / 2) + parseInt(scrollTop);
			} else if ((parseInt(getNumericPart(lb.style.top)) + lb.offsetHeight) < (parseInt(scrollTop) + parseInt(document.documentElement.clientHeight))) {
				// align bottom to bottom
				position = parseInt(scrollTop) + document.documentElement.clientHeight - lb.offsetHeight;
			} else {
				// don't move
				moveLightbox = false;
			}
		} else if (parseInt(scrollTop) < parseInt(lastScrollTop)) {
			// Scrolling up...
			if (lb.offsetHeight <= parseInt(document.documentElement.clientHeight)) {
				// center vertically
				position = ((document.documentElement.clientHeight - lb.offsetHeight) / 2) + parseInt(scrollTop);
			} else if (parseInt(scrollTop) <= parseInt(getNumericPart(lb.style.top))) {
				// align top to top
				position = parseInt(scrollTop);
			} else {
				// don't move
				moveLightbox = false;
			}
		} else {
			// No scrolling...
			moveLightbox = false;
		}
		if (moveLightbox) {
			lb.style.top = position + "px";
			lastScrollTop = scrollTop;
		}
	}
	return false;
}

function hideLightBox() {
	lbc = document.getElementById("LightBoxCont");
	oplb = document.getElementById("OpacityLB");
	
	document.body.removeChild(lbc);
	document.body.removeChild(oplb);
	
	if (window.removeEventListener) {
		window.removeEventListener("scroll", scrolled, false);
	} else if (window.detachEvent) {
		window.detachEvent("onscroll", scrolled);
	}
	
	return false;
}
function viewbio(){
	if(document.getElementById('artistBiography').style.overflow=="visible"){
		document.getElementById('artistBiography').style.height="170px";
		document.getElementById('artistBiography').style.overflow="hidden";
		document.getElementById('viewmore').innerHTML="&nbsp;<a href='' onclick='viewbio(); return false;'>view more</a>";
	}else{
		document.getElementById('artistBiography').style.height="auto";
		document.getElementById('artistBiography').style.overflow="visible";
		document.getElementById('viewmore').innerHTML="&nbsp;<a href='' onclick='viewbio(); return false'>view less</a>";
	}
	
}
function trackSearchClicks(SearchID, TargetURL, PageID, boxclicked){
	if(hasclicked == ""){
		hasclicked = TargetURL
		var http_request = requestObject();
		http_request.open('POST', JSsitepath+'/trackSearchBoxClick.cfm', true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		http_request.send('pageid='+PageID+'&searchid='+SearchID+'&TargetURL='+TargetURL+'&boxclicked='+boxclicked);
		http_request.onreadystatechange = function(){
			if (http_request.readyState == 4){
				if(http_request.status == 200){
					window.location = trim(http_request.responseText);
				}
			}
		}
	}
}
function SubmitFeedBack(topic, feedback,frompgurl){
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/sendfeedback.cfm', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	

	http_request.send('topic='+topic+'&feedback='+feedback+'&frompgurl='+frompgurl);
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4){
			if(http_request.status == 200){
				alert("thank you for your feedback.");
				document.getElementById('topic').value = "";
				document.getElementById('feedback').value = "";
				document.getElementById('submit').disabled = false;
			}
		}
	}
}
function TrackClicks(pageid){
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/trackclicksajax.cfm', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('pageid='+pageid);
}

function PreviewToggle(mediapreview,counter){
	myPlayer=document.getElementById('player');
	myPlayerIframe=document.getElementById('iframeplayer');
	
	//change all to play icon
	var the_inputs=document.getElementsByTagName("img");		
	for(var n=0;n<the_inputs.length;n++){		
		if(the_inputs[n].id.indexOf('ringtone_') > 0){
			the_inputs[n].src='http://images.massivemp3giveaway.com/btn_play.png';
		}		
	}
	
	
	if(myPlayer.style.display=="none" || myPlayerIframe.src != mediapreview){
		//play preview and change the played song to a pause btn
		myPlayer.innerHTML="<iframe id='iframeplayer' src='" + mediapreview + "' frameborder='0' height='1' width='1''>";
		myPlayer.style.display = "block";
		document.getElementById('playringtone_'+counter).src='http://images.massivemp3giveaway.com/pause_btn.jpg';
	} else {
		//everything is already all set to play icons so the player is stopped
		myPlayer.innerHTML="<iframe id='iframeplayer' src='' frameborder='0' height='1' width='1''>";
		myPlayer.style.display = "none";
	}
}

function isValidEmail(str){
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function check(theForm){
	if(isValidEmail(theForm.email.value) == false || isValidEmail(theForm.frEmail.value) == false){
		alert("Invalid Email Address!");
		return false;
	} else if(theForm.theName.value == ''){
		alert("Please Enter your name.");
		return false;
	} else if(theForm.message.text == ''){
		alert("Please enter a message.");
		return false;
	} else{
		var http_request = requestObject();
		http_request.open('POST', JSsitepath+'/sendchallenge.cfm', true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send('email='+document.getElementById('email').value+'&theName='+document.getElementById('theName').value+'&frEmail='+document.getElementById('frEmail').value+'&message='+document.getElementById('message').value);
		hideLightBox();
		return false;
	}
}

function sendRingtone(contentid){
	var http_request = requestObject();
	http_request.open('POST', JSsitepath+'/includes/MobileStreams_API.cfm', true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('contentid='+contentid);
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4){
			if(http_request.status == 200){
				if(trim(http_request.responseText) == "Success|Content delivered Successfully"){
					alert("Ringtone has been successfully sent to your phone");
					hideLightBox();
				} else {
					alert("Ringtone couldn't be sent to your phone. Please contact the support for more information");
					alert(trim(http_request.responseText));
					hideLightBox();
				}
			}
		}
	}
	
}
