function getCookieValue(n){
	// This method has bug as uses indexof to determine the cookie .. selections, hotelselections ahd holidayselections
    cks=document.cookie;
    i=cks.indexOf(n);
    if(i==-1)return null;
    start=i+n.length+1;
    end=cks.indexOf(';',start);
    if(end==-1)end=cks.length;
    return unescape(cks.substring(start,end));
}

function readCookieValue(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0){
			return c.substring(nameEQ.length,c.length);
		}
	}
	return '';
}



function setCookie(n,v){
    setExpireCookieWithPath(n,v,"/");
}

//utility to set the cookie with not only the name and value 
//but also the path and expiry date for this cookie, 
function setExpireCookieWithPath(n,v,path,days){
    if(path){
        //do nothing (Use the path provided)    
    }else{
        // defult path
        path="/";
    }

    var expires="";
    if(days){
        var date=new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires="; expires="+date.toGMTString();
    }else{
        expires="";
    }

    briansUniqueStringName=n+"="+escape(v)+expires+"; path="+path;
    document.cookie=briansUniqueStringName;
}

//utility to set the User Context cookie with not only the name and value 
//but also the path and expiry date for this cookie, 
//escape function on value string is not applied
function setUCCookieWithPathExpire(n,v,path){
    if(path){
        //do nothing (Use the path provided)    
    }else{
        // defult path
        path="/";
    }
    var date=new Date();
    //180 days - as found in usercontextcookiehelper.java
    date.setTime(date.getTime()+(180 * (24 * 60 * 60)));
    var expires="; expires="+date.toGMTString();
    briansUniqueStringName=n+"="+v+expires+"; path="+path;
    document.cookie=briansUniqueStringName;
}

function setExpireCookie(n,v,days){
    setExpireCookieWithPath(n,v,"",days);
}

function eraseCookie(n){
    var v=getCookieValue(n);
    setExpireCookie(n,v,-1);
}

// Utility to set a context cookie.
// We don't want to have to reset the cookie value entirely, so we need a method that gathers
// the current contents of the cookie and adds anything new to it. Cookie's valid for 180 days
// n=name, v=value
function setContextCookie(n,v){
    if(getCookieValue(n)==null){
        setExpireCookieWithPath(n,v,"/",180);
    }else{
        // qualifier-custtype-qf2leisure/qualifier-region-au
        // never seem to write back though
        var cq=getCookieValue(n).split("/"),nq=v.split("/"),nqt=[],nqv=[],cqt=[],cqv=[],pq=[];
        // go through list of new qualifiers
        var origLength=cq.length,candidate="";
        for(var i=0;i<nq.length;i++){
            nqt[i]=nq[i].substring(nq[i].indexOf("-")+1,nq[i].lastIndexOf("-"));
            nqv[i]=nq[i].substring(nq[i].lastIndexOf("-")+1);
            // for each new qualifier go through current qualifiers
            for(var j=0;j<origLength;j++){
                cqt[j]=cq[j].substring(cq[j].indexOf("-")+1,cq[j].lastIndexOf("-"));
                cqv[j]=cq[j].substring(cq[j].lastIndexOf("-")+1);
                
                //if the type on this current matches that on the new, reset the value
                if(cqt[j]==nqt[i]){
                    cq[j]="qualifier-"+cqt[j]+"-"+nqv[i];
                } else {
                    //this is a potential new qualifier
                    // will be one if it never matches any current qualifiers
                    for(var k=0;k<origLength;k++){
                        candidate+=cqt[k];
                    }
                    if(!(candidate.indexOf(nqt[i])>-1)){
                        cq[cq.length]="qualifier-"+nqt[i]+"-"+nqv[i];
                    }
                }
            }
        }
        // now set the new cookie
        var newv="";
        for(var i=0;i<cq.length;i++){
            sep=(i==cq.length-1)?"":"/";
            newv+=cq[i]+sep;
        }

        setExpireCookieWithPath(n,newv,"/",180);
    }
}

