var searchDefaultValue = "Enter Keywords";
var nameDefaultValue = "-kid's name-";
var ageDefaultValue = "-kid's age-";
var mydate=new Date();

var year=mydate.getYear();

if (year < 1000)
	year+=1900;

var day=mydate.getDay()
var month=mydate.getMonth()+1;

if (month<10)
	month="0"+month;

var daym=mydate.getDate();
if (daym<10)
	daym="0"+daym;
var hours = mydate.getHours();
var mins = mydate.getMinutes();
var secs = mydate.getSeconds();

var dateDefaultValue = year+"-"+month+"-"+daym;
var stateDefaultValue = "-state-(optional)";
var countryDefaultValue = "-country-(optional)";
 jQuery(document).ready(function() {
	//setInterval("ajaxTime()",1000); 
	//jQuery(".date-pick").datepicker({ dateFormat: 'yy-mm-dd' });
/*jQuery("#kid_quotes_form a").bind("click", function(e){ 
		e.preventDefault(); 
});
jQuery("#kids_form").bind("submit", function(event){ 
		event.preventDefault(); 
});*/
	if(!jQuery('#search').val()) {
		jQuery('#search').val(searchDefaultValue);
	}
	
	jQuery('#search').focus(function() {
		if(jQuery(this).val() == searchDefaultValue)
			jQuery(this).val('');
	}).blur(function() {
		if(!jQuery(this).val())
			jQuery(this).val(searchDefaultValue);
	});

	if(!jQuery('#kid_name').val()) {
		jQuery('#kid_name').val(nameDefaultValue);
	}
	
	jQuery('#kid_name').focus(function() {
		if(jQuery(this).val() == nameDefaultValue)
			jQuery(this).val('');
	}).blur(function() {
		if(!jQuery(this).val())
			jQuery(this).val(nameDefaultValue);
	});

	if(!jQuery('#kid_age').val()) {
		jQuery('#kid_age').val(ageDefaultValue);
	}
	
	jQuery('#kid_age').focus(function() {
		if(jQuery(this).val() == ageDefaultValue)
			jQuery(this).val('');
	}).blur(function() {
		if(!jQuery(this).val())
			jQuery(this).val(ageDefaultValue);
	});

	jQuery('#kids_quote_date').val(dateDefaultValue);

	if(!jQuery('#kid_state').val()) {
		jQuery('#kid_state').val(stateDefaultValue);
	}
	
	jQuery('#kid_state').focus(function() {
		if(jQuery(this).val() == stateDefaultValue)
			jQuery(this).val('');
	}).blur(function() {
		if(!jQuery(this).val())
			jQuery(this).val(stateDefaultValue);
	});
	
	if(!jQuery('#kid_country').val()) {
		jQuery('#kid_country').val(countryDefaultValue);
	}
	
	jQuery('#kid_country').focus(function() {
		if(jQuery(this).val() == countryDefaultValue)
			jQuery(this).val('');
	}).blur(function() {
		if(!jQuery(this).val())
			jQuery(this).val(countryDefaultValue);
	});

});


function ajaxTime() {
	var submitTime_options = { 'type': 'POST', 'url': jQuery('#timeUrl').val(),  'dataType': 'html', 'callbackFn': 'time_callback' };
	processAjax(submitTime_options);

}
function time_callback(html) {
	jQuery('#kids_quote_date').val(html);
}

function submit(submitUrl) {
	if(jQuery('#kid_quote').val() == "") {
		alert('Please enter the kid quote');
		jQuery('#kid_quote').focus();
		return false;
	}
	if (jQuery('#kid_name').val() == "" || jQuery('#kid_name').val() == nameDefaultValue) {
		alert('Please enter the kid name');
		jQuery('#kid_name').focus();
		return false;
	}
	if (jQuery('#kid_age').val() == "" || jQuery('#kid_age').val() == ageDefaultValue) {
		alert('Please enter the kid age');
		jQuery('#kid_age').focus();
		return false;
	} 
	else {
		if (jQuery('#kid_state').val() != "" ) {
		if(jQuery('#kid_state').val() != "-state-(optional)") {
			$kid_state =  ", "+jQuery('#kid_state').val();
		} else {
			$kid_state = "";
		}
		
	} if (jQuery('#kid_country').val() != "" ) {
		if(jQuery('#kid_country').val() != "-country-(optional)") {
			$kid_country =  ", "+jQuery('#kid_country').val();
		} else {
			$kid_country = "";
		}
		
	}
		var patientFilter = {
		post_status: 'draft',
		post_content: jQuery('#kid_quote').val(),
		post_title: jQuery('#kid_name').val()+", Age "+jQuery('#kid_age').val()+$kid_state+$kid_country,
		post_date: jQuery('#kids_quote_date').val(),
		post_type: 'post'
			};

		jQuery('#kid_quotes_form').hide("slow");
		var jsonData = $.toJSON(patientFilter);
		//var submit_options = { 'type': 'POST', 'url': submitUrl, 'data': 'post_data='+ jsonData, 'dataType': 'json', 'callbackFn': 'post_callback' };
		//processAjax(submit_options);
		
	jQuery.ajax({		
		url: submitUrl,
		data: 'post_data='+ jsonData,
		cache: false,
		async: false,
		type: "POST",
		dataType: "json",
		success: function(json){
			if(json.submit == "not_logged_in") {
				var html = '<div><b>Please log in to submit your kidtalk</b></div>';
				jQuery('#kid_quotes_form').after(html);
				//window.location.href = "../login";
			}
			else if(json.submit != '0' ) {
			var html = '<div><b>Thank you, your Kidtalk has been submitted and saved to your Kidtalk Journal!</b></div>';
			jQuery('#kids_form')[0].reset();
			jQuery('#kid_name').val(nameDefaultValue);
			jQuery('#kid_age').val(ageDefaultValue);
			jQuery('#kids_quote_date').val(dateDefaultValue);
			jQuery('#kid_state').val(stateDefaultValue);
			jQuery('#kid_country').val(countryDefaultValue);
			jQuery('#kid_quotes_form').hide("slow");
			jQuery('#kid_quotes_form').after(html);
			} else {
				alert("Kidtalk not submitted");
				jQuery('#kid_quotes_form').show("slow");
			}
		}	
						
	});
		return;
	}
return false;
}

