/**
|| Function to bookmark the site.
|| Tested on both IE and Firefox, for others it should default to the CTRL-D for Netscape and
|| CTRL-T for Opera.
*/
function bookmark(){
  var title = "Dance Prance";
  var url = "http://www.danceprance.com";
  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");
  }
}

function updateListing(id, listingId, memberId, editElement) {
    window.location = "editListing-exec.php?editElem="+editElement+"&lid="+listingId+"&mid="+memberId+"&"+editElement+"="+urlEncode(document.getElementById(id).value);
}

function updateListingSelect(id, listingId, memberId, editElement) {
    var sel = document.getElementById(id); 
    window.location = "editListing-exec.php?editElem="+editElement+"&lid="+listingId+"&mid="+memberId+"&"+editElement+"="+urlEncode(sel.options[sel.selectedIndex].value);
}

function updateListingMultipleChoiceElement(id, listingId, memberId, editElement) {
    var arr = new Array();
    arr = document.getElementsByName(id);
    var val = '';
    for(var i = 0; i < arr.length; i++)
    {
        var obj = document.getElementsByName(id).item(i);
        if (obj.checked) {
            val = obj.value;
        }
    }
    window.location = "editListing-exec.php?editElem="+editElement+"&lid="+listingId+"&mid="+memberId+"&"+editElement+"="+urlEncode(val);
}

function urlEncode( s )
   {
      return encodeURIComponent( s ).replace( /\%20/g, '+' ).replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /\~/g, '%7E' );
   }
   
function urlDecode( s )
   {
      return decodeURIComponent( s.replace( /\+/g, '%20' ).replace( /\%21/g, '!' ).replace( /\%27/g, "'" ).replace( /\%28/g, '(' ).replace( /\%29/g, ')' ).replace( /\%2A/g, '*' ).replace( /\%7E/g, '~' ) );
   }

function clearPriceEntry() {
	var obj = document.getElementById("price");
	obj.value = "N/A";
}

function clearPriceNA() {
	var obj = document.getElementById("price");
	obj.value = "";

	var obj = document.getElementById("priceNA");
	obj.checked = false;
}

$(document).ready(function(){ 
    $("#sortSelect").change(function() 
    { 
        url = $("#sortSelect").val();
        $(location).attr('href',url);        
    });
    
    // -------------------------------------------------------------------------
    // Handle Search Form at the top of page. Save user selection from page
    // to page. 
    // Depends on the url GET params.
    // -------------------------------------------------------------------------
    var sk = $.getUrlVar('sk');
    if (sk) { //searching from a link - with search key
        $("#c option[value='"+$.urlDecode(sk)+"']").attr('selected', 'selected'); 
    } else { //searching from search form
        $("#c option[value='"+$.urlDecode($.getUrlVar('c'))+"']").attr('selected', 'selected');    
        $("#s").val($.urlDecode($.getUrlVar('s')));    
        $('input:radio[name=type]').filter('[value='+$.getUrlVar('type')+']').attr('checked', true); 
        $('input:radio[name=condition]').filter('[value='+$.getUrlVar('condition')+']').attr('checked', true); 
        $('input:radio[name=gender]').filter('[value='+$.getUrlVar('gender')+']').attr('checked', true); 
    }
    // -------------------------------------------------------------------------
    
    // -------------------------------------------------------------------------
    // Handle confirmation when deleting listing
    // -------------------------------------------------------------------------
    $('.deleteListingLink').click(function() { 
        $.blockUI({ title: 'Are you sure you want to delete this listing?', message: $('#question'), css: { width: '400px', height: '200px' } });
        redirectHref = $(this).attr('href');
        return false;
    }); 

    $('#yes').click(function() {
        window.location.href = redirectHref;
        $.unblockUI(); 
    }); 

    $('#no').click(function() { 
        $.unblockUI(); 
        return false; 
    });
    // -------------------------------------------------------------------------
    
    
    $("#feedbackForm").dialog({
    			autoOpen: false,
    			height: 300,
    			width: 350,
    			modal: true,
    			buttons: {
    				'Create an account': function() {
    					var bValid = true;
    					allFields.removeClass('ui-state-error');
    
    					bValid = bValid && checkLength(name,"username",3,16);
    					bValid = bValid && checkLength(email,"email",6,80);
    					bValid = bValid && checkLength(password,"password",5,16);
    
    					bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
    					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
    					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
    					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
    					
    					if (bValid) {
    						$('#users tbody').append('<tr>' +
    							'<td>' + name.val() + '</td>' + 
    							'<td>' + email.val() + '</td>' + 
    							'<td>' + password.val() + '</td>' +
    							'</tr>'); 
    						$(this).dialog('close');
    					}
    				},
    				Cancel: function() {
    					$(this).dialog('close');
    				}
    			},
    			close: function() {
    				allFields.val('').removeClass('ui-state-error');
    			}
    		});
    		
    		$('#reviewThisItem').click(function() {
    				$('#feedbackForm').dialog('open');
    			});

}); 

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

$.extend({ 
    urlDecode: function(url) {
    // fixed -- + char decodes to space char
    var o = url;
    var binVal, t, b;
    var r = /(%[^%]{2}|\+)/;
    while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
    if (m[1] == '+') {
    t = ' ';
    } else {
    b = parseInt(m[1].substr(1), 16);
    t = String.fromCharCode(b);
    }
    o = o.replace(m[1], t);
    }
    return o;
    }
});



