//panel code
// customer survey is ended. Archive copy stored in asset manager
// customer survey is ended. Archive copy stored in asset manager
var long_period = 5256000;//(5256000 * 60 * 1000)= 10years
var short_period = 1000000;//(1000000 * 60 * 1000)~1.91 years
var time_out_25sec = 25000; //25 second
var time_out_30min = 1800000; //30 minutes
var slide_time = 2000;

function build_Panel()
{
var panel_boby ="<!--Start to slide a scree when the timer get pased the time supplied-->";
panel_boby +='<div id="triqui_container">';
panel_boby +='<form method="post" name="form">';
panel_boby +='<div id="triqui_ad">';
panel_boby +='<div id="footerads" >';
panel_boby +='<a id="btn_close" title="Close">Close</a>';
panel_boby +='<h1>Can we help you?</h1>';
panel_boby +='<div id="data_panel">';
panel_boby +='<div class="question">';
panel_boby +='<p class="text"> Please contact me about:</p>';
panel_boby +='<div class="options">';
panel_boby +='<input name="choices" id="about0" value="Rail Service - carload" type="checkbox"><label for="about0"> Rail service - carload</label><br />';
panel_boby +='<input name="choices" id="about1" value="Intermodal - door-to-door" type="checkbox"><label for="about1"> Intermodal - door-to-door</label><br />';
panel_boby +='<input name="choices" id="about2" value="Warehousing/transloading/logistics parks" type="checkbox"><label for="about2"> Warehousing/transloading/logistics parks</label><br />';
panel_boby +='<input name="choices" id="about3" value="Freight forwarding/customs brokerage" type="checkbox"><label > Freight forwarding/customs brokerage</label><br />';
panel_boby +='<input name="choices" id="OtherOption" value="Other: " type="checkbox"><label for="Other"> Other: </label> <input name="OtherOptionText" id="otherOptionText" type="text"><br />';
panel_boby +='</div>';
panel_boby +='</div>';
panel_boby +='<div class="contactInfo">';
panel_boby +='<table>';
panel_boby +='<tr><td>Name:* </td><td><input name="user-name" id="user-name" type="text"></td></tr>';
panel_boby +='<tr><td>Company:* </td><td><input name="user-company" id="user-company" type="text"></td></tr>';
panel_boby +='<tr><td>Email:* </td><td><input name="user-email" id="user-email" type="text"></td></tr>';
panel_boby +='<tr><td>Phone: </td><td><input name="user-phone" id="user-phone" type="text"></td></tr>';
panel_boby +='</table>';
panel_boby +='</div>';
panel_boby +='</div>';
panel_boby +='<div style="clear:both"></div>';
panel_boby +='<div id="container_btn">';
panel_boby +='<input value="Don\'t ask me again" id="btn_dont_ask_me" type="button">';
panel_boby +='<input value="Ask me later" id="btn_Ask_me" type="button">';
panel_boby +='<p class="errorMessagePanel"></p>';
panel_boby +='<input value="Submit" id="btn_contact_me" type="button">';
panel_boby +='</div>';
panel_boby +='</div>';
panel_boby +='</div>';
panel_boby +='</form>';
panel_boby +='</div>';
$("body").append(panel_boby);
} 
/*-------example SetCookiePanel( 'mycookie', 'visited 9 times', 30, '/', '', '' );--- 
Don't forget to put in empty quotes for the unused parameters or you'll get an error when you run the code. 
This makes the cookie named 'mycookie', with the value of 'visited 9 times', and with a life of 30 days, 
and the cookie is set to your root folder. 
*/
function SetCookiePanel( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function GetCookiePanel( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
// this deletes the cookie when called, example Delete_Cookie('cookie name', '/', '')
function Delete_Cookie( name, path, domain ) {
if ( GetCookiePanel( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function validateEmail(emailAddress) 
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (!reg.test(emailAddress)) {
		return false;
	}	
	return true;
}
/*********************************************************************************
*get time to start the panel
/*********************************************************************************/
function getTimerStartPanel(firstVistTimer)
{
	var todayCOOKIE = new Date();
	//todayCOOKIE.setTime( today.getTime() );
	var expires_date = firstVistTimer - todayCOOKIE.getTime();
	if(expires_date < 0 )
		return 0;
	else
		return expires_date;
}
//to slide the panel from the bottom of the page, 
//when timeout is finished
function slidePanel(timer1)
{
	var timer = getTimerStartPanel(timer1);

	setTimeout(function(){
	// Get a reference to the container.	
	var container = $('#triqui_container');
	SetCookiePanel( 'panelsession', 'Shipping area get visited',0, '/', '', '' );
	Delete_Cookie('panel_timeout', '/', '');
	Delete_Cookie('panel_timeoutPlus', '/', '');
	//to give a margin at the bottom  when the panel gets apeared
	$("div#footer_container ").css("margin-bottom","193px");
	
	// Show panel.
	container.slideDown(slide_time);
	}, timer);			
}

$(document).ready(function () {
		
	//get cookies
	var panel_Off = GetCookiePanel("panel_Off_LT");
	var panel_ON = GetCookiePanel("panelsession");
	var panel_NV = GetCookiePanel("panelNextVisit");
	var panel_timeout_ON = GetCookiePanel("panel_timeout");
	var panel_timeoutPlus_ON = GetCookiePanel("panel_timeoutPlus");
	if ( panel_Off == null && $("body").hasClass("popup")== false )
	{//if the panel_off cookie is not null, we build the panel
		build_Panel();
		if (panel_timeout_ON != null)
		{
			slidePanel(panel_timeout_ON);
			SetCookiePanel( 'panel_timeoutPlus', 'User will see the panel slided up after 25 sec',0, '/', '', '' );
		}
		else if (panel_timeoutPlus_ON != null && panel_ON == null)
		{
			slidePanel(0);
		}
		else if (panel_ON != null)//cookie session
		{
			$('#triqui_container').css('display','block');
			$("div#footer_container ").css("margin-bottom","193px");
		}
		else if($("a.selected").parents("#nav_button_0").is("#nav_button_0")  || panel_NV != null)//first visit or the panel was on from the last visit
		{
			var todayCOOKIE = new Date();
			var timeout1 =  time_out_25sec + todayCOOKIE.getTime();
			SetCookiePanel( 'panel_timeout',timeout1 ,0.42, '/', '', '' );//0.42 ~ 25 second
			SetCookiePanel( 'panelNextVisit', 'User will see the panel slided up for next visit',short_period, '/', '', '' );
			slidePanel(timeout1);
		}
		else
		{
			$('#triqui_container').css('display','none');
			$("div#footer_container ").css("margin-bottom","0");
		}
		/* --------------- ask me again click event----------*/
		$('#btn_Ask_me').click(function(){
			// Hide - slide up.
			$('#triqui_container').slideUp(slide_time);
			$("div#footer_container ").css("margin-bottom","0");
			
			var todayCOOKIE = new Date();
			var timeout2 =  time_out_30min + todayCOOKIE.getTime();
			SetCookiePanel( 'panel_timeout', timeout2 ,30, '/', '', '' );//30 minutes
			slidePanel(timeout2);	
		});
		/* --------------- ask me again click event----------*/
		$('#btn_close').click(function(){	
			// Hide - slide up.
			$('#triqui_container').slideUp(slide_time);
			$("div#footer_container ").css("margin-bottom","0");			
			SetCookiePanel( 'panel_Off_LT', 'dont ask me again', 0, '/', '', '' );			
		});
		/* --------------- dont ask me again click ----------*/
		$('#btn_dont_ask_me').click(function(){
			// Hide - slide up.
			$('#triqui_container').slideUp(slide_time);
			$("div#footer_container ").css("margin-bottom","0");
			SetCookiePanel( 'panel_Off_LT', 'dont ask me again', long_period, '/', '', '' );
			Delete_Cookie('panelsession', '/', '');	
			Delete_Cookie( 'panelNextVisit', '/', '');		
		});
		
		/* --------------- Submit button click event----------*/
		$('#btn_contact_me').click(function( event ){
			/*----------------------- to post data to the server --------------------*/
			var name = $("#user-name").val();
			var company = $("#user-company").val();
			var email = $("#user-email").val();
			var phone = $("#user-phone").val();
			var data = new Array();
			
			if($('#OtherOption').attr("checked"))
				$('#OtherOption').attr("value", $('#OtherOption').attr("value") + $('#otherOptionText').val());
			 
			//to build the string that contains the options selected by the user		
			$(".options input[name='choices']").each(function(i){
				if($(this).attr("checked"))
					data.push($(this).val());
			});
			
			//to re-initiate the value of the input 'OtherOption'	
			$('#OtherOption').attr("value", "Other: ")
			
			/*I will add the other data --*/
			var params = '&user-name='+ name + '&user-company=' + company + '&user-email=' + email + '&user-phone=' + phone + '&choices=' + data;
			
			if(name=='')
			{
				$('.errorMessagePanel').css('display','block');
				$('.errorMessagePanel').html("* Name field cannot be empty");
			}
			else if(company=='')
			{
				$('.errorMessagePanel').css('display','block');	
				$('.errorMessagePanel').html("* Company field cannot be empty");			
			}
			else if(email=='')
			{
				$('.errorMessagePanel').css('display','block');
				$('.errorMessagePanel').html("* Email field cannot be empty");				
			}
			else if(!validateEmail(email))
			{
				$('.errorMessagePanel').css('display','block');
				$('.errorMessagePanel').html("* A valid email must be entered");				
			}
			else
			{
				$.ajax({
				type: "GET",
				url: "/rw/cps/rde/xfw3/modules/calltoaction/email-send-for-details.xml?locale=en",
				data: params,
				success: function()
				{  
					 // Hide - slide up.
					$('#triqui_container').slideUp(slide_time);
					$("div#footer_container ").css("margin-bottom","0");
					SetCookiePanel( 'panel_Off_LT', 'dont ask me again', long_period, '/', '', '' );
					Delete_Cookie('panelsession', '/', '');	
					Delete_Cookie( 'panelNextVisit', '/', '');						
				},
				error: function ()
				{
					window.alert('Error to transmit to the server');
				}
				});
			}
		});
	}
	$("#otherOptionText").click( function(){		
	$("#OtherOption").attr("checked","true");
	});
});
