
////////////////////////////////////////////////////////////
//Bookmark Settings v1.04
////////////////////////////////////////////////////////////

var MainDir    = "http://www.primaveraphoto.com/bookmarks";
var PageName   = "http://www.primaveraphoto.com/bookmarks.php"; //Page your bookmarks are shown on ...

var sepchar    = "@"; // unique separator character leave as is ...
var BMtotal    = 30;	  // max number of bookmarks permitted
var Show_Count = 1;   // Show bookmark number count 1=yes 0=no
var expDays    = 90;  // cookie variables - expiry #days

var My_Width   = "10%"; //Set the width of our bookmarks list table ...
var Bullet     = "del3.gif";

////////////////////////////////////////////////////////////


var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function ListBookmarks(){
	
    var NumBookmarks   = GetCookie('PT_NumBookmarks');
    var i;
	var Bookmark;
    var MyCount;
    
    //alert("ListBookMarks started");
    //alert('Book'+NumBookmarks);

	if (NumBookmarks == null){
	NumBookmarks = 0;
	}
	ShowCount = 0; SwapColour = 0;
    
    //Write our list table ...
    document.write("<table width='"+My_Width+"' cellspacing=0 cellpadding=4 border=0>");
    for (i=1; i <= NumBookmarks; i++){
    Bookmark = GetCookie('PT_Bookmark'+i);
    if (Bookmark != null){
	PrintBookmark(Bookmark, i);
    MyCount = 0;
  }
}
document.write("</table>");

//Show if no bookmarks are saved ...    
if (MyCount != 0){
document.write("<table width=300 cellspacing=0 cellpadding=0 border=0><tr><td align=left class=M1 nowrap>&nbsp;<img src='"+MainDir+"/images/aro_blue.gif'><font color='#CC0000' Face='Verdana'>You have no personal bookmarks saved!</font></td></tr></table>");
}
}

function DeleteBookmark(Count){
	//alert('Delete '+Count);
    DeleteCookie('PT_Bookmark'+Count);
	window.location = window.location;
}

/*
function Delete_All(path){
	// Get cookie string and separate into individual cookie phrases:
	var cookie_string = "" + document . cookie;
	var cookie_array = cookie_string . split ("; ");

	// Try to delete each cookie:
	for (var i = 0; i < cookie_array . length; ++ i)
	{
		var single_cookie = cookie_array [i] . split ("=");
		if (single_cookie . length != 2)
			continue;
		var name = unescape (single_cookie [0]);
		delete_cookie (name, path);
	}
}
*/

function PrintBookmark (Bookmark, Count){

	var color = "";
    var pairs = Bookmark.split(sepchar);
	pairs[0]= unescape(pairs[0]);
	pairs[1]= unescape(pairs[1]);
	//alert(Bookmark+" "+sepchar);
	var BMtitle = pairs[0];
	var BMaddress = pairs[1];
	//alert(BMaddress);

	//SwapColour = 1 - SwapColour;
	//if (SwapColour==1) {color = "bgcolor='#EEEEEE'"} ;	

    ShowCount++;
	//document.write("<table border=1 width=300><tr " + color + ">");
	//Show count ...
    document.write("<tr>");
    if (Show_Count == 1){
    pad = str_pad(ShowCount, 2, 0, 'STR_PAD_LEFT');
    document.write("<td width=10 align=center class=M1><b>"+pad+".</td>");
	}
    document.write("<td width=100% align=left  nowrap><!img src='"+MainDir+"/images/aro_blue.gif'><a href='"+BMaddress+"'>"+BMtitle+"</a>");
	//alert("Ugg!");
	//document.write("<td width=75 align=center><small>"+"<a href='javascript:EditItem("+ShowCount+","+Count+")'>Edit</a>");
	  
    document.write("<td width=75 align=right>"+"<p><a href='#' OnClick='DeleteBookmark("+Count+"); return false;'><img src='"+MainDir+"/images/"+Bullet+"' border=0></a></p</td></tr>");
    //document.write("<td width=75 align=right class=M1 nowrap>"+"&nbsp;<img src='"+MainDir+"/images/"+Bullet+"' align=absmiddle hspace=2 vspace=0>"+"<a href='#' OnClick='DeleteBookmark("+Count+"); return false;'>Delete</a></td></tr>");
}


