function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showCenter(point,DivId)
{
	var div = document.getElementById(DivId);
	if( div.style.left != '-4000px' )
	{
		div.style.position = 'absolute';
		div.style.left = '-4000px';
	}
	else
	{
		div.style.position = "absolute";
		div.style.top = point.y + "px";
		div.style.left = point.x - 150 + "px";
	}
}

function Next(formName, menuName)
{
 var to = Number(formName.to.value);
 var numberOfPages = Number(formName.numberofpages.value);
 to = to + numberOfPages;
 formName.to.value = to;
 MakePages(formName, menuName);
}

function Previous(formName, menuName)
{
	var to = Number(formName.to.value);
	var numberOfPages = Number(formName.numberofpages.value);
 if (to <= numberOfPages){
  to = numberOfPages;
 }else{
  to = to - numberOfPages;
 }

 formName.to.value = to;
 MakePages(formName, menuName);
}

function MakePages(formName, menuName)
{
  var to = Number(formName.to.value);
  var numberOfPages = Number(formName.numberofpages.value); 
  var totalPages = Number(formName.totalpages.value);
  var from = to - numberOfPages;
  var TotalPages = totalPages;
  var MakePages = '';
  var page = formName.page.value;
  var readToPage = (document.getElementById('currentpage') == null) ? 0 : Number(document.getElementById('currentpage').value); 
  var color = '';
  var onclick = '';
  var currentPageName = CurrentPageName();
 
  MakePages += '<table cellpadding="0" cellspacing="0" style="width:100%;border:0px;"><tr>';
  
  if (to > numberOfPages)
      MakePages += '<td style="width:30px"><div class="PagesFontPrev" onClick="Previous(document.' + formName.name + ',\'' + menuName + '\');"><!--Previous--></div></td><td align="center">';
  else
	  MakePages += '<td style="width:30px"><div class="PagesFontPrevDisabled"></div></td><td align="center">';
  
  for (i = 1;i <= TotalPages;i++)
  {
      if (i >= from && i <= to)
      {
	  	  color = 'color: rgb(0, 0, 0);';
	  	  //alert(readToPage);
	  	  classStyle = "SelectedPage";
	  	  if (readToPage == 0)
	  	  {  
	  		  if (page != i){
	  			  color = 'color: rgb(0, 0, 255);';
	  			classStyle = 'UnSelectedPage';
	  		  }
	  	  }else{
	  		if (i < page || i > readToPage){
         		color = 'color: rgb(0, 0, 255);';
         		classStyle = 'UnSelectedPage';
         	}
	  	  }  
	  	  
		  if (menuName == ''){
			  onclick = 'document.' + formName.name + '.event.value=\'OnClick_FullSearch\';document.'+ formName.name +'.page.value=\'' + i + '\';formValues(' + formName.name + ');return false;';
		  }else{	   
			  onclick = 'document.' + formName.name + '.menuid.value = document.' + menuName + '.menuid.value;document.' + formName.name + '.event.value=\'OnClick_RefreshPaging\';document.'+ formName.name +'.page.value=\'' + i + '\';formValues(' + formName.name + ');return false;';
		  }
		  MakePages += '<button type="button" onclick="' + onclick + '" class="sb-uix-button"><span style="' + color + '" class="yt-uix-button-content">' + i + '</span></button>';
		  //MakePages += '<a class="PagesFont" style="' + color + '" href="#" onclick="' + onclick + '"><div style="float:left;padding-top:7px;" class="' + classStyle + '">' + i + '</div></a> ';    	  
      }
  }
  
  MakePages += '</td>';
  
  if (TotalPages > to)
  {
     MakePages += '<td style="width:30px"><div class="PagesFontNext" onClick="Next(document.' + formName.name + ',\'' + menuName + '\');"><!--Next--></div></td>';
  }

  MakePages += '</tr></table>';

  document.getElementById('pages').innerHTML = MakePages;
}

function CurrentPageName()
{
	return location.href.substring(location.href.lastIndexOf('/') + 1);
}

function formValues(form)
{
	document.getElementById('divCursor').style.display = 'block';
	sajax_request_type = "POST";
	var variables = new Array(); 
	var count = 0;
	for (x=0; form.elements[x]; x++)
	{
		if(form.elements[x] == null) continue;
		variables[count] = form.elements[x].getAttribute('name') + '|' + form.elements[x].value + '|'; 
		count++;	
	}
	x_PageEvents(variables,do_result_send_form);
	sajax_request_type = "";
}

