/*
* Sets up the JavaScript Object array for the Terms and Conditions
*/
//var framework_branch = "/default/main/Investments/Internet/UK/WORKAREA/upload/";

function tac ( tacRegEx , tacPath ){
	this.tacRegEx =  tacRegEx  ;
	this.tacPath = tacPath ;
}

//var termsConditionsExclusion= new RegExp(/Terms_Conditions|terms_and_conditions|pages\/index/);
//site/microsites/t_and_c

var termsConditionsExclusion= new RegExp(/site\/index|t_and_c|Terms_Conditions/);

// the RegExp variable "tacTegEx" is a string-ish type but with certain differences,
// the variable is not quoted(' or ") and is surrounded by a forward 
// slash (e.g./matcher/). Forward slashes (/) and other special characters used
// by regular expressions should be escaped by adding a back slash (so / is \/)
// in the variable, as shown below


var stdTac = "/site/microsites/t_and_c/std.html" ;

aTacs = [ 	new tac( new RegExp(/site/) , stdTac )
	] ;
;


/* this script requires the array aTacs
*/ 

function redirectMe(){
	var bodyTag = document.getElementsByTagName("body")[0] ;
	//bodyTag.style.visibility = 'hidden' ;
	//alert(bodyTag.style.visibility) ;
	//return true;
	tacPage = ""; //variable for the associated TAC page to the current URL
	areaNumber = null ; //declare the area value for the iteration
	var mylocation = document.location.href ; //get the current location
		
	var isQuestionmark = mylocation.indexOf("?"); //is there a questionmark
	cookieName = "TACAgree" ; //Cookie name
	
	if ( isQuestionmark != -1){ // true if the charater ? exists in the URL.Used to remove any request params
		var arrayMyLocation = mylocation.split("?") ;
		mylocation = arrayMyLocation[0] ;
	}

	// This if statement excludes the terms and conditions page
	// alert(termsConditionsExclusion);
		
	// alert(mylocation);
	if (termsConditionsExclusion.test(mylocation)){
		//alert("break due to exclusions list");
		bodyTag.style.visibility = 'visible' ;
		return;
	}

	//loop through the aTacs array looking for matches for the current URL
	for ( var ii = 0 ; ii < aTacs.length ; ii++ ){
		//test if the RexExp variable matches the string mylocation
		if( aTacs[ii].tacRegEx.test( mylocation ) ) {
			tacPage = aTacs[ii].tacPath ;
			areaNumber = ii ;
			//alert("tacPage =" +tacPage) ;
			break ;
		}
	}
	
	//having got the tacPage if one exists, decide whether we need to go there by examining the cookie data
	var tacCookieVal = getCookieValue() ;
	
	//no cookie exists so we send them to the TAC page to have one generated	
	if ( tacCookieVal == "" && tacPage != ( null || "" )){
		
		var tacPageWithParams ;
		if ( mylocation.indexOf( "generated" )  != -1 ){ //do we need to slice
			// alert("make relative for TeamSite");
			tacPageWithParams = tacPage + "?srcpage=" + mylocation.slice( mylocation.indexOf( "generated" ) ) ;
		}else{
			tacPageWithParams = tacPage + "?srcpage=" + mylocation  ;
		}
		// alert("redirectMe()>1> no cookie and a tacPage has been assigned,\ngoing to tacPageWithParams: " + tacPageWithParams) ;
		//bodyTag.style.visibility = 'hidden' ;
		//alert("redirect 1") ;
		document.location.href =  tacPageWithParams  ;
		return
	}else{
		//create an array of the areas in the cookie
		var arrayCrumbs = tacCookieVal.split("::") ;
		//alert("redirectMe()>> areaNumber: " + areaNumber + "\n" + "arrayCrumbs.length: " + arrayCrumbs.length + "\n" + "tacPage:" + tacPage );
		
		//check to see whether the area has been accepted
		//sanity check
		if ( areaNumber != null && arrayCrumbs.length > 0 ){
			//if the cookie info is false go to tacPage remembering to add the srcpage parameter
			if ( arrayCrumbs[ areaNumber ] != "true" ){
				var tacPageWithParams = tacPage + "?srcpage=" + mylocation ;
				//alert( "redirectMe()>3> going to: " + tacPageWithParams ) ;
				//bodyTag.style.visibility = 'hidden' ;
				//alert("redirect 2") ;
				document.location.href = tacPageWithParams ;
				return ;
			}else{//if they have already seen the TAC
				bodyTag.style.visibility = 'visible' ;
				//alert("alright") ;
				return ;
				//alert("redirectMe()>4> already seen the tac as  arrayCrumbs[ areaNumber ] = " +  arrayCrumbs[ areaNumber ]) ;
			}
		}else{
			bodyTag.style.visibility = 'visible' ;
		}
	}
}//end of redirectMe

/********************************************
*   Process the accept or decline of the
*   terms and conditions form
*******************************************/
function processTerms(){
	var newLocation = '/site/index';
	var myAccept = document.getElementById("optionAccept");
	var myDecline = document.getElementById("optionDecline");
	if ( myAccept.checked == myDecline.checked){
		alert("Please 'DECLINE' or 'ACCEPT' the terms!");
	}
	else{
		if(myAccept.checked){
			acceptTACs();
		}else{
			window.location = newLocation;
		}
	}
}