//var BaseUrl = 'http://bcod8/M4World/site/';
var BaseUrl = 'http://m4world.mobi/';
function Home()
{
	window.location= BaseUrl + "index.html";
}
function Register()
{
	window.location= BaseUrl + "register.html";
}
function Login()
{
	window.location= BaseUrl + "login.html";
}
function Logout()
{
	document.frmlogout.submit();
}
function Cancel()
{
	window.location= BaseUrl + "index.html";
}
function CancelRegistration()
{
	window.location= BaseUrl + "login.html";
}
function MyAccount()
{
	window.location= BaseUrl + "myaccount.html";
}
function MyUploads()
{
	window.location= BaseUrl + "myuploads.html";
}
function MyDownloads()
{
	window.location= BaseUrl + "mydownloads.html";
}
function MyPoints()
{
	window.location= BaseUrl + "mypoints.html";
}
function SingForLove()
{
	window.location= BaseUrl + "singfor.html?sdt=love";
}
function SingForNation()
{
	window.location= BaseUrl + "singfor.html?sdt=nation";
}
function AllGenres()
{
	window.location= BaseUrl + "allgenre.html";
}
function Upload()
{
	window.open(BaseUrl + 'upload.html','','width=500,height=400,left=100,top=100,scrollbars=yes,resizable=yes,toolbar=no,status=no,directories=no,location=no');
}
function BuyCredits()
{
	window.location= BaseUrl + "buy_credits.html";
}
function MyPlayList()
{
	window.location= BaseUrl + "myplaylist.html";
}
function Playlist(id)
{
	if(id)
	window.location= BaseUrl + "playlist.html?plid="+id;
}
function GetAllMedia(id)
{
	document.forms['frmgenresort'+id].submit();
}
function PointHistory()
{
	window.location= BaseUrl + "point_history.html";
}
function ModifyPlaylist(id)
{
	if(id)
		var fields = '?plid='+id;
	else
		var fields = '';

	window.location= BaseUrl + "modify_playlist.html"+fields;
}
function DeleteFromPlayList(id)
{
	
	if(confirm("Are you sure to delete this song from playlist"))
	{
		document.getElementById('mid').value = id
		document.playlist.submit();
	}
}
function Play(mid,plid)
{
	window.location= BaseUrl + "play_playlist.html?mid="+mid+"&plid="+plid;
}
function AddPlaylist()
{
	document.frmplaylist.submit();
}
function AddToPlaylist(id)
{ 
	window.location= BaseUrl + "addtoplaylist.html?mid="+id;
}
function ForgotPSWD()
{
	window.open(BaseUrl + 'forgotpswd.html','ForgotPswd','width=670,height=350,left=100,top=100,scrollbars=no,resizable=no,toolbar=no,status=no,directories=no,location=no');
}
// ############ ADD SONG TO PLAYLIST - START ############ // 
function AddMediaToPlaylist()
{
	
	var count = document.frmaddtoplaylist.countplist.value;
	for(var i=0; i<count;i++)
	{
		var fieldname = 'check_playlist_'+i;
		if(document.getElementById(fieldname).checked==true)
		{
			var flag = true;
			break;
		}
		else
			var flag = false;
	}
	if(!flag)
	{
		alert("Please select atleast one playlist.");
	}
	else
		document.frmaddtoplaylist.submit();

}
// ############ ADD SONG TO PLAYLIST - END ############ // 

// ############ DELETE PLAYLIST - START ############ // 
function DeletePlaylist(id)
{
	if(confirm("Are you sure to delete this playlist"))
	{
		//var frmname = 'frmplaylist'+id;
		//if(document.frmname!=null)
		//document.getElementById(frmname).submit();
		window.location= BaseUrl + "modify_playlist.html?action=delete&plid="+id;
	}
}
// ############ DELETE PLAYLIST - END ############ // 

