var isTransitioning = false;
var moduleIsTransitioning = false;
var isExtraBoxOn = false;


function getTestCall() {
	var testFirstName = $F('nonifx_firstName');
	var testLastName = $F('nonifx_lastName');
	var testemail = $F('nonifx_email');
	var testorganization = $F('nonifx_organization');
	var testphone = $F('nonifx_phone');
	var testzipcode = $F('nonifx_zipcode');
	
	var theMessage = 'Test Success Standard! - ' + testFirstName + ' ' + testLastName + ' ' + testemail + ' ' + testorganization + ' ' + testphone + ' ' + testzipcode;
	
	alert(theMessage);
}

function getTestCallIFX() {
	var testFirstName = $F('ifx_firstName');
	var testLastName = $F('ifx_lastName');
	var testemail = $F('ifx_email');
	var testorganization = $F('ifx_organization');
	var testphone = $F('ifx_phone');
	var testzipcode = $F('ifx_zipcode');
	
	var theMessage = 'Test Success IFX! - ' + testFirstName + ' ' + testLastName + ' ' + testemail + ' ' + testorganization + ' ' + testphone + ' ' + testzipcode;
	
	alert(theMessage);
}


function showVideo(theSrc, theWidth, theHeight, theTitle) {
	
  var content = new Element("div").setStyle({width: theWidth + "px", height: (theHeight + 39) + "px"}).insert(
    new Element("div", {id: "videoPlayerFlashDiv"})
  );
  
  var flashvars = {
    flashVideoId: "videoPlayerFlash",
    controlsURL: "/res/controlsExternalBlack.swf",
    videoURL: theSrc,
    autoPlay: true
  };
  var params = {};
  var attributes = {
    id: "videoPlayerFlash",
    name: "videoPlayerFlash"
  };
  
  var dialog = new LDialog({
    content: content,
    title: theTitle,
    afterOpen: function () {
      swfobject.embedSWF("/res/videoPlayer.swf", "videoPlayerFlashDiv", theWidth, theHeight, "9.0.0", false, flashvars, params, attributes);
    }
  });
      
}


function validateEmail(email) { 
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return email.match(re);
}


function GetThankYouMessage(formType) {
	var wrapperID = $(formType).up('#formWrapper');
	var thanksID = wrapperID.down('div',0);
	var formID = wrapperID.down('div',1);

	formID.fade({
		duration: .3,
		afterFinish: function() {
			thanksID.appear({
				duration: .3,
				afterFinish: function() {
					isTransitioning = false;
				}
			});
		}
	});
}


function submitContactForm(name, prefix) {

	var errorCheck = 0;

	if ($F(prefix + '_firstName') == '') {
		$(prefix + '_firstName_label').className = "formLabelAlert";
		errorCheck = 1;
	} else { $(prefix + '_firstName_label').className = "formLabel"; }
	if ($F(prefix + '_lastName') == '') {
		$(prefix + '_lastName_label').className = "formLabelAlert";
		errorCheck = 1;
	} else { $(prefix + '_lastName_label').className = "formLabel"; }
	if ($F(prefix + '_email') == '' || !(validateEmail($F(prefix + '_email')))) {
		$(prefix + '_email_label').className = "formLabelAlert";
		errorCheck = 1;
	} else { $(prefix + '_email_label').className = "formLabel"; }
	if ($F(prefix + '_organization') == '') {
		$(prefix + '_organization_label').className = "formLabelAlert";
		errorCheck = 1;
	} else { $(prefix + '_organization_label').className = "formLabel"; }
	if ($F(prefix + '_phone') == '') {
		$(prefix + '_phone_label').className = "formLabelAlert";
		errorCheck = 1;
	} else { $(prefix + '_phone_label').className = "formLabel"; }
	if ($F(prefix + '_zipcode') == '') {
		$(prefix + '_zipcode_label').className = "formLabelAlert";
		errorCheck = 1;
	} else { $(prefix + '_zipcode_label').className = "formLabel"; }
	
	if (errorCheck == '1') {
		//FAILURE
	} else {
		//SUCCESS
		var updateID = $(name).down('#processContactArea');
		
		new Ajax.Updater(updateID, 
			'/h/ifx/AjaxContactForm', 
			{ 
				method: 'get',
				evalScripts: true,
				parameters: {firstName: $F(prefix + '_firstName'),
					     lastName: $F(prefix + '_lastName'),
					     email: $F(prefix + '_email'),
					     organization: $F(prefix + '_organization'),
					     phone: $F(prefix + '_phone'),
					     zipcode: $F(prefix + '_zipcode'),
					     formtype: name
				}
			});	
	}

}

function clearFormVars(name) {
	$$('.contactFormInput').each(function(c) {
		c.value = '';
	});

	$$('.formLabelAlert').each(function(a) {
		a.className = 'formLabel';
	});
}

function clearMessage(name) {
	var wrapperID = $(name).up('#formWrapper');
	var thanksID = wrapperID.down('div',0);
	thanksID.setStyle({
		display: 'none'
	});
	var formID = wrapperID.down('div',1);
	formID.setStyle({
		display: 'block'
	});
}



