/*
* Created By; Alexander Kazakov
* Created Date: 12/18/06
* Last Updated By: Alexander Kazakov
* Last Updated On: 10/02/07
*
* DESCRIPTION:
*
* SHARED BY ALL:
* function $(idValue)
* function isVisibleByName(nameValue)	(OFF)
* function isVisibleById(idValue)
* function toggleField(idValue, mode)
* function phoneNumberFormat(phone)
* function fnAge(dateofBirth)
* EventUtils.addEventListener (target, type, callback, capture)
* requiredForMe(schoolName, arrRequiredFields)
* toggleSection(idValue, mode)
* gmatPopup()
* actPopup()
* satPopup()
* psatPopup()
* mcatPopup()
* hideProgramByValue(value)


* MS FORMS:
* function changeLogo(title)
* function blurb(obj)
* 
*/

/*
*
*/
function goToGPAPopup() {
	open("/leadgentemplate/GPA_popup.asp","GPA","resizable=yes,height=450,width=500");
}

/*
*
*/
function $(idValue){
	return document.getElementById(idValue);
}

/*
function isVisibleByName(nameValue){
	if ( typeof(document.start.eval(nameValue)) != 'undefined' ){
		alert(document.start.eval(nameValue).style.display != 'none');
	}
}
*/

/*
*
*/
function isVisibleById(idValue){
	if ( typeof($(idValue).style) != 'undefined' ){
		return $(idValue).style.display != 'none';
	}
}

/*
*
* MULTISCHOOL GROUP FORMS FUNCTIONS
*
*/

/*
*
*/
function changeLogo(title)
{
	//topLogo AND breadCrumps
	//document.title = title;
	title = title.replace("The Princeton Review -", "");
	
	if (document.getElementById("topBannerLogo"))
	{
		document.getElementById("topBannerLogo").parentNode.innerHTML = "<span id=\"topBannerLogo\">"+ title +"</span>"
	}
	else if (document.getElementById("WWWWrapperSchoolLogo"))
	{
		document.getElementById("WWWWrapperSchoolLogo").innerHTML = "<span id=\"topBannerLogo\">"+ title +"</span>"
	}
	//SetTitleBreadcrumb("<a class=BreadcrumbLink href='http://www.princetonreview.com/home.asp'>Home</a> > <a class=BreadcrumbLink href='http://www.princetonreview.com/mba/default.asp'>B-Schools & Careers</a> > <font class=Breadcrumb>[title]</font>");
}

/*
*
*/
function blurb(obj)
{
	var obj = $(obj);
	
	if ( eval("document.getElementById('"+ obj.id +"Blurb')").style.display == "none" ){
		$(obj.id+"Blurb").style.display = "block";
	}
	else
		$(obj.id+"Blurb").style.display = "none";
}
/********************************************
* FORMATS THE NUMBER IN (xxx) xxx-xxxx format
*********************************************/
function phoneNumberFormat(field, type)
{
	var phone 			= $(field).value;
	var pattern 		= /^(\d{3})[^\d]*(\d{3})[^\d]*(\d{4})$/;
	var UsCanPattern 	= /^\(\d{3}\)\d{3}\-\d{4}$/;
	var dashedPattern 	= /^\d{3}\-\d{3}\-\d{4}$/;
	
	//415-111-2233
	if ( type == "dashed" )
	{
		
		if ( phone.search(pattern) != -1 )
			$(field).value = phone.replace(pattern, "$1\-$2\-$3");
		else
		if ( ! phone.match(dashedPattern) )
			$(field).value = phone.replace(/[- \(\)\.]/g, "");
	} 
	//(415)111-2233
	else{
		if ( phone.search(pattern) != -1 )
			$(field).value = phone.replace(pattern, "\($1\)$2\-$3");
		else
		if ( ! phone.match(UsCanPattern) )
			$(field).value = phone.replace(/[- \(\)\.]/g, "");
	}
}

/*****************************************************
* Capitalize the first character of the given string
******************************************************/
function capitalize(elementName){
	var valueToCap = $(elementName).value;
		
	$(elementName).value = valueToCap.substring(0,1).toUpperCase() + valueToCap.substring(1, valueToCap.length);
}

