// JavaScript Document

document.write ('<script type="text/javascript" src="/hicl/control/js/taclist.js"></script>');

var defaultLocation = 'http://www.hicl.hsbc.com';

var cookieHGAM = function () {
	var urlControl;
	var urlControl2;

	urlControl = urlLive;
	urlControl2 = urlLive2;

	var controller = '/hicl/controller.html';

	var regexPattern = new Array();
	regexPattern[0] = '((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';	// HTTP URL 1
	regexPattern[1] = '.*?(\\?)';	// Non-greedy match on filler
	regexPattern[2] = '(returnurl)';	// Word 1
	regexPattern[3] = '(=)';	// Single Character 2
	regexPattern[4] = '.*?';	// Non-greedy match on filler
	regexPattern[5] = '((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';	// HTTP URL 2
	regexPattern[6] = '.*?(\\?)';	// Non-greedy match on filler
	regexPattern[7] = '(srcpage)';	// Word 2
	regexPattern[8] = '(=)';	// Single Character 4
	regexPattern[9] = '.*?';	// Non-greedy match on filler
	regexPattern[10] = '((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))';
	regexPattern[11] = '.*?(\\?)';	// Non-greedy match on filler
	regexPattern[12] = '(accepted)';	// Word 2
	regexPattern[13] = '(=)';	// Single Character 4
	regexPattern[14] = '.*?';	// Non-greedy match on filler
	regexPattern[15] = '(true)';
	regexPattern[16] = '.*?(\\?)';
	regexPattern[17] = '(tnc)';	// Word 2
	regexPattern[18] = '(=)';	// Single Character 4
	regexPattern[19] = '.*?';	// Non-greedy match on filler
	regexPattern[20] = '((?:[a-z][a-z]+))';
	regexPattern[21] = '.*?(\\?)';	// Non-greedy match on filler
	regexPattern[22] = '(lang)';	// Word 2
	regexPattern[23] = '(=)';	// Single Character 4
	regexPattern[24] = '.*?';	// Non-greedy match on filler
	regexPattern[25] = '((?:[a-z]+_[a-z]+))';
	regexPattern[26] = '.*?(\\?)';	// Non-greedy match on filler
	
	function setCookie(n) {
		var name = n;
		//domain = window.location.hostname;
		//document.cookie = n + "=true; path=/;" + ( ( domain ) ? "domain=" + domain : "" );
		document.cookie = n + "=true; path=/;";
		if(getCookie(n)){
			return true;
		}else{
			return false;
		}
	}

	function matchList(l) {
		if(urlControl[l] && urlControl != undefined){
			return urlControl[l];
		}else{
			return false;
		}
	}
	
	function matchList2(l) {

		// Strip anchors from URL before checking
		var getloc = l + "";
		var arrgetloc = getloc.split("#");
		if (arrgetloc.length > 1) {
			getloc = arrgetloc[0];
		}
		var arrgetloc2 = getloc.split("?");
		if (arrgetloc2.length > 1) {
			getloc = arrgetloc2[0];
		}
		//get rid of the #

		if(urlControl2[getloc] && urlControl2 != undefined){
			return urlControl2[getloc];
		}else{
			return false;
		}
	}

	function getLocation() { return unescape(document.location); }	

	function tncCookieName(tnc) { 
		if(tnc){
			return matchList2(tnc);
		}else{
			return false;
		} return tnc;
	}
	// search the cookiename available on the ukCommonList then loops on the getCookie function.
	function searchCommonCookie(l) {
		var commonCookie = false;
		for(var i in l) {
			if(getCookie(l[i])){
				commonCookie = true;
			}
		}
		return commonCookie;
	}
	
	function tncDestination(s) { 
		if(s){
			return matchList(s);
		}else{
			return false;
		} 
	}

	function getCookie(n) {
		if(n != 'false' || n != 'undefined'){
			var search = n + "=" ;
			var returnString = "";
			if (document.cookie.length > 0) {
				var offset = document.cookie.indexOf(search)
				// check to see if cookie exists
				if (offset != -1) { 
					offset += search.length
					//set index of beginning of value
					end = document.cookie.indexOf(";", offset);
					// set index of end of cookie value avoiding sessionId
					if (end == -1){
						end = document.cookie.length;
					} 
					returnString=unescape(document.cookie.substring(offset, end)) ;
				}
				//alert( "getCookieValue>> returnString is " + returnString );
				if(returnString != ''){
					return returnString ;
				}else{
					return false
				}
			}else{ 
				//alert( "getCookieValue>> cookie length == 0, so returning an empty string");
				return false ;
			}
		}else{
			return false;
		}
	}

	function getUrlParam(l) {
		if(typeof l != 'object'){
			l = getLocation();
		}
		var pat = '';
		//alert(true);
		l = encodeURI(l.toString());
		var rxP = regexPattern;
		pat += rxP[0]; 
		pat += rxP[1];
		if(l.indexOf('lang') != -1){
			pat += rxP[22];
			pat += rxP[23];
			pat += rxP[24];
			pat += rxP[25];
			pat += rxP[26];
		}
		if(l.indexOf('?tnc') != -1){
			//pat += rxP[16];
			pat += rxP[17];
			pat += rxP[18];
			pat += rxP[19];
			pat += rxP[20];
			pat += rxP[21];
		}
		if(l.indexOf('returnurl') != -1){
			pat += rxP[2];
			pat += rxP[3];
			pat += rxP[4];
			pat += rxP[5];
			pat += rxP[6];
		}
		if(l.indexOf('srcpage') != -1){
			pat += rxP[7];
			pat += rxP[8];
			pat += rxP[9];
			pat += rxP[10];
		}
		if(l.indexOf('accepted') != -1){
			pat += rxP[12];
			pat += rxP[13];
			pat += rxP[14];
			pat += rxP[15];
		}

		var p = new RegExp(pat,["i"]);
		var m = p.exec(l);
		var urlPattern = new RegExp(regexPattern[0]);	
		if(m){
			return m;
		}else{
			return false;
		}
	}


	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
	

	return {

		init : function (a) {
			var rUrl = this.returnUrl();
			var sPage = this.srcPage();
			var tLang = this.lang();
			
			if(rUrl != false){
				if(this.clientType != null){
					this.controlTerms(rUrl, sPage, true, this.clientType);
				}else{
					if(a){
						this.controlTerms(rUrl, sPage);
					}else{
						this.controlTerms(rUrl, sPage,'','',tLang);
					}
				}
			}else{
				if(a){
					this.controlTerms('', sPage);
				}else{
					this.controlTerms('', sPage);
				}
			}
		},

		myLoc : getLocation(),
		
		checkTerms : function (r, s) {
			if(s == '' || s == undefined) var s = this.myLoc;
			if(this.returnUrl()) r = this.returnUrl();
			if(this.srcPage()) s = this.srcPage();	
			if(r == '' || r == undefined){
				this.redirect('', s);
			}else{
				this.redirect(r, s);	
			}
		},

		controlTerms : function (r, s, c, t,q) {
			if(r == undefined) var r = '';
			if(this.returnUrl()) r = this.returnUrl();
			if(this.srcPage()) s = this.srcPage();
			if(this.lang()) q = this.lang();

			if(this.hasCookie(s)){
				if(t != '' || t != null || t != undefined){
					this.redirect2(r, s, true, t,q);
				}else{
					if(r == '' || r == undefined) {
						this.redirect2('', s, true);
					}else{
						this.redirect2(r, s, true,'',q);
					}
				}
			}else{
				if(r == '' || r == undefined) {
					this.redirect2('', s, false);
				}else{
					this.redirect2(r, s, false,'',q);
				}
			}
		},
		
				
		clientType : null,
		
		acceptTerms : function (cname) {
			if(cname != undefined){
				if(setCookie(cname)){
					
					switch(cname)
					{	
					case 'ukAdvisers':
						this.clientType = 'ADVIS';
						break;
					case 'ukAdvisersQA':
						this.clientType = 'ADVIS';
						break;
					case 'ukAdvisersIST':
						this.clientType = 'ADVIS';
						break;
					case 'ukInstitutions':
						this.clientType = 'INSTI';
						break;
					case 'ukInstitutionsQA':
						this.clientType = 'INSTI';
						break;
					case 'ukInstitutionsIST':
						this.clientType = 'INSTI';
						break;
					case 'ukIndividuals':
						this.clientType = 'INDIV';
						break;
					case 'ukIndividualsQA':
						this.clientType = 'INDIV';
						break;
					case 'ukIndividualsIST':
						this.clientType = 'INDIV';
						break;
					case 'sgAdvisers':
						this.clientType = 'ADVIS';
						break;
					case 'sgAdvisersQA':
						this.clientType = 'ADVIS';
						break;
					case 'sgAdvisersIST':
						this.clientType = 'ADVIS';
						break;
					/*case 'sgAdvisersIST1':
						this.clientType = 'ADVIS';
						break;*/
					case 'sgInstitutions':
						this.clientType = 'INSTI';
						break;
					case 'sgInstitutionsQA':
						this.clientType = 'INSTI';
						break;
					case 'sgInstitutionsIST':
						this.clientType = 'INSTI';
						break;
					case 'sgIndividuals':
						this.clientType = 'INDIV';
						break;
					case 'sgIndividualsQA':
						this.clientType = 'INDIV';
						break;
					case 'sgIndividualsIST':
						this.clientType = 'INDIV';
						break;					
					case 'sgAdvisersFC':
						this.clientType = 'ADVIS';
						break;
					case 'sgAdvisersFCQA':
						this.clientType = 'ADVIS';
						break;
					case 'sgAdvisersFCIST':
						this.clientType = 'ADVIS';
						break;
					case 'sgInstitutionsFC':
						this.clientType = 'INSTI';
						break;
					case 'sgInstitutionsFCQA':
						this.clientType = 'INSTI';
						break;
					case 'sgInstitutionsFCIST':
						this.clientType = 'INSTI';
						break;
					case 'sgIndividualsFC':
						this.clientType = 'INDIV';
						break;
					case 'sgIndividualsFCQA':
						this.clientType = 'INDIV';
						break;
					case 'sgIndividualsFCIST':
						this.clientType = 'INDIV';
						break;
					case 'frAdvisors':
						this.clientType = 'ADVIS';
						break;
					case 'frAdvisorsQA':
						this.clientType = 'ADVIS';
						break;
					case 'frAdvisorsIST':
						this.clientType = 'ADVIS';
						break;					
					case 'frInstitution':
						this.clientType = 'INSTI';
						break;
					case 'frInstitutionQA':
						this.clientType = 'INSTI';
						break;
					case 'frInstitutionIST':
						this.clientType = 'INSTI';
						break;
					case 'frIndividuals':
						this.clientType = 'INDIV';
						break;
					case 'frIndividualsQA':
						this.clientType = 'INDIV';
						break;
					case 'frIndividualsIST':
						this.clientType = 'INDIV';
						break;
					
					case 'deInstitution':
						this.clientType = 'INSTI';
						break;
					case 'deInstitutionQA':
						this.clientType = 'INSTI';
						break;
					case 'deInstitutionIST':
						this.clientType = 'INSTI';
						break;
					case 'deFinanzintermediare':
						this.clientType = 'ADVIS';
						break;
					case 'deFinanzintermediareQA':
						this.clientType = 'ADVIS';
						break;
					case 'deFinanzintermediareIST':
						this.clientType = 'ADVIS';
						break;
					case 'dePrivatkunden':
						this.clientType = 'INDIV';
						break;
					case 'dePrivatkundenQA':
						this.clientType = 'INDIV';
						break;
					case 'dePrivatkundenIST':
						this.clientType = 'INDIV';
						break;
					
					default:
						break;
					}	
					this.init();
				}
			}else{
			    //document.write("srcPage: " + this.srcPage()  + " \n");
			    //document.write("cookie: " + this.cookieName(this.srcPage()) + " \n");
				if(setCookie(this.cookieName(this.srcPage()))){
					this.init();
				}
			}
		},

		returnUrl : function () {
			var locArr = getUrlParam(this.myLoc);
			for(var x=0; x<locArr.length; x++){
				if(locArr[x] == 'returnurl'){
					return locArr[x+2];
				}
			}
			return false;
		},

		accepted : function () {
			var locArr = getUrlParam(this.myLoc);
			//alert(2 + " - " + locArr);
			for(var x=0; x<locArr.length; x++){
				if(locArr[x] == 'accepted'){
					return locArr[x+2];
				}
			}
			return false;
		},

		srcPage : function () {
			var locArr = getUrlParam(this.myLoc);
			//alert(3 + " - " + locArr);
			for(var x=0; x<locArr.length; x++){
				if(locArr[x] == 'srcpage'){
					return locArr[x+2];
				}
			}
			return false;
		},

		tnc : function () {
			var locArr = getUrlParam(this.myLoc);
			//alert(3 + " - " + locArr);
			for(var x=0; x<locArr.length; x++){
				if(locArr[x] == 'tnc'){
					return locArr[x+2];
				}
			}
			return false;
		},

		locale : function () {
			var locArr = getUrlParam(this.myLoc);
			//alert(3 + " - " + locArr);
			for(var x=0; x<locArr.length; x++){
				if(locArr[x] == 'locale'){
					return locArr[x+2];
				}
			}
			return false;
		},
		
		
		lang : function () {
			var locArr = getUrlParam(this.myLoc);
			//alert(3 + " - " + locArr);
			for(var x=0; x<locArr.length; x++){
				if(locArr[x] == 'lang'){
					return locArr[x+2];
				}
			}
			return false;
		},
		
		onLoad : function (func) {
			addLoadEvent(func);
		},
				 

		rootLocation : function () { 
			var locArr = getUrlParam(this.myLoc);
			return locArr[1];
		},

		hasCookie : function (s) {
			// if ukCommon
			if(this.cookieName(s) == 'ukCommon' || this.cookieName(s) == 'ukCommonIST' || this.cookieName(s) == 'ukCommonQA'){
				return searchCommonCookie(ukCommonList);
			}
			else if(this.cookieName(s) == 'frCommon' || this.cookieName(s) == 'frCommonIST' || this.cookieName(s) == 'frCommonQA'){
				return searchCommonCookie(frCommonList);
			}
			else if(this.cookieName(s) == 'deCommon' || this.cookieName(s) == 'deCommonIST' || this.cookieName(s) == 'deCommonQA'){
				return searchCommonCookie(deCommonList);
			}
			else if(this.cookieName(s) == 'sgCommon' || this.cookieName(s) == 'sgCommonIST' || this.cookieName(s) == 'sgCommonQA'){
				return searchCommonCookie(sgCommonList);
			}
			else if(this.cookieName(s) == 'sgCommonFC' || this.cookieName(s) == 'sgCommonFCIST' || this.cookieName(s) == 'sgCommonFCQA'){
				return searchCommonCookie(sgCommonFCList);
			}
			else if(this.cookieName(s) == 'inCommonFC' || this.cookieName(s) == 'inCommonFCIST' || this.cookieName(s) == 'inCommonFCQA'){
				return searchCommonCookie(inCommonFCList);
			}
			else if(this.cookieName(s) == 'hkCommonFC' || this.cookieName(s) == 'hkCommonFCIST' || this.cookieName(s) == 'hkCommonFCQA'){
				return searchCommonCookie(hkCommonList);
			}
			else{
				return getCookie(this.cookieName(s));
			}
		},

		cookieName : function (s) {
			return (this.tnc() != false)? this.tnc() : tncCookieName(s)
		}, 
	
		redirect : function (r, s) {
			if(r == '') {
				document.location = controller + "?srcpage=" + s;
			}else{
				document.location = controller + "?returnurl=" + r + "?srcpage=" + s;	
			}
		},
		
		redirect2 : function (r, s, c, t,q) {
			if(c){
				if(r == '' || r == undefined){
					document.location = s;
				}else{
					if(t != null && t != undefined && t != ''){
						


						if(s.indexOf('?')>-1){
							if(s.indexOf('&clientType')>-1){
								var url= s;

								var ind = url.indexOf('&clientType');
								
								var count = url.substring(ind,ind+6);
								
								s = url.replace((url.substring(ind,ind+6)), "");
							}
							
							if(s.indexOf('?clientType')>-1){
								var url= s;

								var ind = url.indexOf('&clientType');
								
								var count = url.substring(ind,ind+6);
								
								s = url.replace((url.substring(ind,ind+6)), "");	
							}
								
								
								document.location = r + "?srcpage=" + s + "&clientType=" + this.clientType;
													
						
						}else{
							document.location = r + "?srcpage=" + s + "?clientType=" + this.clientType;
						}
					}else{
						document.location = r + "?srcpage=" + s;
					}
				}
				
			}else{
				// if ukCommon
/*				var cName = (this.tnc() != false)? this.tnc() : this.cookieName(s);
				var tncDes = tncDestination(cName) 
				if(r == '' || r == undefined){
					document.location = tncDes + "?tnc=" + cName + "?srcpage=" + s;
				}else{
					document.location = tncDes + "?tnc=" + cName + "?returnurl=" + r + "?srcpage=" + s;
				}
	*/			
				
				var cName = (this.tnc() != false)? this.tnc() : this.cookieName(s);
				var tncDes = tncDestination(cName) 
				if(r == '' || r == undefined){
					document.location = tncDes + "?tnc=" + cName + "?srcpage=" + s;
				}else{
					//code here to change tnc
					if(q == '' || q == undefined) {
						document.location = tncDes + "?tnc=" + cName + "?returnurl=" + r + "?srcpage=" + s;
					}
					else if (q.toUpperCase() == 'EN_UK') {
						document.location = tncDes +  "?locale=" + q + "?tnc=" + cName + "?returnurl=" + r + "?srcpage=" + s;
					}
					else {
						var end = "_"+ q + ".html"
						tncDes = tncDes.replace(".html",end) 
						document.location = tncDes +  "?locale=" + q + "?tnc=" + cName + "?returnurl=" + r + "?srcpage=" + s;
					}
				}
			}
		}
	}
}

				