//utility to get the value from the cookie for a given context type
// cc=contextCookie, n=name, t=type, it=index of t, iqs=index of qualifier separator
// iqe=index of qualifier end
// TODO: fix the horrible horrible hack
function getContextCookieValue(n,t) {
    var result="",cc=getCookieValue(n);
    if (null!=cc){
        var it=cc.indexOf(t);
        if (it!=-1) {
            var iqs = cc.indexOf("-",it);
            var iqe = cc.indexOf("/",iqs);
            //this was the last qualifier in the string
            if (iqe==-1){
                result=cc.substring(iqs+1);     
            //this was NOT the last qualifier in the string - only take until the next "/"
            }else {
                result=cc.substring(iqs+1,iqe);
            }               
        }
// horrible horrible hack, by Brian Bannister 18/2/02. Someone just shoot me
// the hack sets the default if, somehow, the context cookie is null
    }else{
        if("custtype"==t){
            result="qf2leisure";
        }else if("region"==t){
            result="au";
        }
    }
    return result;
}
//utility to set the cookie with a given value for a given context type
//This is done by reading what is currently in the cookie and then fiddling
//with the string to insert the new value in place of the current value
//already in the cookie
// cc=current cookie, nc=new cookie, n=name, t=type, v=value
// it=index of type
// iqs=index of qualifier separator
// iqe=index of qualifier end
function setContextCookieValue(n,t,v) {
    //the cookie looks something like "qualifier-custtype-qf2leisure/qualifier-region-au"
    var cc=getCookieValue(n),nc="",it=cc.indexOf(t);
    if (it != -1) {
        //found value for this type in the cookie
        var iqs=cc.indexOf("-",it);
        var iqe=cc.indexOf("/",iqs);
        nc=cc.substring(0,iqs+1)+v;
        //this was NOT the last qualifier in the string so we have to stuff around a bit
        if(iqe != -1){
            nc=nc+cc.substring(iqe);
        }
        setContextCookie(n,nc);
    }
}
//---------------Code Added for Cookies---------CQ 15824------Starts-----------
function getCookieArray(n){
    if((document.cookie.indexOf(n))!=-1){   
        var userContextCookie=getCookieValue(n);
        nameValueTupleArray = userContextCookie.split("|");
        return nameValueTupleArray;
    }
}
//---------------Code Added for Cookies---------CQ 15824------Ends-----------
/**
 * Add or Update UC (User Context Cookie)
 * @param keyName - String containing the Key Name
 * @param keyValue - String containing the Key Value
 */
function addOrUpdateUCCookie (keyName, keyValue) {
    if (keyValue == '') {
        return;
    }
    // UC cookie setting changes - starts
    var pipe        =   '|';
    var hash        =   '#';
    var ucCookieVal =   getCookieValue('usercontext');
    var tokens      =   ucCookieVal.split(pipe);
    var tobeset     =   "";
    var found       =   false;
    for(var index = 0; index < tokens.length; index++)
    {
        var token       =   tokens [index];
        var valueToken  =   token.split (hash);
        if (keyName != valueToken [0]) {
            tobeset     =   tobeset + tokens[index] + pipe;
        }
        else {
            tobeset     =   tobeset + keyName + hash + keyValue + pipe;
            found       =   true;
        }
    }
    
    if (!found) {
        tobeset     =   tobeset + keyName + hash + keyValue + pipe;
    }
    
    if (tobeset.length > 1) {
        if (tobeset.substring(tobeset.length - 1) == pipe) {
            tobeset = tobeset.substring(0, (tobeset.length - 1));
        }
        setUCCookieWithPathExpire ('usercontext', tobeset, '/');
    }
    // UC cookie setting changes - starts
   
	//---------------Code Added for Cookies---------CQ 15824------Starts-----------
	function getCookieArray(n){
		if((document.cookie.indexOf(n))!=-1){	
			var userContextCookie=getCookieValue(n);
			nameValueTupleArray = userContextCookie.split("|");
			return nameValueTupleArray;
		}

	}
	//---------------Code Added for Cookies---------CQ 15824------Starts-----------

}
