//validation for registration system

$(document).ready(function() { 



	$('.submit').click(function(e){

	

		// Declare the function variables:

		// Parent form, form URL, email regex and the error HTML

		var $formId = $(this).parents('form');

		var formAction = $formId.attr('action');

		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var $error = $('<span class="error"></span>');



		// Prepare the form for validation - remove previous errors

		$('li',$formId).removeClass('error');

		$('span.error').remove();



		// Validate all inputs with the class "required"

		$('.required',$formId).each(function(){

			var inputVal = $(this).val();

			var $parentTag = $(this).parent();

			if(inputVal == ''){

				$parentTag.addClass('error').append($error.clone().text('Required Field'));

			}



			// Run the name validation using the regex for those input items also having class "name"

			if($(this).hasClass('name') == true){

				var yourname = $('#name').val();

				if(yourname == 'Name'){

				$parentTag.addClass('error').append($error.clone().text('Please enter your name'));

				}

			}

			

			// Run the email validation using the regex for those input items also having class "email"

			if($(this).hasClass('email') == true){

				if(!emailReg.test(inputVal)){

					$parentTag.addClass('error').append($error.clone().text('Enter valid email'));

				}

			}

			

			// Check passwords match for inputs with class "password"

			if($(this).hasClass('password') == true){

				var password1 = $('#password-1').val();

				var password2 = $('#password-2').val();

				if(password1 != password2){

				$parentTag.addClass('error').append($error.clone().text('Passwords must match'));

				}

			}



			// Run the email validation using the regex for those input items also having class "email"

			if($(this).hasClass('agree') == true){

			if ($('#agree').is(':checked')) {

			} else { 

				$parentTag.addClass('error').append($error.clone().text('You must agree with terms'));

			 }

			}

		});



		// All validation complete - Check if any errors exist

		// If has errors

		if ($('span.error').length > 0) {

			

			$('span.error').each(function(){

				

				// Set the distance for the error animation

				var distance = 5;

				

				// Get the error dimensions

				var width = $(this).outerWidth();

				

				// Calculate starting position

				var start = width + distance;

				

				// Set the initial CSS

				$(this).show().css({

					display: 'block',

					opacity: 0,

					right: -start+'px'

				})

				// Animate the error message

				.animate({

					right: -width+'px',

					opacity: 1

				}, 'slow');

				

			});

		} else {

			$formId.submit();

		}

		// Prevent form submission

			register();

			e.preventDefault();

	});

	

	// Fade out error message when input field gains focus

	$('.required').focus(function(){

		var $parent = $(this).parent();

		$parent.removeClass('error');

		$('span.error',$parent).fadeOut();

	});



 	$("#email").blur(function() {

		$("#error").removeClass().addClass('error').text('Checking').fadeIn("slow");

		$.post("inc/check_email.php",{ email:$(this).val() } ,function(data)

        {

		  if(data=='no')

		  {

		  	$("#error").fadeTo(200,0.1,function()

			{ 

			  $(this).html('Unavailable').addClass('error').fadeTo(900,1);

			});		

          }

		  else

		  {

		  	$("#error").fadeTo(200,0.1,function()

			{

			  $(this).html('Available').addClass('error').fadeTo(900,1);	

			});

		  }

				

        });

 

	});

});



function register()

{

	hideshow('loading',1);

	error(0);

	

	$.ajax({

		type: "POST",

		url: "inc/register.php",

		data: $('#registration_form').serialize(),

		dataType: "json",

		success: function(msg){

			

			if(parseInt(msg.status)==1)

			{

				window.location=msg.txt;

			}

			else if(parseInt(msg.status)==0)

			{

				error(1,msg.txt);

			}

			

			hideshow('loading',0);

		}

	});



}



//update profile

//validation for registration system

