/**
 * SSL Product Selection Wizard
 *
 * @author Josue Orozco
 * @copyright 2010 OV Web Solutions
 */

//------------------------------------------------------
// Define global scope variables / objects

	//------------------------------------------------------
	// Div Counter
	var div = 0;
	
var SSLWizard = function() {
	
	this._version = '1.0';
	this.whiteSpace = /^[\s]+$/;

	this.mQ1 = new Array();
	this.mQ2 = false;
	this.mQ3 = false;
	this.mQ4 = false;
	this.mQ5 = false;

	this.mBrandPreference = '';
	this.mSGC = false;
	this.mAuthentication = '';
	this.mEV = false;

	this.mProductRecommendation = '';
	this.mBtnBuySSL = '';
}

SSLWizard.prototype = {
	
	HideAllDivs: function(){

		//------------------------------------------------------
		// Define all of our divs here.
		var QDivs = [
			'Q1',
			'Q2',
			'Q3',
			'Q4',
			'Q5',
			'VeriSign_Secure_Site_Pro_with_EV',
			'VeriSign_Secure_Site_with_EV',
			'VeriSign_Secure_Site_Pro',
			'VeriSign_Secure_Site'
			];

		//------------------------------------------------------
		// Loop through our divs, and set their display to none.
		for(var i = 0, len = QDivs.length; i < len; i++){

			try {
				//document.getElementById(QDivs[i]).style.display = 'none';
				$(QDivs[i]).hide();
			} catch(e){

				XBrowser.Log('HideAllDivs() Failed =>' + e);
			}
		}
	},
	
	ShowDiv: function(div_id){
		
		//------------------------------------------------------
		// Hide all divs
		this.HideAllDivs();
		
		try {
			//document.getElementById(div_id).style.display = 'block';
			$(div_id).show();
		} catch(e){
	
			XBrowser.Log('ShowDiv() => ' + div_id);
		}
	},

	Start: function(){

		//------------------------------------------------------
		// Grab Buy now button element (which is a <p> tag)
		this.mBtnBuySSL = document.getElementById('btnBuySSL');

		this.Continue();
	},

	Continue: function(){

		//------------------------------------------------------
		// Increase div counter
		div++;
		XBrowser.Log('div => ' + div);

		//------------------------------------------------------
		// Show correct div
		switch(div){

			case 1:
				
				//------------------------------------------------------
				// Show div
				this.ShowDiv('Q1');
				break;
			case 2:
				
				this.ValidateQ1();
				break;
			case 3:
				this.ValidateQ2();
				break;
			case 4:
				this.ValidateQ3();
				break;
			case 5:
				this.ValidateQ4();
				break;
			case 6:
				this.ValidateQ5();
				break;
		}
	},

	ValidateQ1: function(){

		var chkPayment = document.getElementById('chkPayment');
		var chkFinancial = document.getElementById('chkFinancial');
		var chkPersonal = document.getElementById('chkPersonal');
		var chkLogin = document.getElementById('chkLogin');
		var chkInternal = document.getElementById('chkInternal');

		//------------------------------------------------------
		// Set Flag
		var flag = false;

		if(chkPayment.checked){
			flag = true;
			this.mQ1.push('A');
		}

		if(chkFinancial.checked){
			
			flag = true;
			this.mQ1.push('B');
		}

		if(chkPersonal.checked){

			flag = true;
			this.mQ1.push('C');
		}

		if(chkLogin.checked){
			
			flag = true;
			this.mQ1.push('D');
		}

		if(chkInternal.checked){

			flag = true;
			this.mQ1.push('E');
		}
	
		XBrowser.Log('flag => ' + true);

		//------------------------------------------------------
		// Check Flag
		if(!flag){

			//------------------------------------------------------
			// Decrease div counter
			div--;

			//------------------------------------------------------
			// Show Error msg.
			MessageBoard.ShowConfirmation('red', 'Please select all that apply...');
		} else {

			//------------------------------------------------------
			// Hide any error messages that we may have encountered.
			MessageBoard.HideConfirmation();

			//------------------------------------------------------
			// GA Tracking
			GaManager.TrackAsyncPageView('/ssl/ssl-product-wizard/rStep1.php');

			//------------------------------------------------------
			// Set brand preference flag
			var flgBrand = false;
			for(x in this.mQ1){

				if(this.mQ1[x] == 'A' || this.mQ1[x] == 'B' || this.mQ1[x] == 'C' || this.mQ1['D']){

					flgBrand = true;
					this.mBrandPreference = 'Verisign';
					this.mEV = true;
				}

				if(this.mQ1[x] == 'C' && !flgBrand || this.mQ1[x] == 'D' && !flgBrand || this.mQ1[x] == 'E'){

					//------------------------------------------------------
					// EV Flag
					if(this.mQ1[x] == 'C'){

						this.mEV = true;
					}

					this.mBrandPreference = 'GeoTrust';
				}
			}

			//------------------------------------------------------
			// Show next div
			this.ShowDiv('Q2');
		}
	},

	ValidateQ2: function (){
		
		//------------------------------------------------------				    
		// First, we cycle through the total number of radio buttons
		rdoQ2 = document.getElementsByName('rdoQ2');
	
		for(var i = 0, len = rdoQ2.length; i < len; i++){
		
			//----------------------------------------
			// If (i) is checked, then let\'s grab it\'s value.
			if(rdoQ2[i].checked == true){

				//------------------------------------------------------
				// Set Q2
				this.mQ2 = rdoQ2[i].value;

				//------------------------------------------------------
				// Set Brand Preference
				if(this.mQ2 == 'true' && this.mBrandPreference == 'GeoTrust'){

					this.mBrandPreference = 'Verisign';
				}

				//------------------------------------------------------
				// GA Funnel Tracking
				GaManager.TrackAsyncPageView('/ssl/ssl-product-wizard/rStep2.php');
			
				//------------------------------------------------------
				// Next Div
				this.ShowDiv('Q3');
			} 
		}
	},

	ValidateQ3: function(){
		
		//------------------------------------------------------				    
		// First, we cycle through the total number of radio buttons
		rdoQ3 = document.getElementsByName('rdoQ3');
	
		for(var i = 0, len = rdoQ3.length; i < len; i++){
		
			//----------------------------------------
			// If (i) is checked, then let\'s grab it\'s value.
			if(rdoQ3[i].checked == true){

				//------------------------------------------------------
				// Set Q3
				this.mQ3 = rdoQ3[i].value;

				//------------------------------------------------------
				// Set Encryption Strength
				if(this.mQ3 == 'true'){

					this.mSGC = true;
				}

				//------------------------------------------------------
				// GA Funnel Tracking
				GaManager.TrackAsyncPageView('/ssl/ssl-product-wizard/rStep3.php');

				//------------------------------------------------------
				// Next Div
				this.ShowDiv('Q4');
			} 
		}
	},

	ValidateQ4: function(){
		
		//------------------------------------------------------				    
		// First, we cycle through the total number of radio buttons
		rdoQ4 = document.getElementsByName('rdoQ4');
	
		for(var i = 0, len = rdoQ4.length; i < len; i++){
		
			//----------------------------------------
			// If (i) is checked, then let\'s grab it\'s value.
			if(rdoQ4[i].checked == true){

				//------------------------------------------------------
				// Set Q4
				this.mQ4 = rdoQ4[i].value;

				//------------------------------------------------------
				// Set Authentication
				if(this.mQ4 == 'true'){
					this.mAuthentication = 'organization';
				} else {

					this.mBrandPreference = 'GeoTrust';
					this.mAuthentication = 'domain';
				}

				//------------------------------------------------------
				// GA Funnel Tracking
				GaManager.TrackAsyncPageView('/ssl/ssl-product-wizard/rStep4.php');

				//------------------------------------------------------
				// Next Div
				this.ShowDiv('Q5');
			} 
		}
	},

	ValidateQ5: function(){
	
		//------------------------------------------------------				    
		// First, we cycle through the total number of radio buttons
		rdoQ5 = document.getElementsByName('rdoQ5');
	
		for(var i = 0, len = rdoQ5.length; i < len; i++){
		
			//----------------------------------------
			// If (i) is checked, then let\'s grab it\'s value.
			if(rdoQ5[i].checked == true){

				//------------------------------------------------------
				// Set Q5
				this.mQ5 = rdoQ5[i].value;

				//------------------------------------------------------
				// Set EV
				if(this.mQ5 == 'true' && this.mEV == false){

					this.mEV = true;
				}

				//------------------------------------------------------
				// GA Funnel Tracking
				GaManager.TrackAsyncPageView('/ssl/ssl-product-wizard/rStep5.php');

				//------------------------------------------------------
				// Show Product Recommendation
				this.GetProductRecommendation();
			} 
		}
	},

	GetProductRecommendation: function(){

		//------------------------------------------------------
		// Get Brand Preference
		if(this.mBrandPreference == 'Verisign'){

			if(this.mEV == true && this.mSGC == true){

				//------------------------------------------------------
				// Set Product Recommendation.
				this.mProductRecommendation = 'VeriSign_Secure_Site_Pro_with_EV';

				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('4');
			} else if (this.mEV == true && this.mSGC == false){

				//------------------------------------------------------
				// Set product recommendation
				this.mProductRecommendation = 'VeriSign_Secure_Site_with_EV';
				
				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('5');
			} else if (this.mEV == false && this.mSGC == true){

				//------------------------------------------------------
				// Set product recommendation
				this.mProductRecommendation = 'VeriSign_Secure_Site_Pro';

				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('6');
			} else if (this.mEV == false && this.mSGC == false){

				//------------------------------------------------------
				// Set product recommendation
				this.mProductRecommendation = 'VeriSign_Secure_Site';

				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('7');
			}
		} else if (this.mBrandPreference == 'GeoTrust'){

			if(this.mEV == true){

				//------------------------------------------------------
				// Set product recommendation
				this.mProductRecommendation = 'GeoTrust_True_BusinessId_with_EV';

				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('1');
			} else if (this.mAuthentication == 'organization' && this.mEV == false){

				//------------------------------------------------------
				// Set product recommendation
				this.mProductRecommendation = 'GeoTrust_True_BusinessId';

				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('2');
			} else if (this.mAuthentication == 'domain'){

				//------------------------------------------------------
				// Set product recommendation
				this.mProductRecommendation = 'GeoTrust_QuickSSL_Premium';

				//------------------------------------------------------
				// Set buy now href
				var href = '/cart/config/?s=' + Base64.UrlEncode('ssl') + '&cid=' + Base64.UrlEncode('3');
			}
		}

		//------------------------------------------------------
		// Set buy now button
		if(XBrowser.GetAppName() == 'Microsoft Internet Explorer'){

			this.mBtnBuySSL.innerHTML = '<a href="' + href + '"><img src="/images/buy-ssl-certificate-now.gif" width="260" height="73" alt="Buy this SSL Certificate Now" /></a>';
		} else {
			this.mBtnBuySSL.innerHTML = '<a href="' + href + '" class="large red awesome"><br />Buy this SSL Certificate Now &raquo;&raquo;<br /><br /></a><br /><br />';
		}

		//------------------------------------------------------
		// Hide questions and adjust left column width
		document.getElementById('wizard-questions').style.display = 'none';
		document.getElementById('wizard-questions').style.width = '57.48em';

		//------------------------------------------------------
		// Show right side options
		document.getElementById('wizard-options').style.display = 'block';

		this.ShowDiv(this.mProductRecommendation);
	}
}

//------------------------------------------------------
// Instantiate an SSL Trial
var Wizard = new SSLWizard();
