// performs an AJAX request to a specified URL with specified parameters
function ajaxFunction( postURL, params ) {
    var ajaxRequest; 
    try {
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert( "An error has occurred." );
                return false;
            }
        }
    }
    ajaxRequest.onreadystatechange = function() {
        if( ajaxRequest.readyState == 4 && ajaxRequest.status == 200 ) {
            var response = ajaxRequest.status;
        }
    }
    ajaxRequest.open( "POST", postURL, false );
    ajaxRequest.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
    ajaxRequest.send( params ); 
}

// disable the enter key
function cancel_Enter(e) {
    if (!e) var e = window.event; //for IE
    var code;
    if(e.keyCode) code = e.keyCode; //for IE
    if(e.which) code = e.which; //for other browsers
    if (code == 13) return false;
    else return true;
}
    
// change genre
function changeGenre() {
    var genre_url = document.getElementById( 'changeGenre' ).value;
    if( genre_url != '' )
    window.location = "/books/" + genre_url + "/";
    else
    window.location = "/books/";
}

// activates all checkboxes for a form field
function checkAll( field, status ) {
    for( i = 0; i < field.length; i++ )
        field[i].checked = status;
}


// check photo extension
function checkImageType( field )
{   
    var ext;
    if( field.value.length > 0 ) {
        ext = field.value.substring( field.value.length - 4 );
        ext = ext.toLowerCase();
        if( ext != '.jpg' ) {
            alert( "You may only upload a JPG image!" );
            return false;
        }
    }
	return true;
}

function clearSearchBox() {
    document.getElementById( 'searchbox' ).value = '';
}

function confirmDelete() {
    var agree = confirm( "This delete action cannot be undone. Do you wish to continue?" );
    if( agree ) return true ;
    else return false ;
}


// friend request is accepted
function friendAccept( user_id ) {
    ajaxFunction( "/inc/functions_penpals.php", "action=accept&user_id=" + user_id );
    window.location.reload( true );
}

// friend request is rejected
function friendReject( user_id ) {
    ajaxFunction( "/inc/functions_penpals.php", "action=reject&user_id=" + user_id );
    window.location.reload( true );
}

// friend is removed
function friendRemove( user_id ) {
    ajaxFunction( "/inc/functions_penpals.php", "action=remove&user_id=" + user_id );
    window.location.reload( true );
}

// friend is requested
function friendRequest( user_id ) {
    ajaxFunction( "/inc/functions_penpals.php", "action=request&user_id=" + user_id );
    document.getElementById("req_" + user_id ).style.display = 'none';
}

// create a popup window
function popUp( url, w, h ) {
    window.open( url, "popUp", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + w + ",height=" + h );
}

// textarea limiter
function textLimiter( field, maxChars ) {
    if( typeof maxchars == 'undefined' )
        maxChars = 75000;
    if( field.value.length > maxChars )
        field.value = field.value.substring( 0, maxChars );
}

// remove extra spaces before and after string 
function trim( str ) {
    return str.replace(/^\s+|\s+$/g,"");
}

// global variables
var penpalreqmsg = " was sent a pen pal request.";


/* uncleaned functions */






function confirmSubmit() {
    var agree=confirm("Are you sure you wish to continue?");
    if (agree)
        return true ;
    else
        return false ;
}

function confirmAuthor() {
    var agree=confirm("You are about to register as an author. This implies that you have published, or are about to publish, books or poetry. If this is correct, click OK to continue.");
    if (agree)
        return true ;
    else
        return false ;
}

function confirmReader() {
    var agree=confirm("You are about to register as a reader. If you have not published any books or poetry, but enjoy reading, then click OK to continue.");
    if (agree)
        return true ;
    else
        return false ;
}