var newC = "";		//used to hold the email address after it is modified from AT to @
var logIn = "";		//used to indicate which login verbage should be used

function createCookie( cookieName, 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 = "";
  }//end if/else
	
  document.cookie = cookieName + "=" + value + expires + "; path=/";
  
}//end function

function readCookie( cookieName ) {

  var nameEQ = cookieName + "=";
  var cookieSplit = document.cookie.split(';');

  //loop through the cookies for this site looking for matching cookie name
  for(var i=0;i < cookieSplit.length;i++) {
    var c = cookieSplit[i];

    //remove any unnecessary spaces at start of cookie
    while (c.charAt(0) == ' ') {
      c = c.substring( 1, c.length );
    }


    if( c.substring( 0, 4 ) == nameEQ ) {
      //a match was found - returns email with AT replaced with @
      //var newC = c.replace( /AT/, "@" );
      newC = c.replace( /AT/, "@" );
      newC = newC.replace( /QUOTE/, "\'" );
      newC = newC.substring( 4, newC.length );

      //if cookie is not found, show account login text
      if (newC == "") {
	logIn = '<a href=\"/loginForm.php\">Account Login</a>';
      } else if (newC != "") {
	logIn = newC + ' is logged in | <a href="/registration/userAccount.php">My Account</a> | <a href=\"log out\" onClick=\"javascript:eraseCookie(\'uid\');location.reload(true); return false;\">Log Out</a> |' ;
      }//end if/else

      //return newC.substring( 4, newC.length );
      return;
    }

  }//end for  
  
  //no cookies found for this site
  logIn = '<a href=\"/loginForm.php\">Account Login |</a>';
  return;
  
}//end function

function eraseCookie( cookieName ) {

  //erases the cookies by setting the days to -1
  createCookie( cookieName, "", -1 );
  
}//end function

//call the function to read the cookie
readCookie( "uid" );


/*-- function to trigger search  --*/
function googleIt() {
	document.googleSearch.submit();
}



/*-- format the search terms --*/
function checkAll( form, value ) {

  //get the value from the text field on the form
  //textSearch = form.q.value;
  //pass in the value from the text box since it may not always have the same name
  textSearch = value;
  
  //first remove any existing commas to start with a clean slate
  if( textSearch.indexOf( "," ) > -1 ) {
    textSearch = textSearch.replace( /,/g, " " );
  }//end if

  //replace any double spaces that exist
  textSearch = textSearch.replace( /  /g, " " );

  //put commas between all words, including exact phrases
//textSearch = textSearch.replace( / /g, ", " );

  //check for a quote in the string
  if( textSearch.indexOf( "\"" ) > -1 ) {

    //position in the string of the first and second quotes
    startQuote = textSearch.indexOf( "\"" );
    endQuote = textSearch.indexOf( "\"", startQuote+1 );
/*
    for( startQuote; startQuote<textSearch.length; ) {

      //if a comma is found between the start and end quote, replace it
      if( textSearch.indexOf( ",", startQuote ) > startQuote && textSearch.indexOf( ",", startQuote ) < endQuote ) {

        //break the string apart so the replace can be done.  it will then be concatenated back together.
        textSearchStart = textSearch.substring( 0, startQuote );
        textSearchSubString = textSearch.substring( startQuote, endQuote );
        textSearchEnd = textSearch.substring( endQuote );

        textSearchSubString = textSearchSubString.replace( /,/g, " " );

        textSearch = textSearchStart + textSearchSubString + textSearchEnd;

        //reset the startQuote position
        startQuote = textSearch.indexOf( "\"", endQuote+1 );

        //check to see if endQuote needs to be reset
        if( startQuote >= endQuote && textSearch.indexOf( ",", startQuote+1 ) != -1) {

          endQuote = textSearch.indexOf( "\"", startQuote+1 );

        } else if( endQuote < textSearch.indexOf( ",", startQuote ) ) {

	  if( endQuote == -1 ) {
	    break;
	  } else {
	
            //reset the start and end quotes because the current quoted phrase is only one word
            startQuote = textSearch.indexOf( ",", startQuote )+1;
  	    endQuote = textSearch.indexOf( "\"", startQuote+2 );
  	  }//end if

        }//end if
      
      }//end if

    }//end for loop
*/
    //replace any double spaces that exist
    textSearch = textSearch.replace( /  /g, " " );

  }//end if

  //format the double quotes with the backslash before each quote like the checkboxes use
//  textSearch = textSearch.replace( /"/g, "\\\"" );

  //store the concatenated string back in the field so it can be passed to the next page
  form.q.value = textSearch;
  form.q2.value = textSearch;

}//end function