function do_result_send_form(z) 
{ 
	document.getElementById('divCursor').style.display = 'none';
	var ajaxElements = z.split('|');
	var elementName = '';
	var javascript = '';
	for (x = 0;x < ajaxElements.length; x = x + 3)
	{
		elementName = ajaxElements[x];
		document.getElementById(elementName).innerHTML = ajaxElements[x+1];
		if(ajaxElements[x+2] != ''){
			eval(ajaxElements[x+2]+'();')
		}
	}
}

function HideCursor()
{
	document.getElementById('divCursor').style.display = 'none';
}

function showHide(elementid){
	if (document.getElementById(elementid).style.display == 'none'){
	document.getElementById(elementid).style.display = '';
	} else {
	document.getElementById(elementid).style.display = 'none';
	}
}

function gup(name)
{
  name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
  var regexS = "[\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function SetVariables()
{
	var aa = document.getElementById('sendbutton');
	var title = document.getElementById('title').value;
	var mail = document.getElementById('mail').value;
	var content = document.getElementById('content').value;

	aa.href = aa.href + '&title=' + title + '&mail=' + mail + '&content=' + content + '&send=1';
}
function SocialSites(id,url,title)
{
	switch(id)
	{
		case 1:
			window.open('http://digg.com/submit?url='+url+'&title'+title,'mywindow','')
			break;
		case 2:
			window.open('http://reddit.com/submit?url='+url+'&title'+title,'mywindow1','')
			break;
		case 3:
			window.open('http://www.stumbleupon.com/submit?url='+url+'&title'+title,'mywindow2','')
			break;
		case 4:
			window.open('http://www.facebook.com/share.php?u='+url,'mywindow3','')
			break;
		case 5:
			window.open('http://twitter.com/home?source=ichc&status=%22'+title+'%22 - '+url,'mywindow4','')
			break;
	}
}

function validateComment()
{
	var valid = true;
	
	if (document.getElementById('comment').value == '')
	{
		document.getElementById('errcomment').style.display = '';
		valid = false;
	}
	else
		document.getElementById('errcomment').style.display = 'none';
	
	return valid;
}

function ValidateUpload(validate)
{
	var valid = true;
	var songname = document.getElementById('originalsongname').value;
	var file = document.getElementById('authorname').value;
	var youtubeURL = document.getElementById('youtubeURL').value;
	
	if (trim(songname) == ''){
		if (validate){
			$('#uploadSongname').addClass('bgerror310');
			document.getElementById('erroriginalsongname').style.display = '';
		}
		valid = false;
	}else{	
		if (validate){
			$('#uploadSongname').removeClass('bgerror310');
			document.getElementById('erroriginalsongname').style.display = 'none';
		}
	}
	if (trim(file) == ''){
		if (validate){
			$('#uploadAuthorname').addClass('bgerror310');
			document.getElementById('errauthorname').style.display = '';
		}
		valid = false;
	}else{	
		if (validate){
			$('#uploadAuthorname').removeClass('bgerror310');
			document.getElementById('errauthorname').style.display = 'none';
		}
	}
	if (youtubeURL != '' && !validateYouTubeURL(youtubeURL)){
		$('#uploadYoutube').addClass('bgerror310plus');
		document.getElementById('erryoutubeexample').style.display = '';
		valid = false;
	}else{
		$('#uploadYoutube').removeClass('bgerror310plus');
		document.getElementById('erryoutubeexample').style.display = 'none';
	}
	return valid;
}

function validateUploadVideo(){
	var url = document.getElementById('youtubeVideoURL').value;
	if (url == '' || !validateYouTubeURL(url)){
		if (location.href.indexOf('lyric_player.php') != -1){
			$('#uploadYoutubeVideo').addClass('');
		}else{
			$('#uploadYoutubeVideo').addClass('bgerror310plus');
		}	
		document.getElementById('erryoutubevideoupload').style.display = '';
		return false;
	}else{
		if (location.href.indexOf('lyric_player.php') != -1){
			$('#uploadYoutubeVideo').removeClass('');
		}else{
			$('#uploadYoutubeVideo').removeClass('bgerror310plus');
		}
		document.getElementById('erryoutubevideoupload').style.display = 'none';
	}
	return true;
}

function validateYouTubeURL(url){
	if (url.substring(0,7) != 'http://'){
		url = 'http://' + url;
	}	

	if (!isUrl(url)){
		return false;
	}
	
	if (url.substring(0,31) != 'http://www.youtube.com/watch?v='){
		return false;
	}
	return true;
}

function isUrl(s) {
	var regexp = /(http|http):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

//Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

function validateRegistration(){
	var fullname = document.getElementById('fullname');
	var username = document.getElementById('username');
	var password = document.getElementById('SeePassword');
	var nickname = document.getElementById('nickname');
	var validate = true;
	
	if (!ShowEmptyWarning(nickname)){
		$('#regNickname').addClass('bgerror310');
		validate = false;
	}else{
		$('#regNickname').removeClass('bgerror310');
	}	
	
	if (!ShowEmptyWarning(fullname)){ 
		$('#regFullname').addClass('bgerror310');
		validate = false;
	}else{
		$('#regFullname').removeClass('bgerror310');
	}	
	if (!ShowEmptyWarning(username)){
		validate = false;
		document.getElementById('errmail').style.display = 'none';
		$('#regUsername').addClass('bgerror310');
	}else{
		if (!emailValidate(username)){
			document.getElementById('errmail').style.display = '';
			$('#regUsername').addClass('bgerror310');
			validate = false;
		}else{
			$('#regUsername').removeClass('bgerror310');
			document.getElementById('errmail').style.display = 'none';
		}
	}
	
	if (!ShowEmptyWarning(password)) {
		$('#regPassword').addClass('bgerror310');
		validate = false;
	}else{
		if(!TextLenght(password,6)) {
			validate = false;
			$('#regPassword').addClass('bgerror310');
		}else{
			$('#regPassword').removeClass('bgerror310');
		}
	}
	return validate;
}

function validateLogin(){
	validate = true;
	var username = document.getElementById('loginEmailError');
	var password = document.getElementById('loginPasswordError');
	
	if($('#loginusername').val() == ''){
		$('#loginEmail').addClass('bgerror310');
		username.style.display = '';
		validate = false;
	}else{
		username.style.display = 'none';
		$('#loginEmail').removeClass('bgerror310');
	}
	
	if($('#loginpassword').val() == ''){
		$('#loginPassword').addClass('bgerror310');
		validate = false;
		password.style.display = '';
	}else{
		$('#loginPassword').removeClass('bgerror310');
		password.style.display = 'none';
	}
	
	return validate;
}

function validateInvitate(){
	var validate = true;
	if ($('#friendemail').val() == ''){
		$('#friendemailValidate').addClass('bgerror310');
		$('#friendemailError').html('&nbsp;(Please fill e-mail field.)');
		validate = false;
	}else{
		if(!emailValueValidate($('#friendemail').val())){
			$('#friendemailValidate').addClass('bgerror310');
			$('#friendemailError').html('&nbsp;(Please fill valid e-mail address.)');
			validate = false;
		}else{
			$('#friendemailValidate').removeClass('bgerror310');
			$('#friendemailError').html('');
		}
	}	
	return validate;
}

function validateProfile()
{
	var fullname = document.getElementById('profilefullname');
	var password = document.getElementById('profilepassword');
	var validate = true;
	
	if (!ShowEmptyWarning(fullname)) {
		validate = false;
		$('#profileFullname').addClass('bgerror310');
	}else{
		$('#profileFullname').removeClass('bgerror310');
	}
	
	if (password.value != '') {
		if(!TextLenght(password,6)){
			validate = false;
			$('#profilePassword').addClass('bgerror310');
		}else{
			$('#profilePassword').removeClass('bgerror310');
		}
	}else{
		$('#profilePassword').removeClass('bgerror310');
	}
	return validate;
}

function TextLenght(element,minLenght)
{
	var validate = true;
	var len = trim(element.value); 

	if (len.length < minLenght){
		document.getElementById('errlenght'+element.id).style.display = '';
		validate = false;		
	}else
		document.getElementById('errlenght'+element.id).style.display = 'none';
	
	return validate;

}

function ShowEmptyWarning(element)
{
	var validate = true;
	if (trim(element.value) == '')
	{
		document.getElementById('err'+element.id).style.display = '';
		validate = false;
	}
	else
		document.getElementById('err'+element.id).style.display = 'none';
	return validate;
}

function emailValidate(element) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = element.value;
   return reg.test(address);
}

function emailValueValidate(value) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(value);
}

