/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "="
	var start = document.cookie.indexOf(prefix)

	if (start==-1) {
		return null;
	}

	var end = document.cookie.indexOf(";", start+prefix.length)
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end)
	return unescape(value);
}

/* This function is used to delete cookies */
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";
  }
}


/**
 * Search
 * @param form
 * @param message
 */
function validateSearch(form, message) {
    if (form.q.value == "") {
        alert(message);
        form.q.focus();
        return false;
    }
    return true;
}


/**
 * Comments
 * @param theForm
 */
function fixURL(theForm) {
    if (theForm.url.value != "" &&
        theForm.url.value.indexOf("http://") == -1) { //prepend http://
        theForm.url.value = "http://"+theForm.url.value;
    }
    saveUserInformation(theForm);
}


/**
 * Comments
 * @param theForm
 */
function saveUserInformation(theForm) {
    if (theForm.rememberInfo.checked) {
        rememberUser(theForm);
    } else {
        forgetUser(theForm);
    }
}


/**
 * Comments
 * @param theForm
 * @param warningMessage
 */
function validateComments(theForm, warningMessage) {
    if (theForm.content.value == "") {
        alert(warningMessage);
        theForm.content.focus();
        return false;
    }
}

function commentsRememberUser(){
    var author = getCookie("commentAuthor");
    var email = getCookie("commentEmail");
    var url = getCookie("commentUrl");
    // check each field - IE will render "null"
    if (author) {
        document.forms['commentForm'].name.value = author;
    }
    if (email) {
        document.forms['commentForm'].email.value = email;
    }
    if (url) {
        document.forms['commentForm'].url.value = url;
    }

    if (author || email || url) {
        document.forms['commentForm'].rememberInfo.checked = true;
    }
}