var r_url = 'http://www.hegre-art.com'; //siteurl

// open url in parent window and close popup
function changeWin(url){
    window.opener.location = url;
    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.bob?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;
}

// open window and show gallery image there
function show_gimage(sid, width, height, with_area, with_join) {
var hw = '';

    height += (with_join ? 0 : 25);
    with_join = (with_join ? '&with_join=1' : '');

    with_area = (with_area ? '&with_area=1' : '');

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_gimage.bob?sid='+sid+with_area+with_join, '','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 window and show film image there
function show_fimage(fid, width, height, free_tour_film) {
var hw = '';

    height += 25;
    free_tour_film = (free_tour_film ? '&ftfilm=1' : '');

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_fimage.bob?fid='+fid+free_tour_film, '','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 window and show image there
function show_image(image, width, height) {
var hw = '';

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_image.bob?image='+image+'&width='+width+'&height='+height, '','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 window and show image there (for freetour only, and join link below)
function show_freetour_image(image, width, height) {
var hw = '';

    //windows_height = height + 25;
    windows_height = (height + 25 > 768 ? 768 : height + 25);
    windows_width = (width > 1024 ? 1024 : width);
    
    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_image.bob?image='+image+'&width='+width+'&height='+height+'&with_join=1', '','toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=yes,width='+windows_width+',height='+windows_height);
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// open window and show image there (for freetour only, and join link below)
function show_freetour_sample_image(image, width, height) {
var hw = '';

    //windows_height = height + 25;
    windows_height = (height + 25 > 768 ? 768 : height + 25);
    windows_width = (width > 1024 ? 1024 : width);
    
    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_image.bob?image='+image+'&width='+width+'&height='+height+'&with_join=1', '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width='+windows_width+',height='+windows_height);
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// open window and show image there (for freetour only, and join link below)
function show_snapshot_image(scode, with_join) {
var hw = '';

    windows_width = 730;
    windows_height = 480;

    with_join = (with_join ? '&with_join=1' : '');
    
    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_snapshot_image.bob?scode='+scode+with_join, '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width='+windows_width+',height='+windows_height);
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// open window and show image there
function show_index_popup() {
var hw = '';

    hw=window.open(r_url+'/show_index_popup.bob', '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=yes,width=350,height=451');

    return false;
}

// open window and show prints image there
function show_prints_image(num, width, height) {
var hw = '';

    width = 600;
    height = 730;
    
    //alert(width);

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_prints_image.bob?num='+num+'&width='+width+'&height='+height, '','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 window and show prints image there
function show_commission_image(num, file_num) {
var hw = '';

    width = 620;
    height = 770;

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/show_commission_image.bob?num='+num+'&file_num='+file_num, '','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=auto,resizable=yes,width='+width+',height='+height);
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// open window
function open_user_comments(lang, page_id) {
var hw = '';

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/'+lang+'/user_comments/'+page_id+'/', '','toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=auto,resizable=yes,width=450,height=650');
    }

    if (!hw.opener) {
       hw.opener = self;
    }

    if (window.focus) {
        hw.focus();
    }

    return false;
}

// open window
function tellafriend_window(link) {
var hw = '';

    if (!hw.closed && hw.location) {
        hw.focus();
    } else {
        hw=window.open(r_url+'/tell_a_friend.bob?url='+link, '','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=no,resizable=yes,width=400,height=550');
    }

    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
