//config
function phoneFlash(status) {
	
	if (status == 'on') {
		var currentTime = new Date()
		var day = currentTime.getDate()
		var hour = currentTime.getHours()
		if ((hour > 8) && (hour < 17)) {
			if (document.getElementById('phoneStatus')) {
			
				var phoneStatus = document.getElementById('phoneStatus');
					phoneStatus.style.display = 'block';	
					
				} 
		}
	}
}

/// specia offer 
function phoneNumBlock(status) {
	
	if (status == 'on') {
		var currentTime = new Date();
		var day = currentTime.getDate();
		var hour = currentTime.getHours();
		if ((hour > 8) && (hour < 17)) {

			if (document.getElementById('phoneNumBlock')) {
		
				var phoneStatus = document.getElementById('phoneNumBlock');
					phoneStatus.style.display = 'block';	
					
				var homeNav = document.getElementById('homeNav');
				homeNav.style.marginRight = "170px";
				} 
		}
	}
}

function newPhoneNumSwitch(status) {
	if (status == 'on') { // we have a num in url or cookie
			
			//numbers list
			var newPhoneNum = '0843 289 5555';
			
			// change numbers on page - look for span 'phoneNum')
			var spanArray = document.getElementsByTagName('span');
			
			for (i=0; i < spanArray.length; i++) { // loop through all spans to find phoneNums
				
				if (spanArray[i].className == 'phoneNum') {
				
					spanArray[i].innerHTML = newPhoneNum;
				
				}
			
			}
			
		}
}

////////////////////////////////////////// START COOKIES ////////////////////////////////////////////

function Set_Cookie( 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 * 60 * 24;
}
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 Get_Cookie( 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;
			cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}				

////////////////////////////////////////// END COOKIES ////////////////////////////////////////////

// Adwords Phone Display

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return false;
  else
    return results[1];
}

function phoneNumSwitch ( state ) {

	if (state == 'on') {
		
		// is num set in ad
		
		// yes - set cookie
		
		// no - is a cookie set
		
		// yes - read cookie and use
		
		// no - ignore
		
		
	
		var num = gup( 'ad' );
		
		if (!num) {
			
			if (Get_Cookie( 'acsPhone' ) != '') { // there is a cookie value
					num = Get_Cookie( 'acsPhone' );
			} else {
				return;	
			}
		} 
		
		if (num) { // we have a num in url or cookie
			// set cookie
			Set_Cookie( 'acsPhone', num, '14', '/', '.ashburtoncookeryschool.co.uk' );
			
			//numbers list
			switch (num) {
				case '0':
					var newPhoneNum = '0843 289 5555';
					break;
				case '1':
					var newPhoneNum = '0843 289 9790';
					break;
				default:
					var newPhoneNum = '(01364) 652784';
			}
			
			// change numbers on page - look for span 'phoneNum')
			var spanArray = document.getElementsByTagName('span');
			
			for (i=0; i < spanArray.length; i++) { // loop through all spans to find phoneNums
				
				if (spanArray[i].className == 'phoneNum') {
				
					spanArray[i].innerHTML = newPhoneNum;
				
				}
			
			}
			
		}
		
	}

}
// End Phone Num Switch



  function courseFinder(action) {
 
	 var cfContainer = document.getElementById('cfContainer');
	 var cookeryNav = document.getElementById('cookeryNav');
	 var cookeryNavSpan = document.getElementById('cookeryNavSpan');
	
	 if (action == "show") {
	 
	 cfContainer.style.display = 'block';
	 cookeryNav.className = 'cookeryNavOver';
	 cookeryNavSpan.style.color = '#ffffff';
	
	 } else if (action == 'hide') {
	 
	  cfContainer.style.display = 'none';
	  cookeryNav.className = '';
	  cookeryNavSpan.style.color = '';
	 }
 
 return;
 }

////////////////////////////////////////// FIRE FUNCTIONS///////////////////////////////////////////
function init() {
phoneNumSwitch('off'); // turn adwords phone switch on
newPhoneNumSwitch('on'); // turn on phone switch
phoneFlash('off');
phoneNumBlock('on');
}

window.onload = init;