var confirm;
var defVal;
var pwdillegalChars = /[\W_]/;

function clearText(thefield,type){
	if (thefield.defaultValue==thefield.value){
		thefield.value = "";
		if (type=='pwd'){
			thefield.type = 'password';
		}
	}
}

function writeText(thefield,type){
	if (thefield.value==""){
		thefield.value = thefield.defaultValue;
		if (type=='pwd'){
			thefield.type = 'text';
		}
	}
}

function close_aboutus(){
	$("#TB_overlay").fadeTo("slow", 0.0, function(){tb_remove(); location.reload(true)});
	
}
function aboutus(){
	tb_show(aboutus_title, 'aboutus.php?KeepThis=true&TB_iframe=true&width=600&height=500');
	$("#TB_overlay").fadeTo("slow", 0.95);
}

function verify_search_form(){
	if($('#select_looking').val() == "" && $('#country').val() == ""){
		if($('#select_looking').val() == "" ){
		  tooltip('#select_looking',field_empty,true,true);
		}else{
				delTooltip('#select_looking');
		}
		if($('#country').val() == ""){
		  tooltip('#country',field_empty,true,true);
		}else{
				delTooltip('#country');
		}
		return false;
	}else{
		return true;
	}
}

function verify_join_form(){


	confirm = true;
	if($('#user_name').val() == $('#user_name')[0].defaultValue){
	  tooltip('#user_name',field_empty,true,true,"left");
	  confirm = false;
	}else{
		delTooltip('#user_name');
	}
	if($('#pass_word_sp').val() == $('#pass_word_sp')[0].defaultValue){
	  tooltip('#pass_word_sp',field_empty,true,true,"left");
	  confirm = false;
	}else{
		if (pwdillegalChars.test($('#pass_word_sp').val())) {
			tooltip('#pass_word_sp',password_nonacceptedchars,true,true,"left");
			confirm = false;
		}else{
			delTooltip('#pass_word_sp');
		}
	}
	if($('#user_email').val() == $('#user_email')[0].defaultValue){
	  tooltip('#user_email',field_empty,true,true,"left");
	  confirm = false;
	}else{
		delTooltip('#user_email');
	}
	if($('#state_sp').css('display') == "inline" && $('#state_sp').val() == "0"){
	  tooltip('#state_sp',field_empty,true,true,"left");
	  confirm = false;
	}else{
		delTooltip('#state_sp');
	}
	if($('#city').val() == $('#city')[0].defaultValue){
	  tooltip('#city',field_empty,true,true,"left");
	  confirm = false;
	}else{
		delTooltip('#city');
	}
	if($('#country_sp').val() == ""){
	  tooltip('#country_sp',field_empty,true,true,"left");
	}else{
		delTooltip('#country_sp');
	}
	if($('#gender').val() == ""){
	  tooltip('#gender',field_empty,true,true,"left");
	}else{
		delTooltip('#gender');
	}
	if($('#birthdate').val() == $('#birthdate')[0].defaultValue){
	  tooltip('#birthdate',field_empty,true,true,"left");
	  confirm = false;
	}else{
		delTooltip('#birthdate');
	}
	if(confirm == false){qtipexists = true;}
	if(confirm == true){
		registUser();
	}else{
		$('#signup').val(tryagain);	
	}
	return false;
}

function registUser(){
	var dataString = "user="+$('#user_name').val()+"&password="+$('#pass_word_sp').val()+"&email="+$('#user_email').val()+"&city="+$('#city').val()+"&country="+$('#country_sp').val()+"&gender="+$('#gender').val()+"&birthdate="+$('#birthdate').val();


	if($('#state_sp').css('display') == "inline"){
		dataString = dataString+"&state="+$('#state_sp').val();
	}
	$.ajax({  
	   type: "POST",
	   url: "ajaxfastjoin.php",
	   data: dataString,
	   dataType: "xml",
	   success: parseAlertXml
	});
	$('#signup').val('LOADING');
	$('#signup').attr('disabled', 'disabled');
	return false;  
}

function parseAlertXml(xml) {  
	$(xml).find("alert").each(function() {  
		//find each instance of loc in xml file and wrap it in a link
		if($(this).find("error").text() == "success"){
			$('#join_content').hide("fast");
			$('#success_content').show("slow");
		}else{
			tooltip($(this).find("field").text(),$(this).find("error").text(),true,true,"left");
			$('#signup').val(tryagain);	
			$('#signup').attr('disabled', '');			
		}
	});
};  

