/**
 * The Cookie function contains code for reading and writing the PlayNow cookies.
 * 
 * It shouldn't contain any references to StateManager.
 */
function Cookie() {
	
	var phoneModelCookie = "phone_model";
	
	this.init = function() {}

    this.createCookie = function(name,value,days) {
	    if (days) {
		   var date = new Date();
		   date.setTime(date.getTime()+(days*24*60*60*1000));
		   var expires = "; expires="+date.toGMTString();
	   }
	   else var expires = "";
	   document.cookie = name+"="+value+expires+"; path=/";
	}

	this.readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		var result = "";
		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) {
				result = c.substring(nameEQ.length,c.length);
				break; 
			}
		}

		return result;
	}
   
	this.readCookiePN = function(subCookie) {
		var returnValue = "";
		var pnCookie = this.readCookie("pncookie");
		if(pnCookie != null) {
			var ca = pnCookie.split('?');
			for(var i=0;i<ca.length;i++){
				var str = ca[i];
				if(str.length > subCookie.length){
					if(str.substr(0, subCookie.length) == subCookie){
						returnValue = str.substr(subCookie.length + 1);
					}
				}
			}
		}
		return returnValue;
	}

	this.eraseCookie = function(name) {
		this.createCookie(name,"",-1);
	}

	this.getPhoneModelCookieName = function(){
		return phoneModelCookie;
  	}

	
	this.setCountryCookie = function(country) {
		   	       
		// Start making a new pncookie. 
		// Add the new value for the "country" key/value pair.
		var newpncookie = "pncookie=country/" + country;
		
		// Get the old cookies. Split them into individual cookies (separated by ';')
		var pncookie = this.readCookie("pncookie");
		
		// Split the pncookie into individual key/value pairs, (separated by '?')
		var oldpncookies = pncookie.split("?");

		// Loop through all pairs...
		var j = 0;
		while (j < oldpncookies.length) {
			// ...and if a pair is *not* the old "country" pair
			if (oldpncookies[j].indexOf("country/") != 0) {
				// ...add it to the new pncookie
				newpncookie += "?" + oldpncookies[j];
			}
			j++;
		}
	
		// Finalize the cookie by adding expiry date (180 days from now) and path. 
		var date = new Date();
		date.setTime(date.getTime()+(180*24*60*60*1000));	
		newpncookie += "; expires=" + date.toGMTString() + "; path=/";
		
		// Set the new cookie.
		document.cookie = newpncookie;
	
	}
	
	this.getKeyValue = function(key){
		var pncookie = this.readCookie("pncookie");
		var pairs = pncookie.split("?");

		// Loop through all pairs...
		var j = 0;
		try {
			while (j < pairs.length) {
				if (pairs[j].indexOf(key+"/") != -1) {
					return (pairs[j].split('/')[1]);	//return the value
				}
				j++;
			}
		} catch ( e ){
			debug("Failed to get keyvalue ["+key+"] from cookie", e );
		}
		return null;
	}
	
	this.clearKeyValue = function(key){
		// Start making a new pncookie. 
		// Add the new value for the "hasNewDL" key/value pair.
		var newpncookie = "pncookie=";
		
		// Get the old cookies. Split them into individual cookies (separated by ';')
		var pncookie = this.readCookie("pncookie");
		
		// Split the pncookie into individual key/value pairs, (separated by '?')
		var oldpncookies = pncookie.split("?");
		
		// Loop through all pairs...
		var j = 0;
		while (j < oldpncookies.length) {
			// ...and if a pair is *not* the old "hasNewDL" pair
			if (oldpncookies[j].indexOf(key + "/") != 0) {
				// ...add it to the new pncookie
				newpncookie += "?" + oldpncookies[j];
			}
			j++;
		}
	
		// Finalize the cookie by adding expiry date (180 days from now) and path. 
		var date = new Date();
		date.setTime(date.getTime()+(180*24*60*60*1000));	
		newpncookie += "; expires=" + date.toGMTString() + "; path=/";
		
		// Set the new cookie.
		document.cookie = newpncookie;	
	}
	
	this.setKeyValue = function(key, value) {
		// Start making a new pncookie. 
		// Add the new value for the "hasNewDL" key/value pair.
		var newpncookie = "pncookie="+key+"/" + value;
		
		// Get the old cookies. Split them into individual cookies (separated by ';')
		var pncookie = this.readCookie("pncookie");
		
		// Split the pncookie into individual key/value pairs, (separated by '?')
		var oldpncookies = pncookie.split("?");
		
		// Loop through all pairs...
		var j = 0;
		while (j < oldpncookies.length) {
			// ...and if a pair is *not* the old "hasNewDL" pair
			if (oldpncookies[j].indexOf(key+"/") != 0) {
				// ...add it to the new pncookie
				newpncookie += "?" + oldpncookies[j];
			}
			j++;
		}
	
		// Finalize the cookie by adding expiry date (180 days from now) and path. 
		var date = new Date();
		date.setTime(date.getTime()+(180*24*60*60*1000));	
		newpncookie += "; expires=" + date.toGMTString() + "; path=/";
		
		// Set the new cookie.
		document.cookie = newpncookie;		
	}
	
	
	this.setNewDownloadsCookie = function(hasNewDL) {
		// Start making a new pncookie. 
		// Add the new value for the "hasNewDL" key/value pair.
		var newpncookie = "pncookie=hasNewDL/" + hasNewDL;
		
		// Get the old cookies. Split them into individual cookies (separated by ';')
		var pncookie = this.readCookie("pncookie");
		
		// Split the pncookie into individual key/value pairs, (separated by '?')
		var oldpncookies = pncookie.split("?");

		// Loop through all pairs...
		var j = 0;
		while (j < oldpncookies.length) {
			// ...and if a pair is *not* the old "hasNewDL" pair
			if (oldpncookies[j].indexOf("hasNewDL/") != 0) {
				// ...add it to the new pncookie
				newpncookie += "?" + oldpncookies[j];
			}
			j++;
		}
	
		// Finalize the cookie by adding expiry date (180 days from now) and path. 
		var date = new Date();
		date.setTime(date.getTime()+(180*24*60*60*1000));	
		newpncookie += "; expires=" + date.toGMTString() + "; path=/";
		
		// Set the new cookie.
		document.cookie = newpncookie;
	}
	
}

var cookie = new Cookie();

///////////////////////////////////////////////////////////////////////////////////
// DO NOT remove the next newline and DO NOT add code after this comment.
