
function init()
{
clickrecall(document.getElementById('story'),'For example: What\'s going on, why this photo is special, etc.');

upc_photographerval = readCookie('upc_photographer');
upc_addressval = readCookie('upc_address');
upc_cityval = readCookie('upc_city');
upc_stateval = readCookie('upc_state');
upc_zipval = readCookie('upc_zip');
upc_phoneval = readCookie('upc_phone');
upc_emailval = readCookie('upc_email');


(upc_photographerval == null ) ? photographerval = "" : photographerval = upc_photographerval;
( upc_addressval == null ) ? addressval  = "" : addressval = upc_addressval ;
( upc_cityval == null ) ? cityval  = "" : cityval = upc_cityval ;
( upc_stateval == null ) ? stateval  = "" : stateval = upc_stateval ;
( upc_zipval == null ) ? zipval  = "" : zipval = upc_zipval ;
(upc_phoneval  == null ) ? phoneval  = "" : phoneval  = upc_phoneval ;
(upc_emailval == null ) ? emailval = "" : emailval = upc_emailval;


document.emailform.photographer_name.value=photographerval;
document.emailform.address.value=addressval;
document.emailform.city.value=cityval;
document.emailform.state.options[document.emailform.state.selectedIndex].value=stateval;
document.emailform.zip.value=zipval;
document.emailform.phone.value=phoneval;
document.emailform.email.value=emailval;

}


function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

/*
// Popup: when using this function on multiple links across a site its best to define a 'type' within your initiate function, if you are defining a popup on a single page you can pass in the options you want as arguments.
*/

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function Popup(args){
	// URL is the only required field. we cannot continue without it.
	if (args.url) {	var url = args.url;} else { alert('url is missing'); return; };
	
	if (args.type) { var type = args.type;};
				
	// option = (if it was passed in as an argument)? use that value : [IF NOT] use this default value;
	var directories = (args.directories)? args.directories : directories = "no";
	var location = (args.location)? args.location : location = "no";
	var menubar = (args.menubar)? args.menubar : menubar = "no";
	var resizable = (args.resizable)? args.resizable : resizable = "no";
	var scrollbars = (args.scrollbars)? args.scrollbars : scrollbars = "auto";
	var status = (args.status)? args.status : status = "yes";
	var toolbar = (args.toolbar)? args.toolbar : toolbar = "no";
	var top = (args.top)? args.top : top = "50";
	var left = (args.left)? args.left : left = "50";
	var width = (args.width)? args.width : width = "250";
	var height = (args.height)? args.height : height = "400";
	var winName = (args.winName)? args.winName : winName = "popup";
	
	/* DEFINE POPUP TYPES */
	if(type == "_blank"){	
		//created to imitate the target='_blank' behavior in html
		var randomNumber = Math.floor(Math.random()*1000);
		var winName="blank" + randomNumber; width=f_clientWidth(); height=f_clientWidth(); 
		scrollbars="yes"; menubar="yes"; toolbar="yes"; directories="yes"; 
		location="yes"; top="0"; left="0";
	} 
	
	if(type == "_pop"){	
		//created to imitate the target='_blank' behavior in html
		var randomNumber = Math.floor(Math.random()*1000);
		var winName="blank" + randomNumber;  
		scrollbars="yes"; menubar="yes"; toolbar="yes"; directories="yes"; 
		location="yes"; top="0"; left="0";
	} 
	
	if(type == "copyright"){
		height = "500"; width = "70"; scrollbars = "yes";
	}

	/* build our complete window options statement */
	windowOptions = "width=" + width + ", height=" + height + ", directories='" + directories + "', location='" + location + "', menubar='" + menubar + "', resizable='" + resizable + "', scrollbars='" + scrollbars + "', toolbar='" + toolbar + "', status='" + status + "', toolbar='" + toolbar + "', top='" + top + "', left='" + left + "'";

	var newWindow = window.open(url,winName,windowOptions);
	
	if (newWindow == null){
		//alert('A popup containing important information was blocked by your browser. Please enable popups for this site in order to view this information.');
	} else { 
		if (window.focus && newWindow) { newWindow.focus() }
	}
}
/* 
// End Popup:
*/

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
thisfield.style.fontStyle="normal";
thisfield.style.color="#000000";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
thisfield.style.fontStyle="italic";
thisfield.style.color="#666666";
}
}

