//---------------------------------------------------------------------------------//
//----- This file is for implementation it will be cleand up for deployment -------//
//---------------------------------------------------------------------------------//

// Original JavaScript code by Duncan Crombie: dcrombie at chirp.com.au
// Please acknowledge use of this code by including this header.
// This Version modified by Danesha Marasinge: ifs Sri Lanka


// The functions are designed to handle single value cookies, multi cookies are not handled

// function getCookie(name) if the cookie specfied by the name exists its value is returned else null is returned
// function setCookie(name, value, expdays, path, domain, secure ) creates a cookie based on the supplied parameters 
// function deleteCookie(name,path,domain) the cookie that matches the parameters is deleted (expired)
// function setExpiry(days) a helper function used to set the validity of a cookie

// function setHomeUrl(url,validDays) creates a cookie which stores the users prefered homepage in the "ifsHome" cookie,
//									  usually it will be valid for a year, also creates a cookie to store the checkbox value 
//									  in the cookie "chifsHome"
// function clearHomeUrl() clear the "chifsHome" and "ifsHome" cookies
// function checkPref(cookieName) checks for the existance of a cookie and sets a check box  		


function getCookie(name) { // use: getCookie("name");
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

function setCookie(name, value, expdays, path, domain, secure ) { // use: setCookie("name", value, expires, path, domain, secure );

	var expire = new Date();
	var expire = setExpiry(expdays);

document.cookie = name + "=" +escape( value ) +
( ( expire ) ? ";expires=" + expire.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );

}


function deleteCookie(name,path,domain){ //use deleteCookie("name",path,domain);
	
	if (getCookie (name) != null){
		document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 29-Feb-1980 00:00:01 GMT";
		//--------------- Debugging -----------
		//document.write(name + " Cookie Expired");
		//--------------- Debugging -----------
	}
}

function setExpiry(days){ //use x = setExpiry(days)

 var today = new Date();
 var expire = new Date(today.getTime() + days* 24 * 60 * 60 * 1000);
 return expire; 	
 
} 

//----------------------------------------------------------//


function setHomeUrl(url,validDays){
	setCookie("ifsHome",url,validDays,"/","","");
	setCookie("chifsHome","true",validDays,"/","","");
}

function clearHomeUrl(){
	deleteCookie("ifsHome","/","");
	deleteCookie("chifsHome","/","");
}

function checkPref(cookieName){
	var prefFlag 
	if (getCookie(cookieName)){
 		prefFlag = true;
 	}
 	else{
 		prefFlag =false;
 	}
	return prefFlag;
}

function wtogglePref(){

 var C1 = document.chPrefForm.cSetPref;
 if (C1.checked){
	setHomeUrl(C1.value,'1');
	C1.checked = true;
 }
 else{
	clearHomeUrl();
	C1.checked =false;
 }	
}

function wcheckPref(){
	var C1 = document.chPrefForm.cSetPref;;
	
 	if (checkPref('chifsHome')){
		var url = getCookie('ifsHome');
		if (url == C1.value){
			C1.checked = true;
		}
		else{
			C1.checked = false;
		}
 	}
	else{
		C1.checked = false;
	}
}