var facebooklink = "http://www.facebook.com"; //is the link the facebook connect links use change to match exact url.
$(document).ready(function(){
go2WhichTab(); //runs function below to set the spite to its saved position on page load
	$('.thisTab').mouseover(function(){
		$('#navBlock').css({'background-position':'0px -'+$(this).find('a').attr('rel')+'px'});						
	}).mouseout(function(){
		go2WhichTab();
	}).click(function(e){
		e.preventDefault();
		var tabCookie = $(this).find('a').attr('rel');
		$('#navChoice').val($(this).find('a').attr('rel'));
		Set_Cookie( 'tabPosition', tabCookie, 1, '/', '', '' );
		document.location = $(this).find('a').attr('href');		
	});
	$('.subnavact').click(function(e){e.preventDefault();document.location = $(this).find('a').attr('href');})
	$('.subnavdis').click(function(e){e.preventDefault();document.location = $(this).find('a').attr('href');})
	$('#fbookCon').click(function(e){e.preventDefault();document.location = facebooklink;})
	$('#subfomr').click(function(e){e.preventDefault();askTheDoc();});
});


function askTheDoc(){
	var myName = $('#name').val();
	var myEmail = $('#email').val();
	var conPref = $('#contacttype').val();
	var myphone = $('#phone').val();
	var curPat = $('#currentpatient').val();
	var msgBody = $('textarea#comments').val();
	var DisplayMsg="";var monkey = "good";
	if(myName == ""){DisplayMsg = DisplayMsg+"Name Blank.<br />";monkey="bad";}else{myID = fixedEncodeURIComponent(myName);}
	if(myEmail == ""){DisplayMsg = DisplayMsg+"Email Blank.<br />";monkey="bad";}else{myID = fixedEncodeURIComponent(myEmail);}
	if(conPref == ""){DisplayMsg = DisplayMsg+"Prefered Contact method not selected.<br />";monkey="bad";}else if(conPref == "none"){DisplayMsg = DisplayMsg+"Prefered Contact method not selected.<br />";monkey="bad";}else{myID = fixedEncodeURIComponent(conPref);}
	if(msgBody == ""){DisplayMsg = DisplayMsg+"Question or Comment not provided.<br />";monkey="bad";}else{myID = fixedEncodeURIComponent(msgBody);}

	if(myphone == ""){myphone = "0000000000";}else{myID = fixedEncodeURIComponent(myphone);}
	if(curPat == ""){curPat = "xx";}else if(curPat == "none"){curPat = "xx";}else{myID = fixedEncodeURIComponent(curPat);}
	
	if(monkey == "good"){
		$.getJSON("sub_askDr.php?myName="+myName+"&myEmail="+myEmail+"&conPref="+conPref+"&myphone="+myphone+"&curPat="+curPat+"&msgBody="+msgBody+"&hc=?", function(chkMe){
			var yesno = chkMe.status; var theword = chkMe.messg;
			if(yesno == "good"){$('#res_ask_right').html('Thank you, your message has been sent. We will review it and contact you as soon as possible.');}
			else if(yesno == "bad"){$('#DisplayMsg').show().html(theword);}
			else{$('#DisplayMsg').show().html('An Unexpected Error Occured, try again. #F0001');}
		});
	}else if(monkey == "bad"){$('#DisplayMsg').show().html(DisplayMsg);
	}else{$('#DisplayMsg').show().html('An unknown error occured while processing your login request, please try again.');}
	
}

function fixedEncodeURIComponent(str){return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');}
function decode(txt){return decodeURIComponent(txt.replace(/\+/g,  " "));}
function isNumber(n) {return !isNaN(parseFloat(n)) && isFinite(n);}
function IsNumeric(val) {if(isNaN(parseFloat(val))){return false;}return true}

function go2WhichTab(){
	var defaultTab = $('#navChoice').val();
	$('#navBlock').css({'background-position':'0px -'+defaultTab+'px'});
}
/*if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24 */
function Set_Cookie( name, value, expires, path, domain, secure ){var today = new Date();today.setTime( today.getTime() );if ( expires ){expires = expires * 1000 * 60 * 60 * 24;}var expires_date = new Date( today.getTime() + (expires) );document.cookie = name + "=" +escape( value ) +( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +( ( path ) ? ";path=" + path : "" ) +( ( domain ) ? ";domain=" + domain : "" ) +( ( secure ) ? ";secure" : "" );}
function Get_Cookie( check_name ) {var a_all_cookies = document.cookie.split( ';' );var a_temp_cookie = '';var cookie_name = '';var cookie_value = '';var b_cookie_found = false;for ( i = 0; i < a_all_cookies.length; i++ ){a_temp_cookie = a_all_cookies[i].split( '=' );cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');if ( cookie_name == check_name ){b_cookie_found = true;if ( a_temp_cookie.length > 1 ){cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );}return cookie_value;break;}a_temp_cookie = null;cookie_name = '';}if ( !b_cookie_found ){return null;}}