/*****************************************************
* Calculate age depending on date of birth
******************************************************/
function fnAge(bDay){
	now = new Date()
	if ( bDay != "" )
	{
		bD = bDay.split('/');
		//alert(bD[2] +" "+ bD[0] +" "+ bD[1]);
		if(bD.length == 3){
	   		born = new Date(bD[2], bD[0]-1, bD[1],0,0,0,0);
			years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
			if ( years < 0 )
				return false;
			else
				return years;
		}
	}
	else
		return false;
}

 /** @constructor */
function EventUtils() {
	throw 'RuntimeException: EventUtils is a static utility class and may not be instantiated';
}

/**
*	@access static
*	@param HTMLElement target
*	@param string type
*	@param Function callback
*	@param boolean captures
*/
EventUtils.addEventListener = function (target,type,callback,captures) {
	if (target.addEventListener) {
		// EOMB
		target.addEventListener(type,callback,captures);
	} else if (target.attachEvent) {
		// IE
		target.attachEvent('on'+type,callback,captures);
	} else {
		// IE 5 Mac and some others
		target['on'+type] = callback;
	}
}

/********************************************
' function: 	BannerRotation()
' description: 	object
'*********************************************/
function BannerRotation(){
	throw 'RuntimeException: BannerRotation is a static utility class and may not be instantiated';
}

BannerRotation.promos = function()
{
	this.currentlyShowing = 1;
	this.mousedOver = false;
	this.mousedOutCounting = false;
	this.timeOutId = "";
}

BannerRotation.mouseOver = function()
{
	document.promos.mousedOver = true;
	if (document.promos.timeOutId != "")
	{
		clearTimeout(document.promos.timeOutId);
	}
}

BannerRotation.mouseOut = function()
{
	document.promos.mousedOver = false;
	
	if (document.promos.mousedOutCounting == false)
		BannerRotation.checkIfMousedOverAgain(0);
}

BannerRotation.checkIfMousedOverAgain = function(count)
{
	document.promos.mousedOutCounting = true;
	
	//$('console').innerHTML += " IN checkIfMousedOverAgain; count = "+ count + "<br />";
	
	if (count < 3)
	{
		if (document.promos.mousedOver == false)
		{
			mouseOutTimeOutId = setTimeout("BannerRotation.checkIfMousedOverAgain("+ ++count +")", 1000)
		}
		else
		{
			document.promos.mousedOutCounting = false;
		}
	}
	else
	{
		BannerRotation.cycleAds();
		document.promos.mousedOutCounting = false;
	}
}

BannerRotation.cycleAds = function()
{
	if (! document.promos)
	{
		document.promos = new BannerRotation.promos();
	}
	else
	{
		document.promos.currentlyShowing = document.promos.currentlyShowing % 2 + 1;
	}
	
	//show/hide appropriate ad
	if (document.getElementById('b1promo1')) document.getElementById('b1promo1').style.display = "none";
	if (document.getElementById('b1promo2')) document.getElementById('b1promo2').style.display = "none";
	
	var b1promoObj = document.getElementById('b1promo' + document.promos.currentlyShowing)
	if (b1promoObj) b1promoObj.style.display = "block";
//	document.getElementById('b1promo' + document.promos.currentlyShowing).style.display = "block";
	
	if (document.promos.timeOutId != "")
	{
		clearTimeout(document.promos.timeOutId);
	}
	document.promos.timeOutId = setTimeout("BannerRotation.cycleAds()", 3000);
}

/********************************************
' function: 	requiredNotForAll()
' description: 	updates the displaying of
'				the starts for required not
'				for all schools fields
'*********************************************/
function requiredForMe(schoolName, arrRequiredFields)
{
	var starElement;
	for (field in arrRequiredFields)
	{
		starElement = $(arrRequiredFields[field] +"ID");
		//if school is selected add it's name to the class prop. of the element in the array
		if ( $(schoolName) && $(schoolName).checked )
		{	
			if ( starElement && starElement.className.search(schoolName) == -1 )
				starElement.className += " " + schoolName;
		}
		//if school is not checked, then remove it's name from the class property for that field
		else if ( $(schoolName) && ! $(schoolName).checked )
		{
			if ( starElement && starElement.className.search(schoolName) != -1 ){
				starElement.className = starElement.className.replace(" "+ schoolName, "");
			}
		}
		
		//Deside wether to show star or not
		if ( starElement && starElement.className.search(' ') != -1 )
			starElement.style.visibility = 'visible';
		else if ( starElement && starElement.className.search(' ') == -1 ){
			starElement.style.visibility = 'hidden';
			//alert(starElement.className);
		}
	}
}
	
