﻿/* Clears Text in Search Text Box */
function clearSearchText(){
    var searchText = document.getElementById('searchText')   
    if (searchText.value.toLowerCase() == 'search') {
        searchText.value = '';
    }
}

function GoTo( url ) {
	document.location.href = url;
}

/* Restores Text in Search Text Box */
function restoreSearchText(searchText){
    if ( ! searchText ) {
    	searchText = document.getElementById('searchText')   
    }
    if (searchText.value == '') {
        searchText.value = 'search';
    }
    else {
	searchText.form.submit();
   }
}

/* ------------------------------------------------
$(function () {
	// Setup Tech Evals Accordion
	var divs = $('.tech_evals div');
	divs.each(function (i, div) {
		div = $(div);
		div.find('h3 a').click(function () {
			div.toggleClass('closed');
		});
	});
});
-------------------------------- */

function switchMenu(obj) {

	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}

	var el2 = document.getElementById('h3_' + obj);
	if ( el.style.display != 'none' ) {
		el2.style.backgroundImage = "url(/images/green_arrow_down.gif)";
	}
	else {
		el2.style.backgroundImage = "url(/images/green_arrow_right.gif)";
	}
}

function toggleDiv(div_id)
{
        var el = document.getElementById( div_id );
        var pb = document.getElementById('pageBody');
        if ( ! el ) {
                return false;
        }

	if ( el.style.display == 'none' ) {
                el.style.display = '';
		if ( pb ) {
                	pb.style.display = '';
		}
        }
	else {
                el.style.display = 'none';
		if ( pb ) {
                	pb.style.display = 'none';
		}
        }

	return true;
}

function showPopup( div_id ) {

	var lightbox = {
                        wrapper : $(this).attr('href'),
                        content : $( $(this).attr('href') + ' .lightbox-content' ),
                        callback : $(this).attr('callback'),
	}

	openLightbox( lightbox );
	return false;
}

function checkEnter( event, theField )
{
	if ( event.keycode == 13 ) {
		theField.form.submit();
	}
}

function EmailSignup(theForm)
{
	var em = theForm.nl_email;

	if(em.value.match(/^[A-Za-z0-9]+[A-Za-z0-9\_\-\.]*\@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,5}$/) == null ) {
		em.focus();
		alert("We're sorry, there appears to be a problem with your e-mail address.  Please try again.");
		return false;
	}

	var url = "/Ajax/MailingList?op=join&em=" + em.value;
	
	makeHttpRequest( url, "cb_signup", "GET", '', true );

	return false;
}

function cb_signup( responseXML )
{
  var status = responseXML.getElementsByTagName("status")[0].firstChild.data;

  if ( status != 'OK' ) {
    var msg    = responseXML.getElementsByTagName("message");
    if ( msg ) {
        msg = msg[0].firstChild.data;
        alert( "Error: " + msg );
    }
    else {
        alert( "Error adding email address to mailing list." );
    }
    return false;
  }

  // SUCCESS
  var msg = "Thank you for subscribing to our bi-weekly newsletter.  You will receive your first edition shortly";
  var email    = getXMLValue( responseXML, 'email' );
  if ( email ) {
	msg += " to: " + email;
  }

  var theDiv = document.getElementById( 'nl_div' );

  if ( theDiv ) {
	theDiv.innerHTML = '<b>' + msg + '</b>';
  }
  else {
	alert( msg );
  }
  return false;

}

function BuyNow( stream_name ) {

	var theForm = document.forms['frmBuyNow'];
	if ( theForm ) {
		var theFld = theForm.ind_stream_name;

		if ( theFld ) {
			theFld.value = stream_name;
			theForm.submit();
		}
	}
}

function SubmitNewsletter( theForm, theValidator ) {

	if ( ! theValidator.validate(theForm) ) {
		return false;
	}

	if ( $('#nl-contact_email' ).val() != $('#nl-confirm_email').val() ) {
		alert( "Email addresses do not match" );
		$('#confirm_email').focus() ;
		return false;
	}


        var retURL = document.getElementById( 'retURL' );
        retURL.value = 'http://www.realstorygroup.com/sf_return_null.html';

	SendToSF(theForm);

        var url = "/ajax/MailingList?op=newsletter&em=" + $('#nl-contact_email').val();
        $.ajax( url );

        var msg = "<h3>Newsletter Signup</h3><p>Thank you for signing up for The Real Story Group Newsletter. You will receive a confirmation email immediately and will also receive periodic updates about new research and services from us. You can unsubscribe at any time.</p>";

        $('#newsletter-form-content').html( msg );

        // var lightbox = {
                // wrapper : '#lb-newsletter-form',
                // content : $( '#lb-newsletter-form' + ' .lightbox-content' )
        // }

        // closeLightbox( lightbox );
}