// ############ CHANGE THE TAB OF FEATURED,PREMIUM AND NEW SONGS  - START ############ // 
function SHOW(Flag)
{
	document.getElementById('featured').style.display='none';
	document.getElementById('premium').style.display='none';
	document.getElementById('newlyadded').style.display='none';
	document.getElementById('imgf').src='images/featured.gif';
	document.getElementById('imgp').src='images/premium.gif';
	document.getElementById('imgn').src='images/newly.gif';

	switch(Flag)
	{
		case 'featured' :
			document.getElementById('imgf').src='images/featured1.gif';
			document.getElementById('featured').style.display='inline';
			break;
		case 'premium' :
			document.getElementById('imgp').src='images/premium1.gif';
			document.getElementById('premium').style.display='inline';
			break;
		case 'new' :
			document.getElementById('imgn').src='images/newly1.gif';
			document.getElementById('newlyadded').style.display='inline';
			break;
		default :
			break;
	}
}
// ############ CHANGE THE TAB OF FEATURED,PREMIUM AND NEW SONGS - END ############ // 
 
// ############ VALIDATION FOR UPLOAD MEDIA - START ############ // 
function UploadMusic()
{
	if(document.getElementById('title').disabled==true ||
	document.getElementById('artist_name').disabled==true ||
	document.getElementById('genre').disabled==true ||
	document.getElementById('lyrics').disabled==true ||
	document.getElementById('description').disabled==true ||
	document.getElementById('music_file').disabled==true)
	{
		alert("Please accept terms and conditions.")
		return false;
	}
	if(document.getElementById('userlogin').value==0)
	{	
		alert("Please login to upload music.")
		return false;
	}
	if(document.getElementById('title').value.length<3)
	{	
		alert("Please enter title.")
		document.getElementById('title').focus();
		return false;
	}
	if(document.getElementById('artist_name').value=='')
	{	
		alert("Please enter artist name.")
		document.getElementById('artist_name').focus();
		return false;
	}
	/*if(document.getElementById('artist').value=='')
	{	
		alert("Please select artist.")
		document.getElementById('artist').focus();
		return false;
	}
	if(document.getElementById('artist').value=='Other')
	{	
		if(document.getElementById('artist_name').value=='')
		{	
			alert("Please enter artist name.")
			document.getElementById('artist_name').focus();
			return false;
		}
	}*/
	if(document.getElementById('genre').value=='')
	{	
		alert("Please select genre.")
		document.getElementById('genre').focus();
		return false;
	}
	if(document.getElementById('genre').value=='Other')
	{
		if(document.getElementById('genre_name').value=='')
		{
			alert("Please enter genre name.")
			document.getElementById('genre_name').focus();
			return false;
		}
	}
	if(document.getElementById('lyrics').value.trim()=='')
	{	
		alert("Please enter lyrics.")
		document.getElementById('lyrics').focus();
		return false;
	}	
	if(document.getElementById('description').value.trim()=='')
	{	
		alert("Please enter description.")
		document.getElementById('description').focus();
		return false;
	}
	if(!document.getElementById('music_file').value.length)
	{	
		alert("Please enter your music file.")
		document.getElementById('music_file').focus();
		return false;
	}
	else 
	{
		
		if(confirm("Do you want to upload this file?"))
		{
			document.getElementById('fadein').style.display='inline';
			document.getElementById('wait').style.display='inline';
			return true;
		}
		else
			return false;
	}								
}
// ############ VALIDATION FOR UPLOAD MEDIA - END ############ // 

//function GetArtist(val)
//{
//	if(val=='0')
//		document.getElementById('divartist').style.display='inline';	
//	else
//		document.getElementById('divartist').style.display='none';
//}

function GetOtherGenre(val)
{
	if(val=='Other')
		document.getElementById('divgenre').style.display='inline';	
	else
		document.getElementById('divgenre').style.display='none';
}

function GetUploadForm()
{
	document.getElementById('uploadpopup').style.display='inline';
	document.getElementById('uploadfrm').style.display='inline';
	
}
function getUploadStatus()
{
	document.FrmMyuploads.submit();
}

// ############ COMMON PAGINATION - START ############ // 
function Pagination(pageid, currentpage)
{
	var url= BaseUrl + "ajax_list.html?pageid=" + pageid + "&currentpage=" + currentpage + "&flag=myuploads"; 
	//alert(url);
	return validate(url,'uploadlist','GET',null);

}
// ############ COMMON PAGINATION - END ############ // 


function GetPagination(pageid, currentpage,flag,div)
{
	if(!div)
	 var div = 'recordlist';
	var url= BaseUrl + "ajax_list.html?pageid=" + pageid + "&currentpage=" + currentpage + "&flag="+flag; 
	//alert(url);
	return validate(url,div,'GET',null);

}

