var FS_INCLUDE_NAMES = 0, FS_EXCLUDE_NAMES = 1, FS_INCLUDE_IDS = 2, FS_EXCLUDE_IDS = 3, FS_INCLUDE_CLASSES = 4, FS_EXCLUDE_CLASSES = 5;

function getFormString( formRef, oAndPass, oTypes, oNames, oCookieName ) {
	
	var oldItems =  retrieveCookie( oCookieName ) ;
		
	if( oNames ) {
		oNames = new RegExp((( oTypes > 3 )?'\\b(':'^(')+oNames.replace(/([\\\/\[\]\(\)\.\+\*\{\}\?\^\$\|])/g,'\\$1').replace(/,/g,'|')+(( oTypes > 3 )?')\\b':')$'),'');
		var oExclude = oTypes % 2;
	}
		
	for( var x = 0, oStr = '', y = false; formRef.elements[x]; x++ ) {
		if( formRef.elements[x].type ) {
			if( oNames ) {
				var theAttr = ( oTypes > 3 ) ? formRef.elements[x].className : ( ( oTypes > 1 ) ? formRef.elements[x].id : formRef.elements[x].name );
				if( ( oExclude && theAttr && theAttr.match(oNames) ) || ( !oExclude && !( theAttr && theAttr.match(oNames) ) ) ) { continue; }
			}
			var oE = formRef.elements[x]; 
			var oT = oE.type.toLowerCase();
			
			var indexLast = oE.id.lastIndexOf('_') ;
			var idName = oE.id ;
			idName = idName.toLowerCase() ;
			idName = idName.replace('usersetting6' ,'industry') ;
			idName = idName.replace('region' ,'state') ;
			
			if(indexLast!=-1){
			   idName = idName.substring(indexLast+1) ;
			}
			
			if( oT == 'text' || oT == 'textarea' || ( oT == 'password' && oAndPass ) ) {
				oStr += ( y ? ',' : '' ) + idName + ":" + oE.value.replace(/%/g,'%p').replace(/,/g,'%c') ;
				y = true;
			} else if( oT == 'checkbox' ) {
			     // oT == 'radio' || 
				oStr += ( y ? ',' : '' )  + idName + ":" + ( oE.checked ? '1' : '' );
				y = true;
			} 
			else if( oT == 'select-one' ) {
			  	oStr += ( y ? ',' : '' )  + idName + ":" + oE.options[oE.selectedIndex].value ;
				y = true;
			} 
		}
	}
	
	if(oldItems){
	  oldItems = oldItems.split(',') ;
	  for(var i=0; i<oldItems.length; i++){
	    var oldValues = oldItems[i].split(':') ;
	    if(oldValues && oldValues.length==2){
	     if(oStr.indexOf(oldValues[0])==-1){
	        oStr = oldValues[0]+":"+oldValues[1]+","+oStr ;
	      }
	    }
	  }
	}
	
	return oStr;
}

function recoverInputs( formRef, oStr, oAndPass, oTypes, oNames ) {
	if( oStr ) {
		oStr = oStr.split( ',' );
		if( oNames ) {
			oNames = new RegExp((( oTypes > 3 )?'\\b(':'^(')+oNames.replace(/([\\\/\[\]\(\)\.\+\*\{\}\?\^\$\|])/g,'\\$1').replace(/,/g,'|')+(( oTypes > 3 )?')\\b':')$'),'');
			var oExclude = oTypes % 2;
		}
        
		for( var x = 0; x<formRef.length ; x++ ) {
		 	if( formRef.elements[x].type ) {
				if( oNames ) {
					var theAttr = ( oTypes > 3 ) ? formRef.elements[x].className : ( ( oTypes > 1 ) ? formRef.elements[x].id : formRef.elements[x].name );
					if( ( oExclude && theAttr && theAttr.match(oNames) ) || ( !oExclude && ( !theAttr || !theAttr.match(oNames) ) ) ) { continue; }
				}
			    					    
			   for(var i=0; i<oStr.length; i++){
	                var oldValues = oStr[i].split(':') ;
	                if(oldValues && oldValues.length==2){
	                   
	                    var indexLast = formRef.elements[x].id.lastIndexOf('_') ;
			            var idName = formRef.elements[x].id ;
			            idName = idName.toLowerCase() ;
				    idName = idName.replace('usersetting6' ,'industry') ;
				    idName = idName.replace('region' ,'state') ;

			            if(indexLast!=-1){
			                 idName = idName.substring(indexLast+1) ;
			            }
	               
	    	               
	                 if(oldValues[0]==idName){                  
	                    var type = formRef.elements[x].type.toLowerCase() ; 
	               	    
	               	    if( type == 'text' || type == 'textarea' || ( type == 'password' && oAndPass ) ) {
					        formRef.elements[x].value = oldValues[1].replace(/%c/g,',').replace(/%p/g,'%');		
				            } 
				           else if(  type == 'checkbox' ) {
				                // type == 'radio' ||
					           formRef.elements[x].checked = oldValues[1] ? true : false;
				           }
				           else if( type == 'select-one' ){
		    		                   for(var y=0; y<formRef.elements[x].length ;y++){
				                       var ddlItem = formRef.elements[x].options[y] ;
					                       if(ddlItem.value==oldValues[1]){
					                        ddlItem.selected=true;
				                       }
				                }       
			               }           
	                  }	                    
	               }
	            }			
			} 
		} 
	}
} 

function retrieveCookie( cookieName ) {
	/* retrieved in the format
	cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
	only cookies for this domain and path will be retrieved */
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
	}
	return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
	if( !cookieName ) { return false; }
	if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
	/* This next line sets the cookie but does not overwrite other cookies.
	syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]
	Because of the way that document.cookie behaves, writing this here is equivalent to writing
	document.cookie = whatIAmWritingNow + "; " + document.cookie; */
	document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
		( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
		( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
		( isSecure ? ";secure" : "");
	//check if the cookie has been set/deleted as required
	if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
	if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;
}
