

var ua = navigator.userAgent.toLowerCase();
var safari = false;
if (ua.indexOf('safari/') != -1){
  safari = true;
}


function openWin(url) {
  window.open(url, "test", "toolbar=no,menubar=no,width=600,height=600,scrollbars=yes");
}


function detailWin(id) {
  window.open("enhance/detail.php?id="+id, "test", "toolbar=no,menubar=no,width=960,height=450,scrollbars=yes");
}


function getParameter(aP){var qS = new String(location.search.substring(1,location.search.length));var p = qS.split("&");var val = "";if(aP){for(i=0;i<p.length;i++){if(p[i].split( "=" )[0] == aP){val = p[i].split( "=" )[1];}}return val;}}


function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	


function setCookie( nam, val ) {
  document.cookie = nam + '=' + escape(val);
}


function validate_form( thisform ) {
  var errors = false;
  var fileerrors = false;
  var error_str = "";
  // check name
  if( document.forms[0].name.value == "" ) {
    error_str += "Name\n";
    errors = true;
  }
  // check email
  if( document.forms[0].email.value == "" ) {
    error_str += "Email\n";
    errors = true;
  }
  // add header
  if( errors > 0 ) error_str = "The following field(s) are required:\n" + error_str;
  // check jpg
  var attach1 = document.forms[0].attachment.value;
  var attach2 = document.forms[0].attachment2.value;
  if( attach1 && checkJPG( attach1 ) == false ) {
    fileerrors = true;
  }
  if( attach2 && checkJPG( attach2 ) == false ) {
    fileerrors = true;
  }
  if( fileerrors == true ) {
    if( errors > 0 ) error_str += "\n";
    error_str += "We can only accept JPG files with this form.\n";
    error_str += "If you have a different file type we will contact\n";
    error_str += "you via email once we receive your order\n";
    error_str += "to obtain your files.\n";
  }
  // check legal
  if( ( attach1 || attach2 ) && document.forms[0].own.checked == false ) {
    if( fileerrors == false ) {
      if( errors > 0 ) error_str += "\n";
      error_str += "You must certify that you own the copyright\n";
      error_str += "for the submitted photo(s)!\n";
      errors = true;
    }
  }
  // alert errors
  if( errors > 0 || fileerrors == true ) {
    alert( error_str );
    return false;
  } else {
    //setTimeout( "timeout()", 60*1000);
    return true;
  }
}


function uploadWarning() {
  var str = "";
  str += "Our servers cannot accept images larger than 2MB.\n";
  str += "Please make a note in the special instructions field\n";
  str += "if you have a bigger image we will contact you via\n";
  str += "email once we receive your order to obtain\n";
  str += "your files.\n\n";
  str += "See our Photo Submission Tips for the optimal\n";
  str += "image specifications.";
  alert( str );
}


function timeout() {
  alert( "upload timeout" );
  window.stop();
}


function checkJPG( path ) {
  var start = path.lastIndexOf('.')+1;
  var finish = path.length;
  if( start == -1 ) {
    return false;
  } else {
    var ext = path.substring( start, finish );
    ext = ext.toLowerCase();
    if( ext == "jpg" || ext == "jpeg" ) {
      return true;
    } else {
      return false;
    }
  }
}


function validate_contactus( thisform ) {
  var errors = false;
  var error_str = "";
  // check name
  if( document.forms[0].name.value == "" ) {
    error_str += "Name\n";
    errors = true;
  }
  // check email
  if( document.forms[0].email.value == "" ) {
    error_str += "Email\n";
    errors = true;
  }
  // check question
  if( document.forms[0].question.value == "" ) {
    error_str += "Question\n";
    errors = true;
  }
  // alert errors
  if( errors > 0 ) {
    alert( error_str );
    return false;
  } else {
    return true;
  }
}


// write order number to session cookie if passed on query string
var prev_order = getParameter("order");
if( prev_order ) {
  setCookie( "prev_order", prev_order );
} else {
  prev_order = getCookie( "prev_order" );
}


/*
if( location.protocol == "http:" ) {
  location.replace( "https://"+location.host+location.pathname+location.search )
}
*/