function Validator(frmname)
{
  this.formobj=document.forms[frmname];
	if(!this.formobj)
	{
	  alert("BUG: couldnot get Form object "+frmname);
		return;
	}
	if(this.formobj.onsubmit)
	{
	 this.formobj.old_onsubmit = this.formobj.onsubmit;
	 this.formobj.onsubmit=null;
	}
	else
	{
	 this.formobj.old_onsubmit = null;
	}
	this.formobj.onsubmit=form_submit_handler;
	this.addValidation = add_validation;
	this.setAddnlValidationFunction=set_addnl_vfunction;
	this.clearAllValidations = clear_all_validations;
	this.addCookies = create_cookies;
}
function set_addnl_vfunction(functionname)
{
  this.formobj.addnlvalidation = functionname;
}
function clear_all_validations()
{
	for(var itr=0;itr < this.formobj.elements.length;itr++)
	{
		this.formobj.elements[itr].validationset = null;
	}
}
function form_submit_handler()
{
	create_cookies();
	
	for(var itr=0;itr < this.elements.length;itr++)
	{
		if(this.elements[itr].validationset &&
	   !this.elements[itr].validationset.validate())
		{
		  return false;
		}
	}
	if(this.addnlvalidation)
	{
	  str =" var ret = "+this.addnlvalidation+"()";
	  eval(str);
    if(!ret) return ret;
	}
	return true;
}
function add_validation(itemname,descriptor,errstr)
{
  if(!this.formobj)
	{
	  alert("BUG: the form object is not set properly");
		return;
	}//if
	var itemobj = this.formobj[itemname];
  if(!itemobj)
	{
	  alert("BUG: Couldnot get the input object named: "+itemname);
		return;
	}
	if(!itemobj.validationset)
	{
	  itemobj.validationset = new ValidationSet(itemobj);
	}
  itemobj.validationset.add(descriptor,errstr);
}
function ValidationDesc(inputitem,desc,error)
{
  this.desc=desc;
	this.error=error;
	this.itemobj = inputitem;
	this.validate=vdesc_validate;
}
function vdesc_validate()
{
 if(!V2validateData(this.desc,this.itemobj,this.error))
 {
    this.itemobj.focus();
		return false;
 }
 return true;
}
function ValidationSet(inputitem)
{
    this.vSet=new Array();
	this.add= add_validationdesc;
	this.validate= vset_validate;
	this.itemobj = inputitem;
}
function add_validationdesc(desc,error)
{
  this.vSet[this.vSet.length]= 
	  new ValidationDesc(this.itemobj,desc,error);
}
function vset_validate()
{
   for(var itr=0;itr<this.vSet.length;itr++)
	 {
	   if(!this.vSet[itr].validate())
		 {
		   return false;
		 }
	 }
	 return true;
}
function validateEmailv2(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}
function V2validateData(strValidateStr,objValue,strError) 
{ 
    var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 
    switch(command) 
    { 
        case "req": 
        case "required": 
         { 
         objString = trim(objValue.value);
           if(eval(objString.length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              alert(strError); 
              return false; 
           }//if 
           break;             
         }//case required 
         
           case "checkbox": 
         { 
           if(eval(objValue.checked) == false) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              alert(strError); 
              return false; 
           }//if 
           break;             
         }//case checkbox
         
         
           case "default": 
         { 
           if(objValue.value == "For example: What\'s going on, why this photo is special, etc.") 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              alert(strError); 
              return false; 
           }//if 
           break;             
         }//case required 
         
        
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//if 
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : " + cmdvalue + " characters minimum  "; 
               }//if               
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false;                 
             }//if 
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alpha-numeric characters allowed "; 
                }//if 
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
        case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only digits allowed "; 
                }//if               
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break;               
           }//numeric 
        case "alphabetic": 
        case "alpha": 
           { 
              
              
              var charpos = objValue.value.search("[^A-Za-z \s]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alpha 
		case "alnumhyphen":
			{
              var charpos = objValue.value.search("[^[A-Za-z0-9._\\-,: \\s]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- ,:,and _"; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 			
			break;
			}
        case "email": 
          { 
               if(!validateEmailv2(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.name+": Enter a valid Email address "; 
                 }//if                                               
                 alert(strError); 
                 return false; 
               }//if 
           break; 
          }//case email 
        case "lt": 
        case "lessthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
            if(eval(objValue.value) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than "+ cmdvalue; 
              }//if               
              alert(strError); 
              return false;                 
             }//if             
            break; 
         }//case lessthan 
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
              return false; 
            }//if 
             if(eval(objValue.value) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than "+ cmdvalue; 
               }//if               
               alert(strError); 
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
        case "regexp": 
         { 
		 	if(objValue.value.length > 0)
			{
	            if(!objValue.value.match(cmdvalue)) 
	            { 
	              if(!strError || strError.length ==0) 
	              { 
	                strError = objValue.name+": Invalid characters found "; 
	              }//if                                                               
	              alert(strError); 
	              return false;                   
	            }//if 
			}
           break; 
         }//case regexp 
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item"); 
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Please Select one option "; 
              }//if                                                               
              alert(strError); 
              return false;                                   
             } 
             break; 
         }//case dontselect 
    }//switch 
    return true; 
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function create_cookies()
{
  createCookie('upc_photographer',document.emailform.photographer_name.value);
    createCookie('upc_address',document.emailform.address.value);
      createCookie('upc_city',document.emailform.city.value);
        createCookie('upc_state',document.emailform.state.options[document.emailform.state.selectedIndex].value);
               createCookie('upc_zip',document.emailform.zip.value);
      createCookie('upc_phone',document.emailform.phone.value);
        createCookie('upc_email',document.emailform.email.value);
              //      createCookie('upc_photo_location',document.emailform.photo_location.value);
           //  createCookie('upc_member_name',document.emailform.member_name.value);
                 //createCookie('upc_member_number',document.emailform.member_number.value);
                 //    createCookie('upc_member_year',document.emailform.member_year.value);
          
}

