/******************************
* Cookie Util Library
******************************/

var cookieCartName = "sCart";		//cookie cart name	
var COOKIE_YEAR = 1000*60*60*24*365; 	//one year of milliseconds


/**************
* Standard set Cookie
***************/
function setCookie(c_name,value,expires){
	var today = new Date();
	today.setTime( today.getTime() );
	
	var exdate = new Date( today.getTime() + expires );
	document.cookie=c_name+ "=" +escape(value)+((expires==null) ? "" : ";expires="+exdate)+";path=/";
	
	//alert( document.cookie);
}

/**************
* Standard get Cookie
***************/
function getCookie(c_name){
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return null
}

/**************
* returns an array of arrays with the itemId as the key
* both arrays are associative
***************/
function getItems(){
	var stringArray = getItemsString();
	alert( "no:" + stringArray );
	var arrayArray = new Array()
	for( key in stringArray )
	{
		var tempArray =  stringToArray( stringArray[key] );
		arrayArray[key] = tempArray;
	}	
	return arrayArray;
}

/**************
* returns an array of string representations of an array.
***************/
function getItemsString(){
	var cookieArray = new Array()
	var cookieString = document.cookie;
	
	while( cookieString.indexOf( cookieCartName ) != -1 )
	{
		var c_name = cookieString.substring( cookieCartName, 
			cookieString.indexOf( "=", cookieString.indexOf( cookieCartName ) )
			);
			
		thisId = c_name.substring( c_name.indexOf( cookieCartName ) + cookieCartName.length );
		
		c_start=cookieString.indexOf(c_name + "=")
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 
			c_end=cookieString.indexOf(";",c_start)
    		if (c_end==-1) 
					{ c_end=cookieString.length; }
    		 cookieArray[thisId] = unescape(cookieString.substring(c_start,c_end));
		}	    
	 cookieString = cookieString.substring( c_end + 1 );
 }
	
	return cookieArray;
}

/**************
* returns an associative array of necessary data.
***************/
function getOrder( inId ){
	var cookieString = getItemstring( inId );
	if( cookieString ) return stringToArray( cookieString );
	
	return null;
}

/**************
* returns an string representation of an associative array of necessary data.
***************/
function getItemstring( inId ){
	return getCookie( cookieCartName + inId );
}

/**************
* adds an array of data to the cookie
***************/
function addOrder( id, array ){
	//add to cookie
	arrayToCookie( cookieCartName, id, array );
}

/**************
* turns an array of data into a string representation and saves it in the cookie
***************/
function arrayToCookie( cookie, inId, inArray ){
	//convert to string
	var arrayString = arrayToString( inArray );
	
	setCookie( cookieCartName + inId, arrayString, COOKIE_YEAR );
}

/**************
* turns a string representation into an array
***************/
function stringToArray( inString ){
	
	var returnArray = new Array();
	
	while( inString.indexOf('^^') != -1 ){
		var key = inString.substring( 0, inString.indexOf( '^' ) );
		var value = inString.substring( inString.indexOf( '^' ) + 1,
						inString.indexOf( '^^' ) );
					
		returnArray[key] = value;
			
		inString = inString.substring( inString.indexOf( '^^' ) + 2 );
	}
	
	return returnArray;

}

/**************
* turns an array into a string representation
***************/
function arrayToString( inArray ){
	var value = '';
	for (key in inArray) {
		value += key + '^' + inArray[key] + '^^';
	}
	
	return value;	
}

function addItem( albumId ){
        var item = items[ albumId ];

        submitItem( albumId, item.getId() );
}

function resumeItem( albumId, price ){
        var item = items[ albumId ];
        item.setPrice( price );

        addOrder( albumId, item.toArray() );

        //passing cookie string to flash.
        var email = "";
        if( member["Email Address"] )
                email = member["Email Address"];

        //alert(document.cookie);
        parent.leftFrame.sendStore( email, document.cookie );
}


/********** TEST CODE **********************/

var idArray = new Array();
idArray["albumId"] = 438652;
idArray["albumName"] = "Razor's Edge";
idArray["artistName"] = "AC/DC";
idArray["albumPrice"] = "3.00";
idArray["albumURL"] = "/path/to/image.jpg";

function addToCookie(){
	addOrder( idArray["albumId"], idArray );
	addOrder( 22, idArray );

}

function getFromCookie(){
	var outArray = getOrder( idArray["albumId"] );
	//alert( 'albumId: ' + outArray["albumId"] );
		
	var allItems = getItemsString();
	///alert( allItems[ idArray["albumId"] ] );
	//alert( allItems[ 22 ] );

	var orderArray = stringToArray( allItems[ 22 ] );
	alert ( orderArray["artistName"] );
	alert ( orderArray["albumId"] );

}