function Decline()
{
	parent.document.getElementById('uploadpopup').style.display = 'none';
	parent.document.getElementById('uploadfrm').style.display = 'none';
	if(parent.document.getElementById('UploadMsg')!=null)
		parent.document.getElementById('UploadMsg').style.display = 'none';

	DisableAll();
}

function Close()
{
	 Decline();
}

// ############ DISABLE ALL FIELDS OF UPLOAD FORM- START ############ // 
function DisableAll()
{
	document.getElementById('title').disabled=true;
	//document.getElementById('artist').disabled=true;	
	document.getElementById('artist_name').disabled=true;
	document.getElementById('genre').disabled=true;
	document.getElementById('lyrics').disabled=true;
	document.getElementById('description').disabled=true;
	document.getElementById('tags').disabled=true;
	document.getElementById('music_file').disabled=true; 
	document.getElementById('submitupload').disabled=true;


}
// ############ DISABLE ALL FIELDS OF UPLOAD FORM- END ############ // 

// ############ ENABLE ALL FIELDS OF UPLOAD FORM- START ############ // 
function Accept()
{
	document.getElementById('title').disabled=false;
	//document.getElementById('artist').disabled=false;
	document.getElementById('artist_name').disabled=false;
	document.getElementById('genre').disabled=false;
	document.getElementById('lyrics').disabled=false;
	document.getElementById('description').disabled=false;
	document.getElementById('tags').disabled=false;
	document.getElementById('music_file').disabled=false;
	document.getElementById('submitupload').disabled=false;
	if(parent.document.getElementById('flagsong')!=null)
	{
		var flagval = parent.document.getElementById('flagsong').value;
		document.getElementById('songflag').value=flagval;
	}

}
// ############ ENABLE ALL FIELDS OF UPLOAD FORM- END ############ // 

function Preview(id,flag)
{
	
	if(flag)
		var url = '&play='+flag;
	else
		var url = '';
	window.location= BaseUrl + "preview.html?id="+id+url;
}

// ############ CHECK BALANCE POINTS AND DOWNLOAD MEDIA - START ############ // 
function Download(id,flag)
{	
	if(!flag)
		var flag = ''
	var balance  = document.getElementById('balancepoints').value;
	var points   = document.getElementById('points'+flag+id).value;
	var validity = true;//document.getElementById('validityofcustomer').value;
	var total = 1;//balance - points;
	if(validity)
	{
		if(total>=0)
		{
			if(confirm("Do you want to download this music?"))
			{
				//document.getElementById('balancepoints').value = total;
				//if(document.getElementById('changepoints')!=null) 
			    	//document.getElementById('changepoints').innerHTML = total;
				
				if(document.getElementById('changedownloads')!=null)
				{
					var nodownloads = parseInt(document.getElementById('nodownloads').value);
					document.getElementById('nodownloads').value = nodownloads+1;
					document.getElementById('changedownloads').innerHTML = nodownloads+1;
				}

				window.location= BaseUrl + "download.html?id="+id;
			}
			
		}
		else
		{
			if(flag)
			{
				if(flag=='Pre')
					document.getElementById('DownLoad'+flag+id).innerHTML ='<a href="javascript:BuyCredits();"><img src="images/credit.jpg" alt="Buy Credit" title="Buy Credit" />';
				else
					document.getElementById('DownLoad'+flag+id).innerHTML ='<a href="javascript:BuyCredits()">Buy Credits</a>';
			}
			
			document.getElementById('ErrMsg'+flag+id).innerHTML ="<b>Don't have enough points to download this music. Please buy credits.</b>";
			alert("Don't have enought points for downloads. Please buy credits.");
		}
	}
	else
	{
		document.getElementById('ErrMsg'+flag+id).innerHTML ="<b>Your validity is expired. Please buy credits.</b>";
		alert("Your validity is expired. Please buy credits.");
	}
}
// ############ CHECK BALANCE POINTS AND DOWNLOAD MEDIA - END ############ // 

function DownloadMedia(id)
{
	window.location= BaseUrl + "download_song.html?mid="+id;
}

function MusicByGenre(id)
{
	if(id)
		window.location= BaseUrl + "mediagenres.html?gid="+id;
	else
		window.location= BaseUrl + "mediagenres.html";

}

