/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
	if(expires) { var dd = new Date(expires);}

    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + dd.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

var qs = "";

function getFromCookie(action){

//action -> nume generic ptr pagina pe care se aplica autosave-ul

	var Key, sInput;
			
	extractCookie(action);	
	
	if(qs!=""){		
			var aInput = qs.split("; ");
			for(var i=0; i< aInput.length; i++){
				var sInput = aInput[i].split("=");						
					if(typeof document.getElementsByName(sInput[0])[0] != 'undefined'){
						if(document.getElementsByName(sInput[0])[0].type == 'checkbox')
							document.getElementsByName(sInput[0])[0].checked = sInput[1];					
						else
							document.getElementsByName(sInput[0])[0].value = sInput[1];					
					}										
			}	
	}			
}

function AutoSaveFromCookie(action){		
	
	if(action=='prjnote' && extractValueFromCookie('prjnote', 'PrjNoteID') == document.getElementsByName("PrjNoteID")[0].value 
	&& extractValueFromCookie('prjnote', 'PrjID') == document.getElementsByName("PrjID")[0].value)

		if(confirm('Recovery: aveti o nota salvata in memoria de recovery - doriti sa re-incarcati datele?')){
			getFromCookie('prjnote');			
		}

	if(action=='prjedit' && extractValueFromCookie('prjnote', 'PrjID') == document.getElementsByName("PrjID")[0].value)
		
		if(confirm('Recovery: aveti detaliile unui proiect salvate in memoria de recovery - doriti sa re-incarcati datele?')){
			getFromCookie('prjedit');					
		}
}

function SaveCookies(action, formName)
{
	var qs = "";//cookie string;
	var d = new Date();
	var myForm = document.getElementsByName(formName)[0];
	
	if(action=="") action = "prjedit";	
	
	if(typeof myForm == 'undefined'){
		alert('Formul ' + formName + ' nu exista!');
		return;
	}
	for(i=0;i<myForm.elements.length;i++){
		if(myForm.elements.item(i).value !="" && myForm.elements.item(i).name != ""){ // && (myForm.elements.item(i).type == 'text' || myForm.elements.item(i).type == 'hidden') 			
			if(myForm.elements.item(i).type == 'checkbox')
				qs += myForm.elements.item(i).name + "=" + myForm.elements.item(i).checked + '; ' ;
			else
				qs += myForm.elements.item(i).name + "=" + myForm.elements.item(i).value + '; ' ;
		}	
	}
	if(qs!=""){ 		
		setCookie('autosave_' + action, qs, d.getMonth() + '/' + d.getDate() + '/' + d.getYear() + 1);
	}	
}

function extractCookie(action){
	var aCookie = document.cookie.split("; ");
	
	for (var i=0; i < aCookie.length; i++)
	{	  
	  var aCrumb = aCookie[i].split("=");	  
	  if (aCrumb[0].indexOf('autosave_' + action)!=-1 && typeof aCrumb[1] != 'undefined'){
		qs = aCrumb[1];
		qs = unescape(qs);			
		break;
	   }	   
	}	
}

function extractValueFromCookie(action, Input){
	var id; 
	
	if(Input=="") return "";
	
	if(qs=="") extractCookie(action);		
	
	if(qs.indexOf(Input + "=")!= -1){			
		id = qs.substr(qs.indexOf(Input + "=") + (Input + "=").length, qs.indexOf("; ", qs.indexOf(Input + "=")) - qs.indexOf(Input + "=") - (Input + "=").length)
			return id;		
	}
	else return "";
}

var as = 0;

function EnableAutoSave(obj)
{
	if((!obj.readOnly) && (!obj.disabled)) as = 1;		
}