////////////////////////////////////////////////
// Ajax activity indicator
////////////////////////////////////////////////

Ajax.Responders.register({
        onCreate: function() {
                if($('notification') && Ajax.activeRequestCount> 0)
                        Effect.Appear('notification',{duration: 0.25, queue: 'front'});
        },
        onComplete: function() {
                if($('notification') && Ajax.activeRequestCount == 0)
                        Effect.Fade('notification',{duration: 0.25, queue: 'end'});
        }
});


////////////////////////////////////////////////
// Basic navigation functions
////////////////////////////////////////////////

function goSearch() {
	$('navStatus_right_content').style.display = 'none';
	$('navStatus_left_content').style.display = 'none';

	new Ajax.Updater('navStatus_right_content',
		'/includes/NavSearch.php',
		{
			method:'get',
			asynchronous:true,
			onComplete: showResponse,
			onSuccess:fadeIn
		});
		
		
	function showResponse(req) {
		$('navStatus_right_content').innerHTML = req.responseText;
		activateSearch();
	}

}

function goFirm() {
	$('navStatus_right_content').style.display = 'none';
	$('navStatus_left_content').style.display = 'none';

	new Ajax.Updater('navStatus_right_content',
		'/includes/NavFirm.php',
		{
			asynchronous:true,
			onSuccess:fadeIn
		});
	}


////////////////////////////////////////////////
// Attorney function
////////////////////////////////////////////////


function goAttorney() {
	$('navStatus_right_content').style.display = 'none';
	new Effect.Appear('navStatus_left_content',{duration: .25, queue: 'end'});

	new Ajax.Updater('navStatus_right_content',
		'/includes/NavAttorney.php',
		{
			asynchronous:true,
			onSuccess:fadeIn
		});	

//	new Ajax.Updater('navStatus_left_content',
//		'/includes/NavAttorney2.php',
//		{
//			asynchronous:true,
//			onSuccess:fadeIn
//		});		
}
	
function goOffice() {
	$('navStatus_right_content').style.display = 'none';
	$('navStatus_left_content').style.display = 'none';

	new Ajax.Updater('navStatus_right_content',
		'/includes/NavOffice.php',
		{
			asynchronous:true,
			onSuccess:fadeIn
		});
	}

function goPracticeGroups() {
	$('navStatus_right_content').style.display = 'none';
	$('navStatus_left_content').style.display = 'none';

	new Ajax.Updater('navStatus_right_content',
		'/includes/NavPracticeGroups.php',
		{
			asynchronous:true
		});
		
	new Ajax.Updater('navStatus_left_content',
		'/includes/clear.php',
		{
			asynchronous:true,
			onSuccess:fadeIn
		});

	}
	
function goSend2Friend() {
	$('navStatus_right_content').style.display = 'none';
	$('navStatus_left_content').style.display = 'none';

	new Ajax.Updater('navStatus_right_content',
		'/includes/NavSend2Friend.php',
		{
			asynchronous:true
		});
		
	new Ajax.Updater('navStatus_left_content',
		'/includes/clear.php',
		{
			asynchronous:true,
			onSuccess:fadeIn
		});

	}

function goCareers() {
	$('navStatus_right_content').style.display = 'none';
	$('navStatus_left_content').style.display = 'none';

	new Ajax.Updater('navStatus_right_content',
		'/careers/includes/NavCareers.php',
		{
			asynchronous:true,
			onSuccess:fadeIn
		});
	}


	
function fadeIn() {
	new Effect.Appear('navStatus_right_content',{duration: .25, queue: 'end'});
	}


////////////////////////////////////////////////
// Advanced attorney navigation functions
////////////////////////////////////////////////
	
function initializeAttorney() {
	window.office = '';
	window.pg = '';
	//window.alert('office = ' + escape(window.office) + ' & pg = ' + escape(window.pg));

}

function setOffice(office) {
	window.office = office;
	//window.alert('office = ' + escape(window.office) + ' & pg = ' + escape(window.pg));
	sqlAjax();
}


function setPracticeGroup(pg) {
	window.pg = pg;
	//window.alert('office = ' + escape(window.office) + ' & pg = ' + escape(window.pg));
	sqlAjax();
	
}


function sqlAjax() {

		pars = 'office=' + escape(window.office) + '&practicegroup=' + escape(window.pg);
    	var mySqlAjax = new Ajax.Request('http://cw.ciprianidesign.com/includes/findAttorney.php',
    	{method: 'get', parameters: pars, onComplete:sqlAjaxResponse});
}