function updateqtip() {
	hideTooltip('#user_name'); hideTooltip('#pass_word_sp'); hideTooltip('#user_email'); hideTooltip('#country_sp'); hideTooltip('#gender'); hideTooltip('#state_sp'); hideTooltip('#city'); hideTooltip('#birthdate');
	setTimeout("verify_join_form()", 10);
}

function tooltip(name,message,error,autoopen,side){
if(error == true){
$(name).css({'background-color' : '#ffe3e3'});
}
if(side == "left"){
$(name).qtip({
    content: {text: message},
	position: { adjust: { x: -3, y: 0 }, corner: {	target: 'leftMiddle',	tooltip: 'rightMiddle'} },
	style: {
      width: 200,
      padding: 5,
      background: '#BC0525',
      color: 'white',
      textAlign: 'center',
      border: {width: 7, radius: 5, color: '#BC0525'},
      tip: 'rightMiddle'
	},
	show: { ready: autoopen}
});
}else{
$(name).qtip({
    content: {text: message},
	position: { adjust: { x: 3, y: 0 }, corner: {	target: 'rightMiddle',	tooltip: 'leftMiddle'} },
	style: {
      width: 200,
      padding: 5,
      background: '#BC0525',
      color: 'white',
      textAlign: 'center',
      border: {width: 7, radius: 5, color: '#BC0525'},
      tip: 'leftMiddle'
	},
	show: { ready: autoopen}
});
}
}

function hideTooltip(name){
	if(typeof $(name).data('qtip') == 'object'){
		$(name).qtip('hide');
	}
}

function delTooltip(name){
	if(typeof $(name).data('qtip') == 'object'){
		$(name).qtip('destroy');
		$(name).css({'background-color' : '#FFFFFF'});
	}
}

function intervalFunction(id){
	if(id == '1'){
		$('.spfrimage').fadeOut('slow', function () {setTimeout("$('.spfrimage').fadeIn('slow')", 5000)});
	}else{
		$('.spfrimage2').fadeOut('slow', function () {setTimeout("$('.spfrimage2').fadeIn('slow')", 5000)});
	}
}
function autocmp(){
$("#city").autocomplete("city_autocomplete.php?country="+$('#country_sp option:selected').val(), {
	width: 150,
	selectFirst: false
});
}
$(document).ready(function() {
	$("#city").autocomplete("city_autocomplete.php?country="+$('#country_sp option:selected').val(), {
		width: 150,
		selectFirst: false
	});
	$('#birthdate').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		yearRange: '-100:+1'
	});
	var timeout;
	var timeout2;

	var interval = setInterval("intervalFunction('1')", 10000);
	var interval2 = setInterval("intervalFunction('2')", 10000);
    $('#spfastregister').hover(function() {
			clearTimeout(timeout);
			$('#birthdate').datepicker('enable');
			$('.spfrimage').animate({ width: 0 }, 'fast');
        }, function() {
			clearTimeout(timeout);
			timeout = setTimeout("hideTooltip('#user_name'); hideTooltip('#pass_word_sp'); hideTooltip('#user_email'); hideTooltip('#country_sp'); hideTooltip('#gender'); hideTooltip('#state_sp'); hideTooltip('#city'); hideTooltip('#birthdate'); $('#birthdate').datepicker('disable'); $('#birthdate').datepicker('hide','fast'); $('.spfrimage').animate({ width: 180 }, 'fast');", 20000);

     });
    $('#ssfastsearch').hover(function() {
            clearTimeout(timeout2);
			$('.spfrimage2').animate({ width: 0 }, 'fast');

        }, function() {
			clearTimeout(timeout2);
			timeout2 = setTimeout("hideTooltip('#country'); hideTooltip('#select_looking'); $('.spfrimage2').animate({ width: 180 }, 'fast');", 20000);

     });
	$('#spfastregister').prepend('<form action="ajaxfastjoin.php" method="post" id="homeForm1"></form>');
	$(".homeform1").prependTo("#homeForm1");
	$('#spfastregister').show("slow"); 
	
	$('#ssfastsearch').prepend('<form action="search_result.php" method="post" id="homeForm2"></form>');
	$(".homeform2").prependTo("#homeForm2");
	$('#ssfastsearch').show("slow");
}); 
//##