// Removes ending whitespaces
function RTrim(value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

function Hide(element){
	var el = document.getElementById(element);
	if (el == null)
		return;
	
	el.style.display = 'none';
}

function Show(element){
	var el = document.getElementById(element);
	if (el == null)
		return;
	
	el.style.display = '';
}

function IsHide(element){
	var el = document.getElementById(element);
	if (el == null)
		return true;
	
	if (el.style.display == 'none')
		return true;
	
	return false;
}

function Switch(element){
	if (IsHide(element))
		Show(element);
	else 
		Hide(element);
}

function ClearValue(element){
	var el = document.getElementById(element);
	if (el == null)
		return;
	
	el.value ='';
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else
		cntfield.html(maxlimit - field.value.length);
}

function textCounterJQuery(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else
		cntfield.html(maxlimit - field.value.length);
}


function isLoggedIn(){
	return ($('#profileLink').length != 0)
}

function ValidateLogin(type,id){
	
	if (isLoggedIn()) {return true;}
	var lyricsMessage = '<div class="fl m15 w400" style="font-family:arial;"><p><h2>Join us and have a fun</h2></p>To use lyric editor you must login first.<br /> If you are not yet Singbre user please sign in. To sign in <a href="#" onclick="$.fn.fancybox.close(); openSingin();return false;">click here</a>.<br />If you are Sinbre user go to login <a href="#" onclick="$.fn.fancybox.close(); openLogin();return false;">click here</a>.</div><div class="fl mt15"><img src="images/penguin.png" border="0" alt="Singbre administrator" /></div>';
	//var songMessage = '<div style="float:left;margin:15px;width:400px;font-family:arial;"><p><h2>Join us and have a fun</h2></p>To use song recorder you must login first.<br /> If you are not yet Singbre user please sign in. To sign in <a href="#" onclick="$.fn.fancybox.close(); openSingin();return false;">click here</a>.<br />If you are Sinbre user go to login <a href="#" onclick="$.fn.fancybox.close(); openLogin();return false;">click here</a>.</div><div style="float:left;margin-top:15px"><img src="images/penguin.png" border="0" alt="Singbre administrator" /></div>';
	var songMessage = '<div class="fl m15 w400" style="font-family:arial;"><p><h2>Join us and have a fun</h2></p>If you are not a singbre user you can record like anonymous. If you wonna record a song with your name just sign in on Singbre.<br /><br /><p><button type="button" onclick="$.fn.fancybox.close(); openSingin();return false;" class="sb-uix-button"><span class="sb-uix-button-content">Yes I want to signin on singbre</span></button>&nbsp;&nbsp;<a href="/audio_recorder.php?aiid=' + id + '"><button type="button" class="sb-uix-button"><span class="sb-uix-button-content">Record song like anonymous</span></button></a></p></div><div class="fl mt15"><img src="images/penguin.png" border="0" alt="Singbre administrator" /></div>';
	//var videoMessage = '<div class="fl m15 w400" style="font-family:arial;"><p><h2>Join us and have a fun</h2></p>To use video recorder you must login first.<br /> If you are not yet Singbre user please sign in. To sign in <a href="#" onclick="$.fn.fancybox.close(); openSingin();return false;">click here</a>.<br />If you are Sinbre user go to login <a href="#" onclick="$.fn.fancybox.close(); openLogin();return false;">click here</a>.</div><div class="fl mt15"><img src="images/penguin.png" border="0" alt="Singbre administrator" /></div>';
	var videoMessage = '<div class="fl m15 w400" style="font-family:arial;"><p><h2>Join us and have a fun</h2></p>If you are not a singbre user you can record like anonymous. If you wonna record a song with your name just sign in on Singbre.<br /><br /><p><button type="button" onclick="$.fn.fancybox.close(); openSingin();return false;" class="sb-uix-button"><span class="sb-uix-button-content">Yes I want to signin on singbre</span></button>&nbsp;&nbsp;<a href="/audio_recorder.php?viid=' + id + '"><button type="button" class="sb-uix-button"><span class="sb-uix-button-content">Record song like anonymous</span></button></a></p></div><div class="fl mt15"><img src="images/penguin.png" border="0" alt="Singbre administrator" /></div>';
	var uploadMessage = '<div class="fl m15 w400" style="font-family:arial;"><p><h2>Join us and have a fun</h2></p>To use upload the file you must login first.<br /> If you are not yet Singbre user please sign in. To sign in <a href="#" onclick="$.fn.fancybox.close(); openSingin();return false;">click here</a>.<br />If you are Sinbre user go to login <a href="#" onclick="$.fn.fancybox.close(); openLogin();return false;">click here</a>.</div><div class="fl mt15"><img src="images/penguin.png" border="0" alt="Singbre administrator" /></div>';
		
	switch(type)
	{
		case 'lyric':
			document.getElementById('basket').innerHTML = lyricsMessage;
			$('#popupmessage').click();
			return false;
			break;
		case 'song':
			document.getElementById('basket').innerHTML = songMessage;
			$('#popupmessage').click();
			return false;
			break;
		case 'video':
			document.getElementById('basket').innerHTML = videoMessage;
			$('#popupmessage').click();
			return false;
			break;
		case 'upload':
			document.getElementById('basket').innerHTML = uploadMessage;
			$('#popupmessage').click();
			return false;
			break;
	}
	return true;
}

function showhidepassword(){
	if (IsHide('password')){
		Show('password');
		Hide('SeePassword');
		$('#showpassword').html('Show password');
	}else{ 
		Show('SeePassword');
		Hide('password');
		$('#showpassword').html('Hide password');
	}
}

function reportaproblem(){
	$('#fancy_div').html();
}

function AddMail(){
	var count = 0;
	var newCount = 0;
	$('.MailItem').each(function(index) {
		count++;
		$(this).attr('name','emailsend' + index);
	});
	
	newCount = count + 1; 
	
	if (count < 4){
		$('#addMails').append('<div><div style="clear:both;float:left;width:130px;">&nbsp;<span id="erremailsend' + newCount + '" class="errorField"><span>&nbsp;</div><div style="float:left;width:340px;"><input type="text" class="MailItem" name="emailsend' + newCount + '" id="emailsend' + newCount + '" style="width:85%">&nbsp;<a href="#" onclick="$(this).parent().parent().remove();DeleteMail();return false;">delete</a></div></div>');
	}

	if(count >=3){
		$('#addMailButton').attr('style','display:none;');  		
	}
}
	
	function DeleteMail(){
		$('#addMailButton').attr('style','display:block;');
	}
	
	function setMailValidetions(){
		var valid = true;
		if($('#sendname').val() == ''){
			$('#errsendname').html(' Please fill name.');
			valid = false;
		}else{
			$('#errsendname').html('');
		}
		
		if($('#emailsend').val() == ''){
			$('#erremailsend').html(' Please fill e-mail.');
			valid = false;
		}else{
			if(!emailValueValidate($('#emailsend').val())){
				$('#erremailsend').html(' E-mail not valid.');
				valid = false;
			}else{
				$('#erremailsend').html('');
			}
		}
		
		if($('#emailsend1').length != 0 && $('#emailsend1').val() != ''){
			if(!emailValueValidate($('#emailsend1').val())){
				$('#erremailsend1').html(' E-mail not valid.');
				valid = false;
			}else{
				$('#erremailsend1').html('');
			}
		}else{
			$('#erremailsend1').html('');
		}
		
		if($('#emailsend2').length != 0 && $('#emailsend2').val() != ''){
			if(!emailValueValidate($('#emailsend2').val())){
				$('#erremailsend2').html(' E-mail not valid.');
				valid = false;
			}else{
				$('#erremailsend2').html('');
			}
		}else{
			$('#erremailsend2').html('');
		}
		
		if($('#emailsend3').length != 0 && $('#emailsend3').val() != ''){
			if(!emailValueValidate($('#emailsend3').val())){
				$('#erremailsend3').html(' E-mail not valid.');
				valid = false;
			}else{
				$('#erremailsend3').html('');
			}
		}else{
			$('#erremailsend3').html('');
		}
		
		if($('#emailsend4').length != 0 && $('#emailsend4').val() != ''){
			if(!emailValueValidate($('#emailsend4').val())){
				$('#erremailsend4').html(' E-mail not valid.');
				valid = false;
			}else{
				$('#erremailsend4').html('');
			}
		}else{
			$('#erremailsend4').html('');
		}
		
		return valid;
	}
    function createUploader(){            
        var uploader = new qq.FileUploader({
            element: document.getElementById('file-uploader-demo1'),
            action: 'php.php',
            multiple: false,
            sizeLimit: 204800,
            onComplete: function(id, fileName, responseJSON){$('#userimage').attr('src',responseJSON.image);$('.qq-upload-list').html('');},
            allowedExtensions: ['jpg', 'jpeg', 'png', 'gif']
        });           
    }

