/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
	var exdate=new Date();
	exdate.setTime(exdate.getTime()+(expires*24*3600*1000))
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + exdate : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//account validate
function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}
function isProper(string) {
    if (string.search(/^\w+( \w+)?$/) != -1) // check for illegal characters
        return true;
    else
        return false;
}       
function isEntered(string) {
	if (string != "") // just check that it's not empty
        return true;
    else
        return false;
}        
function checkForm(form,page) {
var listerrors = "";
	if (page == "reg_add"){
	    if (isEntered(form.fname.value) == false) {
	        listerrors += "Please enter your first name.\n";
	    }
	    if (isEntered(form.lname.value) == false) {
	        listerrors += "Please enter your last name.\n";
	    }
		if (form.birthday_bypass.checked != true){
		    if ((form.bday_month.value == "00") || (form.bday_day.value == "00")) {
		        listerrors += "Please tell me your birthday.\n";
		    }
		}
	    if (isEmail(form.emailaddress.value) == false) {
	        listerrors += "Please enter a valid email address.\n";
	    }
	    if (isProper(form.username.value) == false) {
	        listerrors += "Please enter a valid username.\n";
	    }
	    if (isProper(form.password.value) == false) {
	        listerrors += "Please choose a valid password.\n";
	    }
		if (form.password.value != form.password2.value){
			listerrors += "Passwords do not match.\n";
		}
	}
	else if (page == "reg_edit"){
	    if (isEntered(form.fname.value) == false) {
	        listerrors += "Please enter your first name.\n";
	    }
	    if (isEntered(form.lname.value) == false) {
	        listerrors += "Please enter your last name.\n";
	    }
		if (form.birthday_bypass.checked != true){
		    if ((form.bday_month.value == "00") || (form.bday_day.value == "00")) {
		        listerrors += "Please tell me your birthday.\n";
		    }
		}
	    if (isEmail(form.emailaddress.value) == false) {
	        listerrors += "Please enter a valid email address.\n";
	    }
	    if (isProper(form.username.value) == false) {
	        listerrors += "Please enter a valid username.\n";
	    }
		if (form.password.value != form.password2.value){
			listerrors += "Passwords do not match.\n";
		}
	}
	else if (page == "gift"){
		rating_button = -1;
		for (i=0; i<form.rating.length; i++) {
			if (form.rating[i].checked) {
				rating_button = i;
			}
		}
		if (rating_button == -1) {
	        listerrors += "Please tell everyone how much you want it!\n";
		}

		if (isEntered(form.description.value) == false) {
	        listerrors += "Please enter the gift description/name.\n";
	    }
	}
	else if (page == "bought_nonlist"){
		if (isEntered(form.bought_nonlist.value) == false) {
	        listerrors += "Please enter the description of the gift you bought.\n";
	    }
	}
	else if (page == "registry"){
		if (isEntered(form.name.value) == false) {
	        listerrors += "Please enter a name for the registry.\n";
	    }
		if (form.event_date_bypass.checked != true){
		    if ((form.event_month.value == "00") || (form.event_day.value == "00") || (form.event_year.value == "0000")) {
		        listerrors += "Please enter a valid date, or check 'NO EVENT DATE'.\n";
		    }
		}
	}
		
    if (listerrors != "")	{
		alert("Note: fields may only contain characters A-Z and 0-9\n\n"+listerrors);
		return false;
	} else	{
		return true;
	}
}
//end validate

//checkall
function checkAll(formId, cName, check ) {
    for (i=0,n=formId.elements.length;i<n;i++)
        if (formId.elements[i].className.indexOf(cName) !=-1)
            formId.elements[i].checked = check;
}
//end checkall

//pop
function smallPop(url){
	win=window.open(url,"smallpop","width=400,height=250,resizable=no,status=no,scrollbars=yes,location=no,menubar=no,toolbar=no,left=200,top=200"); 
}

function pop( url, w, h, msr, windowName )     {
	win=window.open(url,windowName,"height=" + h + ",width=" + w + ",resizable=" + msr + ",status=no,scrollbars=" + msr + ",location=no,menubar=" + msr + ",toolbar=no"); 
}
//end pop

//bookmark
function bookmark(){
  if (!document.all) {
    alert('Please hit CTRL-D to bookmark this page');
  }
  else external.AddFavorite('http://www.myWishlister.com','my Wishlister ');
  return false;
}
//end bookmark

//confirm bulk delete
function confirmBulkDelete(){
	var confirmIt = confirm("Are you sure you want to delete the selected gifts on your wishlist?");
	if (confirmIt){
		return true;
	}
	else{
		alert("No gifts were deleted");
		return false;
	}
}
//end bulk

function popAlert(message){
	alert(message);
}