$(document).ready(function() { 



	$('.profile_submit').click(function(e){

	

		// Declare the function variables:

		// Parent form, form URL, email regex and the error HTML

		var $formId = $(this).parents('form');

		var formAction = $formId.attr('action');

		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var $error = $('<span class="profile_error"></span>');



		// Prepare the form for validation - remove previous errors

		$('li',$formId).removeClass('profile_error');

		$('span.profile_error').remove();



		// Validate all inputs with the class "required"

		$('.profile_required',$formId).each(function(){

			var inputVal = $(this).val();

			var $parentTag = $(this).parent();

			if(inputVal == ''){

				$parentTag.addClass('profile_error').append($error.clone().text('Required Field'));

			}



			// Run the name validation using the regex for those input items also having class "name"

			if($(this).hasClass('profile_name') == true){

				var yourname = $('#profile_name').val();

				if(yourname == 'Name'){

				$parentTag.addClass('profile_error').append($error.clone().text('Please enter your name'));

				}

			}

			

			// Run the email validation using the regex for those input items also having class "email"

			if($(this).hasClass('profile_email') == true){

				if(!emailReg.test(inputVal)){

					$parentTag.addClass('profile_error').append($error.clone().text('Enter valid email'));

				}

			}

			

			// Check passwords match for inputs with class "password"

			if($(this).hasClass('profile_password') == true){

				var password1 = $('#profile_password-1').val();

				var password2 = $('#profile_password-2').val();

				if(password1 != password2){

				$parentTag.addClass('profile_error').append($error.clone().text('Passwords must match'));

				}

			}



			// Run the email validation using the regex for those input items also having class "email"

			if($(this).hasClass('profile_agree') == true){

			if ($('#profile_agree').is(':checked')) {

			} else { 

				$parentTag.addClass('profile_error').append($error.clone().text('You must agree with terms'));

			 }

			}

		});



		// All validation complete - Check if any errors exist

		// If has errors

		if ($('span.profile_error').length > 0) {

			

			$('span.profile_error').each(function(){

				

				// Set the distance for the error animation

				var distance = 5;

				

				// Get the error dimensions

				var width = $(this).outerWidth();

				

				// Calculate starting position

				var start = width + distance;

				

				// Set the initial CSS

				$(this).show().css({

					display: 'block',

					opacity: 0,

					right: -start+'px'

				})

				// Animate the error message

				.animate({

					right: -width+'px',

					opacity: 1

				}, 'slow');

				

			});

		} else {

			$formId.submit();

		}

		// Prevent form submission

			update_profile();

			e.preventDefault();

	});

	

	// Fade out error message when input field gains focus

	$('.profile_required').focus(function(){

		var $parent = $(this).parent();

		$parent.removeClass('profile_error');

		$('span.profile_error',$parent).fadeOut();

	});



 	$("#profile_email").blur(function() {

		$("#profile_error").removeClass().addClass('profile_error').text('Checking').fadeIn("slow");

		$.post("inc/check_email.php",{ email:$(this).val() } ,function(data)

        {

		  if(data=='no')

		  {

		  	$("#profile_error").fadeTo(200,0.1,function()

			{ 

			  $(this).html('Unavailable').addClass('profile_error').fadeTo(900,1);

			});		

          }

		  else

		  {

		  	$("#profile_error").fadeTo(200,0.1,function()

			{

			  $(this).html('Available').addClass('profile_error').fadeTo(900,1);	

			});

		  }

				

        });

 

	});

});



function update_profile()

{

	profile_hideshow('loading',1);

	profile_error(0);

	

	$.ajax({

		type: "POST",

		url: "inc/update_profile.php",

		data: $('#profile_form').serialize(),

		dataType: "json",

		success: function(msg){

			

			if(parseInt(msg.status)==1)

			{

				//window.location=msg.txt;
				profile_error(1,msg.txt);

			}

			else if(parseInt(msg.status)==0)

			{

				profile_error(1,msg.txt);

			}

			

			profile_hideshow('loading',0);

		}

	});



}



function profile_hideshow(el,act)

{

	if(act) $('#'+el).css('visibility','visible');

	else $('#'+el).css('visibility','hidden');

}



function profile_error(act,txt)

{

	hideshow('profile_error',act);

	if(txt) $('#profile_error').html(txt);

}



//end update profile



function hideshow(el,act)

{

	if(act) $('#'+el).css('visibility','visible');

	else $('#'+el).css('visibility','hidden');

}



function error(act,txt)

{

	hideshow('register_error',act);

	if(txt) $('#register_error').html(txt);

}