// ############ SUBMIT COMMENTS USING POST AJAX- START ############ // 
function SubmitComments()
{
	val = document.getElementById('comment').value.trim();
	if(val=='')
	{	
		alert("Please enter comments.")
		document.getElementById('comment').focus();		
	}
	else
	{
		document.getElementById('comment').value='';
		document.getElementById('comment').disabled=true;
		document.getElementById('submitimag').disabled=true;
		document.getElementById('msg').value='sending...';
		var div = 'msg';
		var url= BaseUrl + "ajax_getvalue.html"; 
		 var poststr = "comment=" + encodeURI(val) + "&flag=comments";
		//alert(url);
		return  makePOSTRequest(url, poststr);
	}	

}

function SubmitCommentsPreview(ID)
{	
	/*val = document.getElementById('comment').value.trim();
	if(val=='')
	{	
		alert("Please enter comments.")
		document.getElementById('comment').focus();		
	}
	else
	{
		document.getElementById('comment').value='';
		document.getElementById('comment').disabled=true;
		document.getElementById('submitimag').disabled=true;
		document.getElementById('msg').value='sending...';
		var div = 'msg';
		var url= BaseUrl + "ajax_getvalue.html"; 
		 var poststr = "comment=" + encodeURI(val) + "&flag=previewcomments&mediaid="+ID;
		return makePOSTRequest(url,poststr);*/
		/*alert(val);
		alert(url);
		alert(poststr);*/
		
		/*window.location	= BaseUrl + "preview.html?id="+ID;
		exit();*/
	//}	

}
// ############ SUBMIT COMMENTS USING POST AJAX- END ############ // 


// ############ CHECK MEDIA IN PLAYLIST - START ############ // 


String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,"");};//to trim string functions

// ############ CHECK MEDIA IN PLAYLIST - START ############ // 
function CheckMediaInPlaylist(id,i)
{
	var fieldname = 'check_playlist_'+i;
	var divname = 'err'+id;
	if(document.getElementById(fieldname).checked==true)
	{		
		var mid = document.getElementById('mid').value;
		var url= BaseUrl + "ajax_getvalue.html?flag=mediainplaylist&mid=" +mid+"&plid=" + id; 
		//alert(url);
		return validate(url,divname,'GET',null);
	}
	else
		document.getElementById(divname).innerHTML = '';
}
// ############ CHECK MEDIA IN PLAYLIST - END ############ // 

function GetLoad(flag)
{
	switch(flag)
	{
		case 'Downloads' :
			break;
		case 'Uploads' :
			MyUploads();
			break;
		default :
			break;
	}
}

// ############ FOOTER AND REDIRECT TO STATIC PAGES - START ############ // 
function ShowPage(Page)
{
	if(Page)
	{
		switch(Page)
		{
			case 'upload':
				GetUploadForm();
				break;
			case 'account':
				MyAccount();
				break;
			case 'credits':
				BuyCredits();
				break;
			case 'playlist':
				MyPlayList();
				break;
			case 'new':
			case 'popular':
				window.location= BaseUrl + "medias.html?req="+Page;
				break;
			case 'genre':
			case 'artist':
				window.location = BaseUrl + "allmedia.html?req="+Page;
				break;	
			case 'contact_us':
				window.location= BaseUrl + "contact_us.html";
				break;
			default :
				window.location = BaseUrl + "static.html?page="+Page;
				break;
		}
	}
}
// ############ FOOTER AND REDIRECT TO STATIC PAGES - END ############ // 


// ##################### SUBMIT CLOUD TAG - START ##################### // 
function CloudTag(tag)
{
	document.getElementById('cloudquery').value = tag;
	document.searchcloud.submit();
}
// ##################### SUBMIT CLOUD TAG - END  ##################### // 


// ################# CHANGE VIEW FOR GENRES - START ################# // 
function ChangeGenreView()
{
	if(document.getElementById('by_view').style.display=='inline')
	{
		document.getElementById('by_view').style.display='none';
		document.getElementById('by_songs').style.display='inline';
	}
	else 
	{
		document.getElementById('by_view').style.display='inline';
		document.getElementById('by_songs').style.display='none';
	}
}
// ################# CHANGE VIEW FOR GENRES- END   ################# // 