function sqlAjaxResponse(response) {
	$('attorneyStatus').style.display = 'none';
   	$('findattorney').style.display = 'none';
    refineAttorneyStatus();
	$('findattorney').innerHTML = response.responseText;
	fadeIn();
	new Effect.Appear('attorneyStatus',{duration: .25, queue: 'end'});
	new Effect.Appear('findattorney',{duration: .25, queue: 'end'});

}

function refineAttorneyStatus() {
	if (window.office == '' && window.pg == '') {
//		$('attorneyStatus').innerHTML = '<span class=status>Attorneys from <b><font color="#BBBBBB">ALL offices</font></b> in <b><font color="#BBBBBB">ALL practice groups</font></b>:</span>';
		$('attorneyStatus').innerHTML = '<h2>Attorneys in All offices / ALL practice groups</h2>';
	} else if (window.pg == '') {
//		$('attorneyStatus').innerHTML = '<span class=status>Attorneys from the <b><font color="#BBBBBB">' + escape(window.office) + '</font></b> office in <b><font color="#BBBBBB">ALL practice groups</font></b>:</span>';
		$('attorneyStatus').innerHTML = '<h2>Attorneys in ' + escape(window.office) + ' / ALL practice groups</h2>';
	} else if (window.office == '') {
//		$('attorneyStatus').innerHTML = '<span class=status>Attorneys from <b><font color="#BBBBBB">ALL offices</font></b> in the <b><font color="#BBBBBB">' + window.pgName + '</font></b> group:</span>';
		$('attorneyStatus').innerHTML = '<h2>Attorneys in ALL offices / ' + window.pgName + '</h2>';
	} else {
//		$('attorneyStatus').innerHTML = '<span class=status>Attorneys from the <b><font color="#BBBBBB">' + escape(window.office) + '</font></b> office in the <b><font color="#BBBBBB">' + window.pgName + '</font></b> group:</span>';
		$('attorneyStatus').innerHTML = '<h2>Attorneys in ' + escape(window.office) + ' / ' + window.pgName + '</h2>'; 

	}
}

////////////////////////////////////////////////
// Header photo display
////////////////////////////////////////////////

function getPhoto() {
	$('photo').style.display = 'none';
	var p = window.photoImage;
	$('photo').innerHTML = '<img src=\"/images/photos/' + p.toLowerCase() + '.jpg\">';
	Effect.Appear('photo',{duration: 1.5, queue: 'end'});
}



////////////////////////////////////////////////
// Journal Subscription functions
////////////////////////////////////////////////

function goJournal() {
	$('navStatus_right_content').style.display = 'none';
	new Ajax.Updater('navStatus_right_content',
		'/includes/NavJournal.php',
		{
			asynchronous:true,
			onSuccess:fadeIn,
			onComplete: showJournalResponse
		});
	
	function showJournalResponse(Journalreq) {
		$('navStatus_right_content').innerHTML = Journalreq.responseText;
		activateJournal();
		}
	}


function activateJournal() {
    if ($('Journalform')) {
		$('JournalEmail').value = 'Enter an email address here...';	
		$('JournalEmail').onfocus = focusEmail;

		JournalSubmit.value = 'Subscribe';
        $('Journalform').onsubmit = function() { 
        	doJournal();return false; 
        	};

        is_sending = false;

    }
}


function doJournal() {
    if (is_sending) return false; 
    JournalEmail = $F('JournalEmail');
    mode = $F('__mode');
    blog_id = $F('blog_id');
    redirect = $F('_redirect');
	
    if (JournalEmail == '' || JournalEmail == 'Enter an email address here...') { $('Journalresults').innerHTML = "Please enter your email address"; return false; }
	
    is_searching = true;
    JournalSubmit.value = 'Sending...';
    JournalSubmit.disabled = true;
    
    pars = 'email=' + escape(JournalEmail) + '&__mode=' + escape(mode) + '&_redirect=' + escape(redirect) + '&blog_id=' + escape(blog_id);
    var myAjax = new Ajax.Request('/mt/mt-add-notify.cgi', 
          {method: 'post', parameters: pars, onComplete:doJournalResponse});
}


function doJournalResponse(response) {
	$('Journalresults').innerHTML = response.responseText;
	resetJournalForm();
}


function resetJournalForm() {
    JournalSubmit.value = 'Subscribe Again';
    JournalSubmit.disabled = false;
    is_sending = false;
    activateJournal();
}



