function add_to_cart(the_id) {


	  $.post("/cgi-bin/links/myfaves.cgi", {
		ID:  the_id,
		action: 'add_ajax'
      }, function(response){
        setTimeout("finishAjax('"+escape(response)+"','" + the_id + "')", 400);
      });



}

function finishAjax(response,the_ID) {


	response = unescape(response);

	var anum = /(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(response)) {

		var the_html = '<a href="javascript: void(0);" onclick="remove_value(' + the_ID + ');">Remove from basket</a>';
		$("#basket"+ the_ID).html(the_html);

		load_current_cookie_vals();

	} else {
		alert("ERROR: " + response);
	}


}

function load_current_cookie_vals(doing_remove) {

	var cookie_value = dogetCookie('IDList');
	var number_of_entries;
	if (cookie_value) {

		var check_array_length = cookie_value.split(" ");
			
		for ( var i=0, len=check_array_length.length; i<len; ++i ){
			var the_id = check_array_length[i];
			var the_html = '<a href="javascript: void(0);" onclick="remove_value(' + the_id + ');">Remove from basket</a>';
			$("#basket"+ the_id).html(the_html);


		}		

		number_of_entries = check_array_length.length;

	} else {
		number_of_entries = '0';
	}


	var cookie_value_saved = dogetCookie('IDListSaved');
	var number_of_entries_saved;
	if (cookie_value_saved) {

		var check_array_length = cookie_value_saved.split(" ");
		number_of_entries_saved = check_array_length.length;

	} else {
		number_of_entries_saved = '0';
	}



		
	$("#basket_faves").html("My selection (" + number_of_entries + " new, and " + number_of_entries_saved + " saved)");

}

function dogetCookie (check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}

}


function remove_value(the_ID) {

	  $.post("/cgi-bin/links/myfaves.cgi", {
		ID:  the_ID,
		action: 'remove_ajax'
      }, function(response){
        setTimeout("finishAjaxRemove('"+escape(response)+"','" + the_ID + "')", 400);
														
      });


}

function finishAjaxRemove(response,the_id) {

	response = unescape(response);

	var anum = /(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(response)) {
		load_current_cookie_vals('do_remove');

		var the_html = '<a href="javascript: void(0);" onclick="add_to_cart(' + the_id + ');">Add to basket</a>';
		$("#basket"+ the_id).html(the_html);

	} else {
		alert("ERROR: " + response);
	}


}


function do_form_checks_faves() {

	var the_name     = $('#populate').val();
	var the_email    = $('#Form_Contact_Email').val();
	var the_comments = $('#Comments').val();
	var the_captcha  = $('#SessionID').val();

	if (the_comments.length < 10 || the_comments == "Message: * + Telephone number") {
		alert("Sorry, your comments are too short.")
		$('#Comments').focus();
		return false;
	}

	if (!the_captcha || the_captcha.length < 4 || the_captcha.length > 4) {
		alert("Sorry, CAPTCHA security image seems either too long, or too short." + the_captcha)
		$('#SessionID').focus();
		return false;
	}

	if (the_name.length < 4 || the_name == "Enter your name") {
		alert("Sorry, you need to enter a valid name")
		$('#populate').focus();
		return false;
	}


	/* we have some inline error checking for this, so ONLY when the email field is blank - will we not do any checks */
	if (!the_email) {
		return false;
	}

	if (echeck(the_email) == false || the_email == "Enter your E-mail") {
		alert("Sorry, you need to enter a valid email address.")
		$('#Form_Contact_Email').focus();
		return false;
	}

	return true;

}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

