$(document).ready(function(){
	$("#spice_up_info").popupwindow();
	$(".terms_and_conditions").popupwindow();
	$("tr[id*='height_row_']").hide();
	$("tr[id*='build_row_']").hide();
	$("tr[id*='relationship_row_']").hide();
	$("#show_height").html("<strong>Show Options</strong>");
    $("#show_build").html("<strong>Show Options</strong>");
	
	$("#show_height").toggle(
		function(){
			$("#show_height").html("<strong>Hide Options</strong>");
			$("tr[id*='height_row_']").show();
		},
		function(){
			$("#show_height").html("<strong>Show Options</strong>");
			$("tr[id*='height_row_']").hide();
		}
	);
	
	$("#show_build").toggle(
		function(){
			$("#show_build").html("<strong>Hide Options</strong>");
			$("tr[id*='build_row_']").show();
		},
		function(){
			$("#show_build").html("<strong>Show Options</strong>");
			$("tr[id*='build_row_']").hide();
		}
	);
	
	$("#show_relationship").toggle(
		function(){
			$("#show_relationship").html("<strong>Hide Options</strong>");
			$("tr[id*='relationship_row_']").show();
		},
		function(){
			$("#show_relationship").html("<strong>Show Options</strong>");
			$("tr[id*='relationship_row_']").hide();
		}
	);
			
	$("#check-spelling").click(
		function(){
			var check_string = new RegExp('\n|\r','gi');
			var sent_text = $("#self_descr").val().replace(check_string,' ');
			$('#check-spelling').attr("href",'/public/popup/spellcheck.php?text='+sent_text);
		}
	);
	
	$("#check-spelling").popupwindow();
	
	$('#username').change(
		function(){
			$.ajax({
				url: '/config/check-username.php?username='+$(this).val(),
				processData: false,
				dataType: "html",
				success: function(html){
					if(html.length>0){
						alert(html);
						$("#username").val("");
					}
				}
			});
		}
	);
	
	$("input[id*='want_marital_']").click(
		function(){
			var count_want_marital = 0;
			if($(this).attr('id')=="want_marital_1"){				
				if($('#want_marital_1').attr('checked')==true){
					for(var i=2;i<=4;i++){
						$('#want_marital_'+i).attr('checked',"");
					}
				}
			}
			else{
				$('#want_marital_1').attr('checked',"");
				for(var i=2;i<=4;i++){
					if($('#want_marital_'+i).attr('checked')==true){
						count_want_marital++;
					}
				}
				if(count_want_marital==0){
					$('#want_marital_1').attr('checked',"checked");
				}				
			}
		}
	);
	
	$('#add_smile').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+':)'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_grin').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+':D'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_wink').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+';)'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_blush').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+':I'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);		
	$('#add_shock').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+'8O'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_sad').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+':('+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_angry').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+'X('+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_tongue').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+':p'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	$('#add_evil').click(
		function(){
			old_value = $('#self_descr').val();
			var range = $('#self_descr').getSelection();
			first_section = $('#self_descr').val().substr(0,range.end);
			last_section = $('#self_descr').val().substr(range.end);
			new_value = first_section+'>:)'+last_section;
			$('#self_descr').val(new_value);
			return false;
		}
	);
	
	$('#country_id').change(
		country_change = function(){
			$.ajax({
				url: '/config/xml.php?method=get_states'+"&"+'param='+$("#country_id").val(),
				processData: false,
				dataType: "html",
				success: function(html){
					var def = '<option value="">Please choose your State</option>';
					$("#state_id").html(def+html);
				}
			});
			setTimeout('state_change()',300);
		}
	);
	
	$('#state_id').change(
		state_change = function(){
			$.ajax({
				url: '/config/xml.php?method=get_regions'+"&"+'param='+$('#state_id').val(),
				processData: false,
				dataType: "html",
				success: function(html){
				    var def = '<option value="">Please choose your Region</option>';
					$("#region_id").html(def+html);
				}
			});
			setTimeout('region_change()',300);
		}
	);
	
	$('#region_id').change(
		region_change = function(){
			$.ajax({
				url: '/config/xml.php?method=get_locations'+"&"+'country_id='+$('#country_id').val()+"&"+'param='+$('#region_id').val(),
				processData: false,
				dataType: "html",
				success: function(html){
                    var def = '<option value="">Please choose your Location</option>';
					$("#location_id").html(def+html);
				}
			});
		}
	);
	
	spell_check_update = function(old_word,new_word){
		var find_string = new RegExp(old_word,'gi');
		var description = $("#self_descr").val().replace(find_string,new_word);
		$("#self_descr").val(description);
	}
	
	$('#save').click(
		function(){
			var want_contact_count = 0;
            var vr = validateResidency();
            if ( ! vr.stat) {
                alert(vr.msg);
                return false;
            }
            
			$("input[id*='want_contact_by_']").each(
				function(){
					if($(this).attr('checked')===true){
						want_contact_count++;
					}
				}
			);
			if(want_contact_count==0){
				want_contact_count = "";
			}
			$('#count_contacts').val(want_contact_count);
		}
	);
    
    // Added by mark 2009-11-11
    function validateResidency()
    {
        var $c = $('select#country_id');
		if ($c.val()=='1' || $c.val()=='2' || $c.val()=='3') // Australia, USA, UK
        {
            var msg = '';
            var x = $('select#state_id');
            var y = $('select#region_id');
            var z = $('select#location_id');
            
            $(x).parent().css('border', 'none');
            $(y).parent().css('border', 'none');
            $(z).parent().css('border', 'none');
            if ($(x).val()=='') {
                $(x).parent().css('border', '1px solid red');
                $(x).focus();
                msg += "\"Resident State\" is a Required Field\r\n";
            }
            if ($(y).val()=='') {
                $(y).parent().css('border', '1px solid red');
                $(y).focus();
                msg += "\"Resident Region\" is a Required Field\r\n";
            }
            if ($(z).val()=='') {
                $(z).parent().css('border', '1px solid red');
                $(z).focus();
                msg += "\"Resident Location\" is a Required Field\r\n";
            }
            
            if (msg.length>0) return {'msg':msg, 'stat':false};
        }
        return {'msg':'', 'stat':true};
    }
	
	
	check_vowels = function(){
		var return_val = false;
		var vowels = Array("a","e","i","o","u","A","E","I","O","U");
		for(x=0; x < vowels.length; x++){
			if($("#self_descr").val().indexOf(vowels[x])>-1){
				return_val = true;
			}
		}
		if(return_val==false){
			$("#self_descr").attr('class','invalid');
			alert("You must enter a valid self description (not just smileys)");
		}
		else{
			$("#self_descr").attr('class','');
		}
		return return_val;
	}
});
