// JavaScript Document for Biz Trip Central. Copyrights 2009


//function invokes cfwindow to alerts user their doc is being uploaded
function uploadingAlert(msg)
{
	switch (msg)
	{
		//if called from 'calculator' form (home, order, prices pages)
		case 'calc':
			if (document.frmOnlineQuote.customer_file.value != '')
			{
				ColdFusion.Window.show('winUploading');
			}
			break;
		//if called from contact form (support, rfq, contact pages)
		case 'contact':
			if (document.frmContact.attachment.value != '')
			{
				ColdFusion.Window.show('winUploading');
			}	
			break;
	}
	
}

//function determines the password's strength
function passwordStrength(password, descID, strengthID)
{
	var desc = new Array();
    desc[0] = "Very Weak Password";
    desc[1] = "Weak Password";
	desc[2] = "Ok Password";
	desc[3] = "Good Password";
	desc[4] = "Strong Password";
	desc[5] = "Excellent Password!";
	var score = 0;
	
	//if password bigger than or equal 6 give 1 point
	if (password.length >= 6) score++;
	
	//if password bigger than or equal 10 chars give another 1 point
	if (password.length >= 8) score++;
	
	//if password has at least one number give 1 point
	if (password.match(/\d+/)) score++;
	
	//if password has both lower and uppercase characters give 1 point      
	if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

	//if password has at least one special caracther give 1 point
	if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
	//display password's rating 
	document.getElementById(descID).innerHTML = desc[score];
	//display password's strength color bar
	document.getElementById(strengthID).className = "strength" + score;
	//if pwd field is blank, display "Password Meeter"
	if (password.length < 1){document.getElementById(descID).innerHTML = "Password Meeter";}
}

//function to confirm user's chosen pwd during registration and during pwd change
function confirmPassword(pwd1, pwd2, pwdConfirmMsg, submitButton, maginClass)
{
	if (document.getElementById(pwd1).value != pwd2.value)
	{
		//if password don't match, alert user and disable 'submit' button
		document.getElementById(pwdConfirmMsg).innerHTML = "<span class='red'><b>X</b></span><br><span class='red " + maginClass + "' style='display:block'>Passwords don't match - try again!</span>";
		document.getElementById(submitButton).disabled = 1;	
	}
	else
	{
		//passwords mtach, make sure 'submit' button is enabled
		document.getElementById(pwdConfirmMsg).innerHTML = "<span class='green'><b>&radic;</b></span>";
		document.getElementById(submitButton).disabled = 0;
	}
}

//function masks phone # as entered by user to (000) 000-0000
// [dFilter] - A Numerical Input Mask for JavaScript
// Written By Dwayne Forehand - March 27th, 2003
// Please reuse & redistribute while keeping this notice.

var dFilterStep

function dFilterStrip (dFilterTemp, dFilterMask)
{
    dFilterMask = replace(dFilterMask,'#','');
    for (dFilterStep = 0; dFilterStep < dFilterMask.length++; dFilterStep++)
		{
		    dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
		}
		return dFilterTemp;
}

function dFilterMax (dFilterMask)
{
 		dFilterTemp = dFilterMask;
    for (dFilterStep = 0; dFilterStep < (dFilterMask.length+1); dFilterStep++)
		{
		 		if (dFilterMask.charAt(dFilterStep)!='#')
				{
		        dFilterTemp = replace(dFilterTemp,dFilterMask.charAt(dFilterStep),'');
				}
		}
		return dFilterTemp.length;
}

function dFilter (key, textbox, dFilterMask)
{
		dFilterNum = dFilterStrip(textbox.value, dFilterMask);
		
		if (key==9)
		{
		    return true;
		}
		else if (key==8&&dFilterNum.length!=0)
		{
		 	 	dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);
		}
 	  else if ( ((key>47&&key<58)||(key>95&&key<106)) && dFilterNum.length<dFilterMax(dFilterMask) )
		{
        dFilterNum=dFilterNum+String.fromCharCode(key);
		}

		var dFilterFinal='';
    for (dFilterStep = 0; dFilterStep < dFilterMask.length; dFilterStep++)
		{
        if (dFilterMask.charAt(dFilterStep)=='#')
				{
					  if (dFilterNum.length!=0)
					  {
				        dFilterFinal = dFilterFinal + dFilterNum.charAt(0);
					      dFilterNum = dFilterNum.substring(1,dFilterNum.length);
					  }
				    else
				    {
				        dFilterFinal = dFilterFinal + "";
				    }
				}
		 		else if (dFilterMask.charAt(dFilterStep)!='#')
				{
				    dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep); 			
				}
		    dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
		}


		textbox.value = dFilterFinal;
    return false;
}

function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;

    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;

    var newstr = fullString.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

    return newstr;
}