$(document).ready(function() {

       		$('input[type="text"]').focus(function() {

    		    if (this.value == this.defaultValue){ 

    		    	this.value = '';

				}

				if(this.value != this.defaultValue){

	    			this.select();

	    		}

    		});

    		$('input[type="text"]').blur(function() {

    		    if ($.trim(this.value) == ''){

			    	this.value = (this.defaultValue ? this.defaultValue : '');

				}

    		});



       		$('input[type="password"]').focus(function() {

    		    if (this.value == this.defaultValue){ 

    		    	this.value = '';

				}

				if(this.value != this.defaultValue){

	    			this.select();

	    		}

    		});

    		$('input[type="password"]').blur(function() {

    		    if ($.trim(this.value) == ''){

			    	this.value = (this.defaultValue ? this.defaultValue : '');

				}

    		});

		

       		$('textarea').focus(function() {

    		    if (this.value == this.defaultValue){ 

    		    	this.value = '';

				}

				if(this.value != this.defaultValue){

	    			this.select();

	    		}

    		});

    		$('textarea').blur(function() {

    		    if ($.trim(this.value) == ''){

			    	this.value = (this.defaultValue ? this.defaultValue : '');

				}

    		});



        // I put classes here, but I can directly change style...

        $('.video_list tr:even').addClass('white');

        $('.video_list tr:odd').addClass('gray'); 



// more countries

$(function() {

  $("ul.categories").each(function() {

    $("li:gt(14)", this).hide(); /* :gt() is zero-indexed */

    $("li:nth-child(15)", this).after("<li class='more_categories'><a href='#'>More &raquo;</a></li>"); /* :nth-child() is one-indexed */

  });

  $("li.more_categories a").live("click", function() {

    var li = $(this).parents("li:first");

    li.parent().children().show('slow');

    li.remove();

    return false;

  });

});



//top ten videos

    var len = 0;

    var curStart = 0;

    var count = 5;

    var items=new Array();

    function BackupList() {

        var lst = $("ul.top_ten");

        len= $("ul.top_ten li").length;

        if (len <= count)

            return;



        $("ul.top_ten li").each(function() {

            items.push($(this));

            $(this).remove();

        });



        var html="";

        for (curStart; curStart < count && curStart < len; curStart++) {

            html += "<li>" + $(items[curStart]).html() + "</li>";

        }

        $(html).appendTo($(lst));

    }



    function ShowMore() {

        if (curStart >= len) {

            curStart = 0;

        }



        $("ul.top_ten li").each(function() {

            $(this).remove();

        });



        var l = curStart;

        var html = "";

        for (curStart; curStart < (l + count) && curStart < len; curStart++) {

            html += "<li>" + items[curStart].html() + "</li>";

        }

        $(html).appendTo($("ul.top_ten"));

    }



        BackupList();

        $("a.next_five").click(function() { ShowMore(); });

		//end top ten





//featured videos

    var flen = 0;

    var fcurStart = 0;

    var fcount = 8;

    var fitems=new Array();

    function FeaturedList() {

        var lst = $("div.featured_videos");

        flen= $("div.featured_videos ul").length;

        if (flen <= fcount)

            return;



        $("div.featured_videos ul").each(function() {

            fitems.push($(this));

            $(this).remove();

        });



        var html="";

        for (fcurStart; fcurStart < fcount && fcurStart < flen; fcurStart++) {

            html += "<ul class='video float_left'>" + $(fitems[fcurStart]).html() + "</ul>";

        }

        $(html).appendTo($(lst));

    }



    function FeaturedMore() {

        if (fcurStart >= flen) {

            fcurStart = 0;

        }



        $("div.featured_videos ul").each(function() {

            $(this).remove();

        });



        var l = fcurStart;

        var html = "";

        for (fcurStart; fcurStart < (l + fcount) && fcurStart < flen; fcurStart++) {

            html += "<ul class='video float_left'>" + fitems[fcurStart].html() + "</ul>";

        }

        $(html).appendTo($("div.featured_videos"));

    }



        FeaturedList();

        $("a.next_featured").click(function() { FeaturedMore(); });

		//end top ten



});