/* Functions for iFX Process Modal Box Animations
 * Parameters: name (name of div with modal box content)
 *             choice (the selection box item)
 *             question (the radio button item)
 */
function openExtraBox(name, choice, question) {
	if (!isTransitioning) {
		isTransitioning = true;
		isExtraBoxOn = true;
		
		if (name == 'ifx') {
			new Ajax.Updater('processStepFourArea', 
				'/h/ifx/AjaxStepFour', 
				{ 
					method: 'get', 
					parameters: {choice: choice, question: question}
				});
		}
		
		if (name == 'tradein') {
			$('tradeButton').hide();
		}
	
		hideExtraBoxContent(name);
	}	
}

function hideExtraBoxContent(name) {
	$(name + 'BoxContent').fade({
		duration: .3,
		afterFinish: animateExtraBoxOpen.curry(name)
	});
}

function animateExtraBoxOpen(name) {
	new Effect.Morph($(name + 'BoxWrapper'), {
		style: { width: '753px', height: '385px' },
		duration: .5,
		afterFinish: showExtraBoxContent.curry(name)
	});
}

function showExtraBoxContent(name) {
	$(name + 'BoxContent').down('#rightSideBox').className = 'displayBlock';

	if (name == 'ifx') {
		$(name + 'BoxContent').down('#stage1').className = 'displayNone';
		$(name + 'BoxContent').down('#stage2').className = 'displayBlock';
	}
	$(name + 'BoxContent').appear({
		duration: .3,
		afterFinish: function() {
			isTransitioning = false;
		}
	});
}


/* Functions for Navigation Modal Box Animations
 * Parameters: name (name of div with modal box content)
 */
function openBox(name) {
	if (!isTransitioning) {
		isTransitioning = true;
	
		if ($(name + 'Box').className == 'displayNone') {
		
			if (name == 'ifx') {
				$(name + 'BoxContent').down('#stage1').className = 'displayBlock';
				$(name + 'BoxContent').down('#stage2').className = 'displayNone';
				$(name + 'BoxContent').down('#rightSideBox').className = 'displayNone';
				resetProcess();
			}
			if (name == 'tradein') {
				$(name + 'BoxContent').down('#rightSideBox').className = 'displayNone';
				$('tradeButton').show();
			}
		
			$(name + 'Box').className = 'displayBlock';
			$(name + 'BoxWrapper').className = 'displayBlock';
			animateBoxOpen(name);
		} else {
			var formName = name + 'Form';
			clearFormVars(formName);
			hideBoxContent(name);
		}
	}	
}

function animateBoxOpen(name) {
	new Effect.Morph($(name + 'BoxWrapper'), {
		style: { width: '377px', height: '385px' },
		duration: .5,
		afterFinish: showBoxContent.curry(name)
	});
}

function showBoxContent(name) {
	$(name + 'BoxContent').appear({
		duration: .3,
		afterFinish: function() {
			isTransitioning = false;
		}
	});
}

function hideBoxContent(name) {
	$(name + 'BoxContent').fade({
		duration: .3,
		afterFinish: animateBoxClose.curry(name)
	});
}

function animateBoxClose(name) {
	new Effect.Morph($(name + 'BoxWrapper'), {
		style: { height: '0px', width: '0px' },
		duration: .5,
		afterFinish: displayOff.curry(name)
	});
}

function displayOff(name) {
    	$(name + 'Box').className = 'displayNone';
    	$(name + 'BoxWrapper').className = 'displayNone';
	var formName = name + 'Form';
	clearMessage(formName);
    	isTransitioning = false;
}


/* Functions for iFX Form Process
 */
function GetStepTwo(){
	resetStepThree();
	
	new Ajax.Updater('processStepTwoArea', 
		'/h/ifx/AjaxStepTwo', 
		{ 
			method: 'get', 
			evalScripts: true,
			parameters: {choice:$F('processSelector')}
		});
}

function GetStepThree() {
	var theQuestion = '';
	$$('.radioQuestion').each( function(i) {
	    if ($F(i)) { theQuestion = $F(i); }
	} );
	
	new Ajax.Updater('processStepThreeArea', 
		'/h/ifx/AjaxStepThree', 
		{ 
			method: 'get', 
			parameters: {choice: $F('processSelector'),question: theQuestion}
		});
}

function resetProcess() {
	$('#theProcess').reset();
	
	new Ajax.Updater('processStepTwoArea', 
	'/h/ifx/AjaxStepTwo', 
	{ 
		method: 'get', 
		parameters: {choice: ''}
	});
		
	new Ajax.Updater('processStepThreeArea', 
		'/h/ifx/AjaxStepThree', 
		{ 
			method: 'get', 
			parameters: {choice: '',question: ''}
		});
		
	new Ajax.Updater('processStepFourArea', 
		'/h/ifx/AjaxStepFour', 
		{ 
			method: 'get', 
			parameters: {choice: '',question: ''}
		});
}

function resetStepThree() {
	new Ajax.Updater('processStepThreeArea', 
		'/h/ifx/AjaxStepThree', 
		{ 
			method: 'get', 
			parameters: {choice: '',question: ''}
		});
}


