var User = {};
	User.uid, User.oid = -1;
	User.errorMessage = '';
	User = {
		Login: function(login,password,terms) {
			document.cookie = 'login=' + encodeURIComponent(login) + '; path=/;';
			document.cookie = 'password=' + encodeURIComponent(password) + '; path=/;';
			document.cookie = 'rules=' + encodeURIComponent(terms) + '; path=/;';
            $.getScript('/auth.php');
		},
		LoggedIn: function(email) {
			$('#bookmarklet > a').attr('href','javascript:(function(){if(!window.a){window.a={};window.a.path="http://shopwisely.org/";window.a.domain=window.location.hostname}window.a.d='+User.uid+';window.a.c='+User.oid+';if(window.a.b)window.a.b();else{document.getElementsByTagName("head").length>0||document.getElementsByTagName("html")[0].insertBefore(document.createElement("head"),document.getElementsByTagName("body")[0]);if(document.getElementById("shopwisely-load")==null){loadShopWisely=document.createElement("script");document.getElementsByTagName("head")[0].appendChild(loadShopWisely).setAttribute("src", window.a.path+"shopwisely.js?rand="+encodeURIComponent(Math.random()));document.getElementsByTagName("head")[0].appendChild(loadShopWisely).setAttribute("id","shopwisely-load")}}})();');
			$('#loginBox').html('Your bookmarklet is now linked to '+email+'! Continue to step 3.').css('text-align','center').css('width','200px');
            $('#logged_in').show();
            $('#logged_out').hide();
		},
		LoginFail: function() {
			$('#login .error').show().text(User.errorMessage);
		}
	};
	
$(document).ready(function() {
$('a#showFundable').click(function() {
	$('#newFundableBox').slideDown('slow');

    return false;

  });
$('#cancel').click(function() {
$('#newFundableBox').slideUp('slow');


    return false;

  });
	
	$('#banner #benef').change(function(){
		User.oid = $(this).val();
		$('#bookmarklet > a').attr('href','javascript:(function(){if(!window.a){window.a={};window.a.path="http://shopwisely.org/";window.a.domain=window.location.hostname}window.a.d='+User.uid+';window.a.c='+User.oid+';if(window.a.b)window.a.b();else{document.getElementsByTagName("head").length>0||document.getElementsByTagName("html")[0].insertBefore(document.createElement("head"),document.getElementsByTagName("body")[0]);if(document.getElementById("shopwisely-load")==null){loadShopWisely=document.createElement("script");document.getElementsByTagName("head")[0].appendChild(loadShopWisely).setAttribute("src", window.a.path+"shopwisely.js?rand="+encodeURIComponent(Math.random()));document.getElementsByTagName("head")[0].appendChild(loadShopWisely).setAttribute("id","shopwisely-load")}}})();');

	});
	$('form#login').submit(function(){
		User.Login($('input#email').val(),$('input#password').val(),$('input#rules:checked').val());
		return false;
	});
});
//$('.saveButton').click(function(){saveChanges(this, false);});
//$('.cancelButton').click(function(){saveChanges(this, revert);});

function edit(id,target,type) {
if(type == 'title') {
var input = '<span><input id="title'+id+'" name="title" type="text" value="'+$(target).text()+'" />';
}
if(type == 'description') {
var input = '<span<textarea id="description'+id+'" cols="55" name="title">'+$(target).text()+'</textarea><br />';
}

	//$(this).click(function () {
							var revert = $(target).text();
	if(type == 'amount') {	
var input = '<span><input id="amount'+id+'" name="title" type="text" size="5" value="'+$(target).text()+'" />';
input += '<br />';	
}						
var button	 = '<input type="button" value="Save" class="saveButton" onclick="confirmSave(this,'+id+',\''+type+'\',\''+revert+'\')" /><input type="button" value="Cancel" class="cancelButton" onclick="saveChanges(this,\''+revert+'\','+id+',\''+type+'\')" /></span>';

$(target).after(input+button).remove();

  //});

};

function confirmSave(target,id,type,revert) {
	$('#passwordBox').show();
	$('#save').html('<input type="button" value="save" class="saveButton" onclick="saveChanges(this,false,'+id+',\''+type+'\')" /><input type="button" value="Cancel" class="cancelButton" onclick="saveChanges(this,\''+revert+'\','+id+',\''+type+'\')" />');
	
}
function saveChanges(obj, cancel, id, type) {
if(!cancel) {
	
var t = $('#'+type+id).val();
var g = $('#password').val();
$.post("/process",{
  content: t,
  pass: g,
  id: id,
  type: type
},function(txt){
if(txt == 1) {
$('#'+type+id).parent().after('<div onclick="edit(\''+id+'\',this,\''+type+'\')">'+t+'</div>').remove();
$('#passwordBox').hide();
}
if(txt == 0) {
	alert('Incorrect password');
}
});




}
else {
var t = cancel;
$('#'+type+id).parent().after('<span onclick="edit(\''+id+'\',this,\''+type+'\')">'+t+'</span>').remove();
$('#passwordBox').hide();
}

//if(t=='') t='(click to add text)';

}



function load(name, div) {
	if(div == 'cat2') {
	$('#pageContent').hide();
	$('#cat2').show();
	$('#goBack').show();
	}
	  $('div#'+div).hide().load(name, function() { $('div#'+div).slideDown('slow'); });
	  
	return false;
}
//search
function searchQ() {
query = $("#query").val();
cat = $("#cat").val();
url = '/results?q=' + query + '&cat=' + cat;
urle = url.replace(" ","+");

load(urle, "cat2");
}
function onEnter(e){
	 var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13) {
	searchQ();
	//MM_effectAppearFade('cat2', 1000, 0, 100, false);
	 
	 	return false;
		}
          
		
     else { 
          return true;
		  }
	
}
function hideDeals(div) {
	$('div#'+div).slideUp('slow');
	
}
//popup
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//bookmark
function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

//beneficiary preview
function setLink( oid ) {
url = '/get/partnerdetails.php?id=' + oid;
load(url, "details");
}
//go back
function goBack() {
	$('#cat2').hide();
	$('#goBack').hide();
		$('#pageContent').slideDown('fast');
	 //MM_effectAppearFade('cat2', 1000, 100, 0, false);
	 //MM_effectAppearFade('pageContent', 1000, 0, 100, false);
	 
	}
	
	//email
	function postForm( formName )
{
    theForm = document.getElementById( formName );
    
    query = '';
    for( i=0; i < theForm.length; i++ )
    {
        query = query + theForm.elements[ i ].name +'='+ theForm.elements[ i ].value +'&';
    }
        
    req = new XMLHttpRequest();
    req.onreadystatechange = function()
    {
        if( req.readyState == 4 )
        {
            alert( 'Welcome to the club! \n If you did it right, there should be a confirmation in your inbox. \n Just click the link in the email to join.' );
        }
    }
    
    req.open( 'POST', "/list/mail.cgi", true );
    req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' );
    req.setRequestHeader( 'Content-Length', query.length );
    req.send( query );
}



function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13) {
	 postForm('newsletter');
	 javascript:disableSubmit('submit');
	 
	 	return false;
		}
          
		
     else { 
          return true;
		  }
}
function disableSubmit(whichButton)
{
    if (document.getElementById)
    {
        // this is the way the standards work
        document.getElementById(whichButton).disabled = true;
    }
    else if (document.all)
    {
        // this is the way old msie versions work
        document.all[whichButton].disabled = true;
    }
    else if (document.layers)
    {
        // this is the way nn4 works
        document.layers[whichButton].disabled = true;
    }
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