//function will toggle forms on sign-up page
var observer = {};

observer.nextEffect = false;
observer.onPostEffect = function(e){
	if (this.nextEffect)
	{
		var eff = this.nextEffect;
		setTimeout(function(){eff.start();}, 10);
	}

	this.nextEffect = false;
}

function slideUserForms(currentPanel)
{
    // The list of all the panels that need sliding
	var panels = ['user_form', 'contact_admin_form'];
	var opened = -1;

	// Let's check if we have an effect for each of these sliding panels
	if (typeof effects == 'undefined')
		effects = {};

	for (var i=0; i < panels.length; i++)
	{
		if (typeof effects[panels[i]] == 'undefined'){
			effects[panels[i]] = new Spry.Effect.Slide(panels[i], {from: '0%', to: '100%', toggle: true});
			effects[panels[i]].addObserver(observer);
		}
		 
		if (effects[panels[i]].direction == Spry.forwards && currentPanel != panels[i])
			opened = i;

		//prevent too fast clicks on the buttons
		if (effects[panels[i]].direction == Spry.backwards && effects[panels[i]].isRunning)
		{
			observer.nextEffect = effects[currentPanel];
			return;
		}
	}
	if (opened != -1)
	{
		observer.nextEffect = effects[currentPanel];
		effects[panels[opened]].start();
	} 
	else //if (effects[currentPanel].direction != Spry.forwards)
	{
		effects[currentPanel].start();
	}	
};

function slideEventForms(currentPanel)
{
    // The list of all the panels that need sliding
	var panels = ['hotel_form', 'airline_form', 'carpool_form', 'docs_form', 'event_invitations_form', 'edit_event_form', 'email_participants_form'];
	var opened = -1;

	// Let's check if we have an effect for each of these sliding panels
	if (typeof effects == 'undefined')
		effects = {};

	for (var i=0; i < panels.length; i++)
	{
		if (typeof effects[panels[i]] == 'undefined'){
			effects[panels[i]] = new Spry.Effect.Slide(panels[i], {from: '0%', to: '100%', toggle: true});
			effects[panels[i]].addObserver(observer);
		}
		 
		if (effects[panels[i]].direction == Spry.forwards && currentPanel != panels[i])
			opened = i;

		//prevent too fast clicks on the buttons
		if (effects[panels[i]].direction == Spry.backwards && effects[panels[i]].isRunning)
		{
			observer.nextEffect = effects[currentPanel];
			return;
		}
	}
	if (opened != -1)
	{
		observer.nextEffect = effects[currentPanel];
		effects[panels[opened]].start();
	} 
	else //if (effects[currentPanel].direction != Spry.forwards)
	{
		effects[currentPanel].start();
	}	
};


function slideAttendeeForms(currentPanel)
{
    // The list of all the panels that need sliding
	var panels = ['hotel_form', 'airline_form', 'carpool_form'];
	var opened = -1;

	// Let's check if we have an effect for each of these sliding panels
	if (typeof effects == 'undefined')
		effects = {};

	for (var i=0; i < panels.length; i++)
	{
		if (typeof effects[panels[i]] == 'undefined'){
			effects[panels[i]] = new Spry.Effect.Slide(panels[i], {from: '0%', to: '100%', toggle: true});
			effects[panels[i]].addObserver(observer);
		}
		 
		if (effects[panels[i]].direction == Spry.forwards && currentPanel != panels[i])
			opened = i;

		//prevent too fast clicks on the buttons
		if (effects[panels[i]].direction == Spry.backwards && effects[panels[i]].isRunning)
		{
			observer.nextEffect = effects[currentPanel];
			return;
		}
	}
	if (opened != -1)
	{
		observer.nextEffect = effects[currentPanel];
		effects[panels[opened]].start();
	} 
	else //if (effects[currentPanel].direction != Spry.forwards)
	{
		effects[currentPanel].start();
	}	
};