/**************************************************
* Toggles section on/off. If toggling off, resets 
* all the form elements value of the in elements
**************************************************/
function toggleSection(idValue, mode, type)
{
	if ($(idValue))
	{
		if ( typeof(type) == 'undefined' )
			type = "block";
			
		if ( (typeof(mode) == 'undefined' || mode == '') ){
			if ($(idValue).style.display != "none" && $(idValue).style.display != "" )
				mode = "off";
			else
				mode = "on";
		}
		
		if ( mode == "on" )
		{
			$(idValue).style.display = type;
		}
		else 
		if ( mode == "off" )
		{
			$(idValue).style.display = "none";
			clearInnerFields(idValue);
		}
	}
}

/**************************************************
* Function: toggleField
**************************************************/
function toggleField(idValue, mode, type)
{
	toggleSection(idValue, mode, type);
}

function clearInnerFields(idValue)
{
	var elements;
	
	//Clearing out input and select elements values and resetting checkboxes and radio btn checked property to false
	elements = $(idValue).getElementsByTagName("input");
	for (var i = 0; i < elements.length; i++)
	{
		if ( elements[i].type && elements[i].type != "checkbox" && elements[i].type != "radio" )
		{
			elements[i].value = "";
		}
		else
			elements[i].checked = false;
	}
	//for select lists
	elements = $(idValue).getElementsByTagName("select");
	for (var i = 0; i < elements.length; i++)
		elements[i].value = "";
}

function waitForMs(millis)
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
}

function gmatPopup() {
	var testTaken = $('GMAT_Check').value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if ( testTaken == "Y" )
		toggleSection('gmatPopup', 'on');
	else
		toggleSection('gmatPopup', 'off');
}

function grePopup() {
	var testTaken = $('GRE_Check').value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if ( testTaken == "Y" )
		toggleSection('grePopup', 'on');
	else {
		toggleSection('grePopup', 'off');
	}
}

function LSATPopup() {
	var testTaken = $("LSATDateDay").value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if (testTaken == "Y") {
		toggleSection("LSATPopup", "on");
	} else {
		toggleSection("LSATPopup", "off");
	}
}

function SATPopup(){
	var testTaken = $('SAT_2_Check').value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if ( testTaken == "Y" )
		toggleSection('dynSAT', 'on');
	else
		toggleSection('dynSAT', 'off');
}

function PSATPopup(){
	var testTaken = "";
	
	if ( $('LSATScorePercent') )
		testTaken = $('LSATScorePercent').value;
	else if ( $('PSAT_Check') )
		testTaken = $('PSAT_Check').value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if ( testTaken == "Y" )
		toggleSection('dynPSAT', 'on');
	else
		toggleSection('dynPSAT', 'off');
}

function ACTPopup(){
	
	var testTaken = $('ACT_check').value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if ( testTaken == "Y" )
		toggleSection('dynACT', 'on');
	else
		toggleSection('dynACT', 'off');
}

function mcatPopup() {
	var testTaken = $('MCAT_Check').value;
	testTaken = (testTaken.length > 0) ? testTaken.substr(0, 1) : testTaken;
	
	if ( $('dynMCAT') )
	{
		if ( testTaken == "Y" ){
			toggleSection('dynMCAT', 'on');
			
		}
		else{
			toggleSection('dynMCAT', 'off');
		}
	}
	else if ( $('mcatPopupScore') && $('mcatPopupWhen') )
	{
		if ( testTaken == "Y" ){
			toggleSection('mcatPopupScore', 'on');
			toggleSection('mcatPopupWhen', 'off');
		}
		else if ( testTaken == "N" ){
			toggleSection('mcatPopupScore', 'off');
			toggleSection('mcatPopupWhen', 'on');
		}
		else{
			toggleSection('mcatPopupScore', 'off');
			toggleSection('mcatPopupWhen', 'off');
		}
	}
}


function hideProgramByValue(programField, value)
{
	for (var i = 0; i < document.getElementById(programField).options.length; i++)
	{
		if (document.getElementById(programField).options[i].value == value)
		{
			document.getElementById(programField).options[i] = null;
			i--;
		}
	}
}