function focusEmail() {
    if ($F('JournalEmail') == 'Enter an email address here...') $('JournalEmail').value = '';
}


////////////////////////////////////////////////
// SMS functions
////////////////////////////////////////////////

function goSMS() {
	$('navStatus_right_content').style.display = 'none';
	new Ajax.Updater('navStatus_right_content',
		'/mt/sms/sms_form.php',
		{
			method:'get',
			asynchronous:true,
			onSuccess: fadeIn,
			onComplete: showSMSResponse
		});

	function showSMSResponse(SMSreq) {
		$('navStatus_right_content').innerHTML = SMSreq.responseText;
		activateSMS();
	}
}


function activateSMS() {
    if ($('SMSform')) {
		$('SMSmessage').value = 'Enter your message here...';
		$('SMStoNumber').value = 'Enter your 10-digit phone number...';
		
		$('SMSmessage').onfocus = focusSMSmessage;
        $('SMStoNumber').onfocus = focusSMStoNumber;

		SMSSubmit.value = 'Send to Phone';
        $('SMSform').onsubmit = function() { 
        	doSMS();return false; 
        	};

        is_sending = false;

    }
}


function doSMS() {
    if (is_sending) return false; 
    SMSm = $F('SMSmessage');
    SMSt = $F('SMStoNumber');
    SMSn = $F('SMSnetwork');
	
	//window.alert(isInteger(SMSt));
    if (SMSm == '' || SMSm == 'Enter your message here...') { $('SMSresults').innerHTML = "Please enter your message"; return false; }
	if (SMSt == '' || SMSt == 'Enter your 10-digit phone number...') { $('SMSresults').innerHTML = "Please enter a valid phone number"; return false; }
	if (isInteger(SMSt) == false || SMSt.length != 10) { $('SMSresults').innerHTML = "Please enter a 10-digit phone number"; return false; }
	if (SMSn == '' || SMSn == 'default') { $('SMSresults').innerHTML = "Please select your mobile service carrier"; return false; }
	
    is_searching = true;
    SMSSubmit.value = 'Sending...';
    SMSSubmit.disabled = true;
    
    pars = 'SMSmessage=' + escape(SMSm) + '&SMStoNumber=' + escape(SMSt) + '&SMSnetwork=' + escape(SMSn) + '&SMSsubject=' + escape($F('SMSsubject'));
    var myAjax = new Ajax.Request('/mt/sms/sms.php', 
          {method: 'get', parameters: pars, onComplete:doSMSResponse});
}

function doSMSResponse(response) {
	$('SMSresults').innerHTML = response.responseText;
	resetSMSForm();
}

function resetSMSForm() {
    SMSSubmit.value = 'Send to Phone';
    SMSSubmit.disabled = false;
    is_sending = false;
    activateSMS();
}

function focusSMSmessage() {
    if ($F('SMSmessage') == 'Enter your message here...') $('SMSmessage').value = '';
}

function focusSMStoNumber() {
    if ($F('SMStoNumber') == 'Enter your 10-digit phone number...') $('SMStoNumber').value = '';
}



////////////////////////////////////////////////
// SMS Phone validation
////////////////////////////////////////////////

var digits = "0123456789";
var phoneNumberDelimiters = "";
var validWorldPhoneChars = phoneNumberDelimiters + "";
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone) {
	s = stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(Phone){
	
	if ((Phone.value==null)||(Phone.value=="")){
		$('SMSresults').innerHTML = "Please Enter a Valid Phone Number";
		$('SMStoNumber').focus();
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		$('SMSresults').innerHTML = "Please Enter a Valid Phone Number";
		$F('SMStoNumber') = "";
		$('SMStoNumber').focus();
		return false
	}
	return true
 }
 
 

////////////////////////////////////////////////
// Text selection detection
////////////////////////////////////////////////

function getSel() {

        var message = '';
        var foundIn = '';

        if (window.getSelection)
        {
                message = window.getSelection();
        }
        else if (document.getSelection)
        {
                message = document.getSelection();
        }
        else if (document.selection)
        {
                message = document.selection.createRange().text;
        }
        else return;
//        document.forms[0].message.value = message;
        window.alert(message);
}

//Event.observe(window, 'mouseup', window.alert('done'));



////////////////////////////////////////////////
// Global event capture functions
////////////////////////////////////////////////

Event.observe(window, 'load', initializeAttorney, false);
Event.observe(window, 'load', getPhoto, false);


