// Prevent stuff from opening in uncontrolled windows BEGIN
/* redesign
  if( location.href.indexOf("details.php") != -1 ) {
    if(window.name != "detailwindow" && window.name != "bigwindow" )
      location.href = url_home;
  }
redesign */
// Prevent stuff from opening in uncontrolled windows END

function cbeSetCookie(name, value, expire, path) {
  document.cookie = name + "=" + escape(value) + ((!expire) ? "" : ("; expires=" + expire.toGMTString())) + "; path=/";
}
function cbeGetCookie(name) {
  var value=null, search=name+"=";
  if (document.cookie.length > 0) {
    var offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      var end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      value = unescape(document.cookie.substring(offset, end));
    }
  }
  return value;
}
function xGetElementById(e) {
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}


// Gallery utility functions BEGIN
function centerWindow(windowWidth) {
  x=(screen.availWidth-windowWidth)/2;
  moveTo(x,0);
}
var resizePos;
function resizeWindow(xOffset,yOffset){
  if( resizePos == null )
    return;
  if( window.name == "detailwindow" ){
    xOffset = 25;
	yOffset = 33;
  }else if(window.name == "postcardwindow"){
    xOffset = 45;
	yOffset = 40;
  }
  newWidth = resizePos.pageX()+xOffset;
  newHeight = resizePos.pageY()+yOffset;
  if( newWidth > screen.width )
    newWidth = screen.width;
  if( newHeight > screen.height )
    newHeight = screen.height;
  if( window.name == "postcardwindow" )
    centerWindow(newWidth);
  if( window.name == "detailwindow" ){
    if( newWidth < 295 )
	  newWidth = 295;
  }
  window.resizeTo(newWidth,newHeight);
}

function cookiesDisabled() {
  var checkValue = Math.floor(Math.random()*1000000);
  var today = new Date();
  var expires = new Date();
  expires.setTime(today.getTime() + 1000*60*60*24);
  cbeSetCookie("CookieCheck", checkValue.toString(), expires, "/");
  var cookie = cbeGetCookie("CookieCheck");
  if(cookie != null && cookie == checkValue.toString()){
    // Delete the cookie in case "www" is added or deleted 
	// from the URL the next time this page is loaded
    cbeSetCookie("CookieCheck", "", 0, "/");
    return false;
  }else{
    return true;
  }
}

function VividViewsWindowOnload() {
  if( xGetElementById("resizeMarker") ) {
    if( location.href.indexOf("details.php") != -1 && cookiesDisabled() ) {
      alert("You must enable cookies in your browser to view the images.");
	  window.opener.location.href = url_cookies;
	  window.close();
    }
    resizePos = xGetElementById("resizeMarker").cbe;
    if( window.name != "" && location.href.indexOf("postcard_id=") == -1 ) // Don't resize picked up postcards
	  resizeWindow();
  }
  if( window.name == "" ) {
    window.name = "mainwindow";
  }
}

function aboutBitPassWindow(url,windowName) {
x=(screen.availWidth-476)/2;
  aboutWin = window.open(url,windowName,'toolbar=0,top=0,left='+x+',scrollbars=0,resizable=1,width=476,height=495');
  aboutWin.focus();
}

function openNewWindow(url, windowName) {
  x=(screen.availWidth-476)/2;
  newWin = window.open(url,windowName,'toolbar=no,top=0,left='+x+',scrollbars,resizable=yes,width=476,height='+(screen.availHeight-45));
  newWin.focus();
}

var bigwindow;
function openBigWindow(url,width,height) { 
  x=((screen.availWidth-width)/2)-18;
  if(x<0) x=0;
  y=((screen.availHeight-height)/2)-15;
  if(y<0) y=0;
y=55; // y=110;
  scrollbars = "no";
  resizable = "no";
  if(width>screen.availWidth) {
    width = screen.availWidth;
    x = 0;
    scrollbars = "yes";
    resizable = "yes";
  }
  if(height>screen.availHeight) {
    height = screen.availHeight;
    y = 0;
    scrollbars = "yes";
    resizable = "yes";
  }
  bigwindow = window.open('','bigwindow')
  bigwindow.close();
  bigwindow = window.open(url,'bigwindow','status=no,toolbar=no,top='+y+',left='+x+',screenX=0,screenY=0,scrollbars='+scrollbars+',resizable='+resizable+',width='+width+',height='+height);
  if( bigwindow == null ) {
	  alert("Unable to open the \"Bigger View\" window. Please disable any pop-up blocker you may be using and try again.");
	  return;
  }
  bigwindow.focus();
}

function openInOpener(url) {
  if( window.opener != null ) {
    window.opener.location=url;
    window.opener.focus();
  }
  else
    top.location.href =url;
}

function opendetailwindow(url) {
  if( url == "" )
    return;
  if( cookiesDisabled() ) {
    alert("You must enable cookies in your browser to view the images.");
	top.location.href = url_cookies;
  } else {
    if( !getTermsOfUseCookie() )
	  top.location.href = url_termsOfUse;
    else {
      detailwindow = window.open(url,'detailwindow','toolbar=no,top=0,left=0,screenX=0,screenY=0,scrollbars=no,resizable=yes,width=300,height=400');
      detailwindow.focus();
	}
  }
}

function setFormFocus() {
  if( document.contactForm ) {
    if(document.contactForm.sender_name == "")
      document.contactForm.sender_name.focus();
    else if(document.contactForm.sender_email == "")
      document.contactForm.sender_email.focus();
    else
      document.contactForm.subject.focus();
  }
  else if( document.registration )
    document.registration.user_name.focus();
  else if( document.advanced_search )
    document.advanced_search.search_keywords.focus();
  else if( document.user_profile )
    document.user_profile.user_email.focus();
  else if( document.forgot_password )
    document.forgot_password.user_email.focus();
}

// Gallery utility functions END

