var r_url = ''; // rel path from root

// open url in parent window and close popup
function changeWin(url){
    //window.opener.location = url;
    nw=window.open(url, 'new_window');
    parent.close();
}

// Show popup "members enter here"
function popup_window(link) {
var hw = '';

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_popup.php?link='+link, '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width=423,height=550');
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// Show popup for gallery
function gallery_popup_window(link) {
var hw = '';

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_popup.php?gal='+link, '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width=308,height=510');
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// open window
function open_window(link, width, height) {
var hw = '';

    if (width <= 0) width = 200;
    if (height <= 0) height = 300;

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_image.php?image='+link, '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width='+width+',height='+height);
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

/**
 * Open page
 */
function open_page(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/**
 * Displays an error message if an element of a form hasn't been completed and
 * should be
 *
 * @param   object   the form
 * @param   string   the name of the form field to put the focus on
 *
 * @return  boolean  whether the form field is empty or not
 */
function is_field_empty(theForm, theFieldName)
{
    var isEmpty  = 1;
    var theField = theForm.elements[theFieldName];
    // Whether the replace function (js1.2) is supported or not
    var isRegExp = (typeof(theField.value.replace) != 'undefined');

    if (!isRegExp) {
        isEmpty      = (theField.value == '') ? 1 : 0;
    } else {
        var space_re = new RegExp('\\s+');
        isEmpty      = (theField.value.replace(space_re, '') == '') ? 1 : 0;
    }
    if (isEmpty) {
//        theForm.reset();
        theField.select();
        alert("All the fields marked with red are required to submit the form.");
        theField.focus();
        return false;
    }

    return true;
}

function is_certify_checked(theForm, theFieldName)
{
    var theField = theForm.elements[theFieldName];
    var isEmpty = (theField.checked == true) ? 0 : 1;

    if (isEmpty) {
        theField.select();
        alert("Please read the terms and conditions and check the box before you submit the form.");
        theField.focus();
        return false;
    }

    return true;
}

/**
 * Ensures a value submitted in a form is numeric and is in a range
 *
 * @param   object   the form
 * @param   string   the name of the form field to check
 * @param   integer  the minimum authorized value
 * @param   integer  the maximum authorized value
 *
 * @return  boolean  whether a valid number has been submitted or not
 */
function is_element_in_range(theForm, theFieldName, min, max)
{
    var theField         = theForm.elements[theFieldName];
    var val              = parseInt(theField.value);
    var text;
    
    if (typeof(min) == 'undefined') {
        min = 0;
    }
    if (typeof(max) == 'undefined') {
        max = Number.MAX_VALUE;
    }

    // It's not a number
    if (isNaN(val)) {
        theField.select();
        alert("Numeric value have to be there!");
        theField.focus();
        return false;
    }
    // It's a number but it is not between min and max
    else if (val < min || val > max) {
        theField.select();
        if (max!=Number.MAX_VALUE)
         text='Value have to be from ' + min + ' to ' + max;
        else
         text='Value have to be more than ' + min;
        alert(text);
        theField.focus();
        return false;
    }
    // It's a valid number
    else {
        theField.value = val;
    }

    return true;
} // end of the 'checkFormElementInRange()' function