// ############### SET STAR RATING FOR MEDIA - START ############### // 
function SetRating(rates,mid,flag)
{
	var width = rates * 25;
	var divname = flag + mid;
	document.getElementById(flag+'_curr'+mid).style.width = width +'px';

	if(confirm("Do you want to rate this song?"))
	{
		document.getElementById(divname).innerHTML = "Rating..<img src='"+BaseUrl+"images/Spin.gif'>";
		document.getElementById('mid').value = mid;
		document.getElementById('flag').value = flag;
		document.getElementById('rates').value = rates;
		setTimeout(SubmitRates,2000);
		
	}
	else
	{
		var width = document.getElementById(flag+'_ratew'+mid).value;
		document.getElementById(flag+'_curr'+mid).style.width = width +'px';
	}

}

function SubmitRates()
{
	var mid   = document.getElementById('mid').value;
	var flag  = document.getElementById('flag').value;
	var rates = document.getElementById('rates').value;
	var divname = flag + mid;
	var url= BaseUrl + "ajax_getvalue.html?flag=rating&mid=" +mid+"&rates=" + rates+"&flag0=" + flag; 		
		return validate(url,divname,'GET',null);
}
// ############### SET STAR RATING FOR MEDIA - END   ############### // 



// ############### MOBILE VALIDATION BUY CREDITS - START   ############### // 
function SubmitBuyCredits(id)
{
	non_numeric = /[^0-9+ ]+/;
	var mobile = document.getElementById('mobile'+id).value.trim();
	if(!(mobile) || non_numeric.test(mobile) || mobile=='Enter mobile no.')
	{
		alert("Please enter valide mobile number.");
		return false;
	}
	else
		return true;
}
// ############### MOBILE VALIDATION BUY CREDITS - END   ############### // 


// ############### MOBILE VALIDATION BUY CREDITS - START   ############### // 
function EmailSong()
{
	document.getElementById('sendemail').style.display = 'inline';
}

function EmailMultipleSongs()
{
	document.getElementById('sendemailmultiple').style.display = 'inline';
}

function CheckEmailMessage()
{
	var chks = document.getElementsByName('check_media[]');
	var count=0;
	for (var i = 0; i < chks.length; i++)
    {
		if (chks[i].checked)
		{
			   count=count+1;
		}
	}

	if(count==0 || count == '')
	{
		alert("Please select atleast one song to email");
		return false;
	}		

	var EmailID = document.getElementById('email_id').value;
	var Message = document.getElementById('message').value;

	if( EmailID == "" || EmailID == null || EmailID == 0 )
	{
		alert("Email ID is Invalid.");
		document.getElementById('email_id').focus();
		return false;
	}

	if(isEmail(EmailID) == false)
	{
		return false;
	}

	if( Message == "" || Message ==null )
	{
		alert("Please Enter An Valid Message.");
		document.getElementById('message').focus();
		return false;
	}		
}

// ############### MOBILE VALIDATION BUY CREDITS - END   ############### // 


// ############### SHOW DETAILS OF BOXS - START   ############### // 
function ShowDetails(name)
{
	
	document.getElementById('sing_love').style.display = 'none';
	document.getElementById('sing_nation').style.display = 'none';
	document.getElementById('guide_song').style.display = 'none';
	document.getElementById('your_voice').style.display = 'none';
	document.getElementById('emerging_artist').style.display = 'none';
	document.getElementById('pros').style.display = 'none';
	
	document.getElementById(name).style.display = 'inline';
	
}
// ############### SHOW DETAILS OF BOXS - END   ############### // 

// ############### HIDE DETAILS OF BOXS - START   ############### // 
function CloseDetails(name)
{
		document.getElementById(name).style.display = 'none';

}
// ############### HIDE DETAILS OF BOXS - END   ############### // 

function isEmail(emailStr1)
{
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,4})\.(\d{1,4})\.(\d{1,4})\.(\d{1,4})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

		var matchArray=emailStr1.match(emailPat)
		if (matchArray==null) {
			alert("Email address seems incorrect (check @ and .'s)")		
			return false
		}
		
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) {
			alert("The user name doesn't seem to be valid.")
			return false
		}

		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			  for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					alert("Destination IP address is invalid!")
					return false
				}
			}	    
			return true
		}

		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			alert("The domain name doesn't seem to be valid.")
			return false
		}

		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) {
		   alert("The address must end in a three-letter domain, or two letter country.")
		   return false
		}

		if (len<2) {
		   var errStr="This address is missing a hostname!"
		   alert(errStr)
		   return false
		}	
		return true
 }


