	$(document).ready(function(){
		
		var requiredHTML  = "<img src='/images/icons/required.png' /> Required...";
		var isInvalidHTML = "<img src='/images/icons/error.png' /> it's invalid!"
		var isValidHTML   = "<img src='/images/icons/tik.png' />";
		var defaultPopupHeight = 400;
		var defaultPopupWidth  = 500;
		
		/* AJAX LOADING */
		
		$("#loading").ajaxStart(function(request, settings){
		   $(this).fadeIn();
		});
		
		$("#loading").ajaxComplete(function(){
		   $(this).fadeOut();
		});
		
		/* OTHER */
		$('.delete').click(function(){
			if(!confirm('Are you sure?! Do you want to delete this?!'))
				return false;
		});
		/* POPUP */
		$('a.popup').click(function(){
			var w = defaultPopupWidth;
			var h = defaultPopupHeight;
			
			if($(this).attr('popupwidth')!='')
				w = $(this).attr('popupwidth');
				
			if($(this).attr('popupheight')!='')
				h = $(this).attr('popupheight');
			
			window.open($(this).attr('href'),'popup','width='+w+',height='+h+'');
			
			return false;
		});
		/* ************ */
		
		
		/*  FORM VALIDATOR */
		$('.required').focus(function(){
			$(this).next('span.validator').removeClass('red');
			$(this).next('span.validator').removeClass('green');
			$(this).next('span.validator').html(requiredHTML);
		});
		
		$('.required').change(function(){
			var isValid = 1;
			
			if($(this).is('[type="checkbox"]')){
				if(!$(this).is(':checked')){
					isValid = 0;
				}
			}
			
			if(isValid == 1){
				$(this).next('span.validator').removeClass('red');
				$(this).next('span.validator').addClass('green');
				$(this).next('span.validator').removeClass('invalid');
				$(this).removeClass('required');
				$(this).next('span.validator').html(isValidHTML);
			}else{
				$(this).next('span.validator').removeClass('green');
				$(this).next('span.validator').addClass('red');
				$(this).next('span.validator').addClass('invalid');
				$(this).next('span.validator').html(isInvalidHTML);
			}			
			
		});
		
		$('.required').blur(function(){
			var isValid = 1;
			
			if($(this).is('select')){
				if($(this).val() == 'select'){
					isValid = 0;
				}
			}
			
			if($(this).is(".emailValidator")){
				if (!emailValidator($(this).val())) {
					isValid = 0;
				}
			}
			
			var thisFixcount = $(this).attr('fixcount');
			if(thisFixcount > 0){
				if(!($(this).val().length == thisFixcount)){
					isValid = 0;
				}
			}
			
			var thisMincount = $(this).attr('mincount');
			if(thisMincount > 0){
				if(!($(this).val().length >= thisMincount)){
					isValid = 0;
		      var isInvalidHTML = "<img src='images/icons/error.png' /> Min Chars: "+thisMincount+"!"
				}
			}
			
			var thisMaxcount = $(this).attr('maxcount');
			if(thisMaxcount > 0){
				if(!($(this).val().length <= thisMaxcount)){
					isValid = 0;
		      var isInvalidHTML = "<img src='images/icons/error.png' /> Max Chars: "+thisMincount+"!"
				}
			}
			
			var sameAs = $(this).attr('sameas');
			if(sameAs != undefined){
				if($(sameAs).val() != $(this).val()){
					isValid = 0;
		      var isInvalidHTML = "<img src='images/icons/error.png' /> Passwords not matching!"
				}
			}
			
			if ($(this).val().length < 1) {
				isValid = 0;
		    var isInvalidHTML = "<img src='images/icons/error.png' /> it's invalid!"
			}
			
			if(isValid == 1){
				$(this).next('span.validator').removeClass('red');
				$(this).next('span.validator').addClass('green');
				$(this).next('span.validator').removeClass('invalid');
				$(this).removeClass('required');
				$(this).next('span.validator').html(isValidHTML);
			}else{
				$(this).next('span.validator').removeClass('green');
				$(this).next('span.validator').addClass('red');
				$(this).next('span.validator').addClass('invalid');
				$(this).next('span.validator').html(isInvalidHTML);
			}
			
		});
		
		$('myForm1.submitValidator').submit(function(){
			var invalidInputs = $(this).find('.invalid').length;
			var requireInputs = $(this).find('.required').length;
			if(invalidInputs > 0 || requireInputs > 0){
				alert("Please fill all fields correctly!");
				$(this).children('input#hiddenValidator').val(0);
				return false;
			}else{
				$(this).children('input#hiddenValidator').val(1);
			}
		});
		
		/*  **********  */
		
		/*  LOCATION SELECTOR  */
		
		$('select.locationFinder').change(function(){
			//---CLEAR OTHER SELECTs
			$(this).parent().nextAll('div').children('select').html('');
			//---
			if ($(this).val() != 0) {
				var thisD = $(this).attr('id');
				var next = $(this).parent().next('div').children('select');
				var data = 'act=location_finder&thisd=' + thisD + '&next=' + next.attr('id') + '&parent_id=' + $(this).attr('pid') + '&this_value=' + $(this).val();
				
				$.ajax({
					type: "POST",
					url: "ajax.php",
					data: data,
					success: function(msg){
						next.html(msg);
					}
				});
			}
		});
		
		$('input#addLocation').click(function(){
			var thisOb = $(this);
			var data = 'act=add_location&'+$(this).parents('myForm1').serialize();
			$.ajax({
					type: "POST",
					url: "ajax.php",
					data: data,
					success: function(msg){
						if (msg == 'err') {
							alert('You have 5 location!');
						}else{
							var liStr = '<li id="location_'+msg+'"><b>Global &gt; ';
							thisOb.parent().children('div').children('select').children('option:selected').each(function(){
								if ($(this).val() != 0) {
									liStr += $(this).text();
								}
							});
							liStr += ' <a href="#" onclick="removeLocation('+msg+')">Remove</a> </b></li>';
							$('#locations').append(liStr);
						}
					}
			});
		});
		
		/* ************ */
		
	});
	
	var emailValidator = function(email){
		
		var emailValidator  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
		if (emailValidator.test(email)) {
			return true;
		}else{
			return false;
		}
		
	}
	
	var loadStates = function(country,selector){
		 if (country != 'select') {
		 	$.ajax({
		 		type: "POST",
		 		url: "ajax.php",
		 		data: "act=countrylist&country=" + country,
		 		success: function(msg){
		 			$(selector).html(msg);
		 		}
		 	});
		 }
	}
	
	var removeLocation = function(lId){
		 	var data = "act=remove_location&location="+lId;
			$.ajax({
		 		type: "POST",
		 		url: "ajax.php",
		 		data: data,
		 		success: function(msg){
		 			if(msg == 'ok'){
						$('li#location_'+lId).remove();
					}else{
						alert(msg);
					}
		 		}
		 	});
	}
	
	var checkExpDate = function(tdObj){
		var isInvalidHTML = "<img src='images/icons/error.png' /> it's invalid!"
		var isValidHTML   = "<img src='images/icons/tik.png' />";
		//---
		var nowD = new Date();
		var thisM = nowD.getMonth()+1;
		var thisY = nowD.getFullYear();
		//---
		var thisMD = new Date(thisY,thisM);
		var thisMtime = thisMD.getTime()/1000;
		//---
		var exp_y = tdObj.children('#exp_y').val();
		var exp_m = tdObj.children('#exp_m').val();
		//---
		var entredD = new Date(exp_y,exp_m);
		var entredTime = entredD.getTime()/1000;
		
		if(entredTime < thisMtime){
			tdObj.children('span').removeClass('valid');
			tdObj.children('span').addClass('invalid');
			tdObj.children('span').addClass('red');
			tdObj.children('span').html(isInvalidHTML);
		}else{
			tdObj.children('span').removeClass('invalid');
			tdObj.children('span').addClass('valid');
			tdObj.children('span').html(isValidHTML);
		}
	}


function toggle(nr) {
 if(document.layers) {
  if(document.layers[nr].display == 'block') {
     document.layers[nr].display = 'none';
  } else {
     document.layers[nr].display = 'block';
  }
        
 } else if(document.all) {
  if(document.all[nr].style.display == 'block') {
     document.all[nr].style.display = 'none';
  } else {
     document.all[nr].style.display = 'block';
  }
 } else if(document.getElementById) {
  if(document.getElementById(nr).style.display == 'block') {
     document.getElementById(nr).style.display = 'none';
  } else {
     document.getElementById(nr).style.display = 'block';
  }
 }
}
	