//function deteremines if USA states will display or if simply put a province field
function stateProv(country, stateOptions, frmState)
{
	if (document.getElementById(country).value == "US")
	{
		document.getElementById(stateOptions).innerHTML="<select name='" + frmState + "' id='" + frmState + "' required='no' title='Please select your state'><option value='' selected='selected'>-- Select US State --</option><option value='AK'>AK</option><option value='AL'>AL</option><option value='AR'>AR</option><option value='AZ'>AZ</option><option value='CA'>CA</option><option value='CO'>CO</option><option value='CT'>CT</option><option value='DC'>DC</option><option value='DE'>DE</option><option value='FL'>FL</option><option value='GA'>GA</option><option value='HI'>HI</option><option value='IA'>IA</option><option value='ID'>ID</option><option value='IL'>IL</option><option value='IN'>IN</option><option value='KS'>KS</option><option value='KY'>KY</option><option value='LA'>LA</option><option value='MA'>MA</option><option value='MD'>MD</option><option value='ME'>ME</option><option value='MI'>MI</option><option value='MN'>MN</option><option value='MO'>MO</option><option value='MS'>MS</option><option value='MT'>MT</option><option value='NC'>NC</option><option value='ND'>ND</option><option value='NE'>NE</option><option value='NH'>NH</option><option value='NJ'>NJ</option><option value='NM'>NM</option><option value='NV'>NV</option><option value='NY'>NY</option><option value='OH'>OH</option><option value='OK'>OK</option><option value='OR'>OR</option><option value='PA'>PA</option><option value='PR'>PR</option><option value='RI'>RI</option><option value='SC'>SC</option><option value='SD'>SD</option><option value='TN'>TN</option><option value='TX'>TX</option><option value='UT'>UT</option><option value='VA'>VA</option><option value='VT'>VT</option><option value='WA'>WA</option><option value='WI'>WI</option><option value='WV'>WV</option><option value='WY'>WY</option><option value='Other'>Other</option></select>";
	}
	else if (document.getElementById(country).value == "CA")
	{
		document.getElementById(stateOptions).innerHTML="<select name='" + frmState + "' id='" + frmState + "' required='no' title='Please select your state'><option value='' selected='selected'>-- Select Canadian Province --</option><option value='AB'>Alberta</option><option value='BC'>British Columbia</option><option value='MB'>Manitoba</option><option value='NB'>New Brunswick</option><option  value='NL'>Newfoundland and Labrador</option><option value='NT'>Northwest Territories</option><option value='NS'>Nova Scotia</option><option value='NU'>Nunavut</option><option value='ON'>Ontario</option><option value='PE'>Prince Edward Island</option><option value='QC'>Quebec</option><option value='SK'>Saskatchewan</option><option value='YT'>Yukon Territory</option><option value='Other'>Other</option></select>";
	}
	else
	{
		document.getElementById(stateOptions).innerHTML="<input type='text' class='txtInput' name='" + frmState + "' id='" + frmState + "' required='no' title='State/Province'>";
	}
	
}

//function deteremines if 'select' option is 'other' it changes to a text field
function sel2text(spanID, formID, val)
{
	if (document.getElementById(formID).value == val)
	{
		document.getElementById(spanID).innerHTML="<input type='text' class='txtInput' name='" + formID + "' id='" + formID + "' required='no' title='" + formID + "' >";
	}
}

//function set's height dynamically for iframe
function resize_iframe(my_iframe)
{
   document.getElementById(my_iframe).style.height = document.getElementById(my_iframe).contentWindow.document.body.scrollHeight + "px";
   document.getElementById(my_iframe).style.visibility = "visible";
}

//function set's height dynamically for cfwindows
function resize_window(my_window)
{
   document.getElementById(my_window).style.height = document.getElementById(my_window).contentWindow.document.body.scrollHeight + "px";
   document.getElementById(my_window).style.visibility = "visible";
}

//funcation called from 'profile.cfm' to hide/show password form
function showHide(theID)
{
	if (document.getElementById(theID).style.display=='none')
	{
		document.getElementById(theID).style.display='block'; 
	}
	else
	{
		document.getElementById(theID).style.display='none';
	}
}

/*functions clears field values & hides/shows said fields*
*@f1, @f2, @f3 are the fields*
*@showHide determines if hiding or not the fields*
*@theID is the id that wraps the 3 fields*/
function resetFields(f1, f2, f3, showHide, theID)
{
	document.getElementById(f1).value ="";
	document.getElementById(f2).value ="";
	document.getElementById(f3).value ="";
	if (showHide == "hide")
	{
		document.getElementById(theID).style.display='none';
	}
}

//function to allow users to bookmark webpage. works in IE or FF.
function bookmark()
{
	//if firefox browser
	if (window.sidebar)
	{
		window.sidebar.addPanel(document.title, document.URL, "");
	}
	//if MS IE browser
	else if (document.all)
	{
		window.external.AddFavorite(document.URL, document.title);
	}
	else
	{
		alert("Press CTRL-D (CMD-D on Macs) to bookmak this page");
	}
}

//function to open demos
function openDemo(theURL)
{
	var left = Math.floor( (screen.width - 760) / 2); //to center horizontally
	var top = Math.floor( (screen.height - 800) / 2); //to center vertically
	var winParams = "left=" + left + ",top=" + top + ",width=760,height=600,location=0,status=1,scrollbars=0,resizeable=1,toolbar=0,menubar=0";
	return window.open(theURL, "Demos",winParams)
}

//function to reply to post on Event Workspace
function replyToPost(theTextArea, replyToName)
{
	document.getElementById(theTextArea).focus();
	document.getElementById(theTextArea).value = '@' + replyToName + ' - ';
}