function AddBookmark(BMtitle, BMaddress){

	var NumBookmarks = GetCookie('PT_NumBookmarks');
	var i;
	var ToDoItem;
	var Bookmark;
	var OldestBookmark = 0;
	var CountBookmarks = 0;

    if (NumBookmarks == null) 
	{	NumBookmarks = 0;	}
	
	//Check if already exists, and count bookmarks ...
	for (i=1; i <= NumBookmarks ; i++){
	Bookmark = GetCookie('PT_Bookmark'+i);
		if (Bookmark != null) 
		{	CountBookmarks++;			
			if (OldestBookmark == 0)
			{	OldestBookmark = i;	}	
		}
	
    //This fixes a bug where we may book mark our anchor.
    //Thus causing several instances of a bookmark ...
    var BmCheck = (BMtitle+sepchar+BMaddress);
	BmCheck = BmCheck.replace(/#/, '');
    
    if (Bookmark == BmCheck){
    //Dynamic Alert Block ...     
    var yes = new LertButton('Close Window', function() {
	//do nothing
    });
    var message = (BMtitle+" - Bookmarked Previously");
	var AddBookmarkLert = new Lert(message,
    [yes],{ // Define Vars ...
	defaultButton:yes,
	icon: MainDir+'/i/dialog-error.gif'});
    AddBookmarkLert.display();
    
	return;			
    }
	}

	//Check if limit reached ...
	if (CountBookmarks >= BMtotal){	
    //alert('Maximum Count Reached ' +CountBookmarks);
    DeleteBookmark(OldestBookmark);
	}

	//Now add our bookmarks ...
	NumBookmarks++;
	
    //This fixes a bug where we may book mark our anchor.
    //Thus causing several instances of a bookmark ...
    var BmCheck = (BMtitle+sepchar+BMaddress);
	BmCheck = BmCheck.replace(/#/, '');
    
    SetCookie('PT_Bookmark'+NumBookmarks, BmCheck, exp);
	SetCookie('PT_NumBookmarks',NumBookmarks, exp);
    
    //Dynamic Alert Block ...     
    var yes = new LertButton('Close Window', function() {
	//do nothing
    });
    var show = new LertButton('Show Bookmarks', function() {
	window.location.href = PageName;
	});
    var message = ("Bookmark added for - "+BMtitle);
	var AddBookmarkLert = new Lert(message,
    [show,yes],{ // Define Vars ...
	defaultButton:yes,
	icon: MainDir+'/i/dialog-information.gif'});
    AddBookmarkLert.display();
    
}

//Cookie functions ...

function getCookieVal (offset) {  
    var endstr = document.cookie.indexOf (";", offset);  
    if (endstr == -1)    
    endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  

	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  

	while (i < clen) {    
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}


function SetCookie (name, value) {  

	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = "/"; //Dir Fix ...
    //var path = (argc > 3) ? argv[3] : null; 
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
    
	document.cookie = name + "=" + escape (value) + 
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
			((path == null) ? "" : ("; path=" + path)) +  
			((domain == null) ? "" : ("; domain=" + domain)) +    
			((secure == true) ? "; secure" : "");

}

function DeleteCookie (name) {  

	var exp = new Date();  

	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function str_pad(input, pad_length, pad_string, pad_type) {
    // Returns input string padded on the left or right to specified length with pad_string  
    // version: 905.2617
    // discuss at: http://phpjs.org/functions/str_pad
    // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // namespaced by: Michael White (http://getsprink.com)
    
    var half = '', pad_to_go;
    var str_pad_repeater = function(s, len) {
    var collect = '', i;

        while(collect.length < len) {collect += s;}
        collect = collect.substr(0,len);

        return collect;
    };

    input += '';

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }
return input;
}