function post_callback(json) {
	if(json == "not_logged_in") {
		var html = '<div><b>Please log in to submit your kidtalk</b></div>';
		jQuery('#kid_quotes_form').after(html);
	}
	else if(json != "not_logged_in" && json != '0' ) {
	var html = '<div><b>Thank you, your Kidtalk has been submitted and saved to your Kidtalk Journal!</b></div>';
	jQuery('#kids_form')[0].reset();
	jQuery('#kid_name').val(nameDefaultValue);
	jQuery('#kid_age').val(ageDefaultValue);
	jQuery('#kids_quote_date').val(dateDefaultValue);
	jQuery('#kid_state').val(stateDefaultValue);
	jQuery('#kid_country').val(countryDefaultValue);
	jQuery('#kid_quotes_form').hide("slow");
	jQuery('#kid_quotes_form').after(html);
	} else {
		alert("Kidtalk not submitted");
		jQuery('#kid_quotes_form').show("slow");
	}
}
function processAjax(ajax_options)
{
	var ajaxManager = $.manageAjax({manageType: 'sync', maxReq: 0});
	var resDiv_obj = jQuery('#'+ajax_options.resultantDiv).get(0);
	var p_statusInfo = jQuery('<span></span>').attr('id','statusInfo');
	if(resDiv_obj) {
		if(jQuery('#statusInfo').get(0)) jQuery("#statusInfo").remove();
		jQuery('#'+ajax_options.resultantDiv).prepend(p_statusInfo);
		if((ajax_options.resultantDiv != 'pageContainer') && (ajax_options.resultantDiv != 'updaterclick')) 
		jQuery('#statusInfo').text(loadingText).prepend(indicatorImg);		
	}
	ajaxManager.add({ 
		type: 	ajax_options.type,
		url: 	ajax_options.url,		
		data: 	ajax_options.data,
		dataType: ajax_options.dataType,
		/*
		timeout: 90000,
	    error: function(){
			if(resDiv_obj) {
	    	    jQuery('#statusInfo').html('Error loading document').addClass('error');
			}
	    },
		*/
		success: function(ajaxResponse) 
		{
			if(ajax_options.dataType=='xml') 
			{
				var responseXML = { 'ResData': jQuery(ajaxResponse).find('ResData').eq(0).text(), 'SuccessMsg': jQuery(ajaxResponse).find('SuccessMsg').eq(0).text(), 'ErrorMsg':jQuery(ajaxResponse).find('ErrorMsg').eq(0).text() };
				if(resDiv_obj && responseXML.ResData) {
					jQuery('#'+ajax_options.resultantDiv).html(responseXML.ResData);	
					jQuery('#'+ajax_options.resultantDiv).prepend(p_statusInfo);	
				}
				if (responseXML.SuccessMsg) {
					jQuery("#statusInfo.error").removeClass('error');
					jQuery('#statusInfo').html(responseXML.SuccessMsg);
				}else if (responseXML.ErrorMsg) {
					jQuery('#statusInfo').html(responseXML.ErrorMsg).addClass('error');				
				}
			} else {
				
				if(resDiv_obj && ajaxResponse) {
					jQuery('#'+ajax_options.resultantDiv).html(ajaxResponse);
				}
			}
			if(ajax_options.callbackFn) {
				if(ajax_options.callbackParm) {
					eval(ajax_options.callbackFn)(ajax_options.callbackParm,ajaxResponse);
				} else {
					eval(ajax_options.callbackFn)(ajaxResponse);
				}
			}
		}
	});
	return false;
}

