//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright International Business Machines Corporation. 2003
//*     All rights reserved.
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*

//////////////////////////////////////////////////////////
// Checks whether a string contains a double byte character
// target = the string to be checked
//
// Return true if target contains a double byte char; false otherwise
//////////////////////////////////////////////////////////
function containsDoubleByte (target) {
     var str = new String(target);
     var oneByteMax = 0x007F;

     for (var i=0; i < str.length; i++){
        chr = str.charCodeAt(i);
        if (chr > oneByteMax) {return true;}
     }
     return false;
}

//////////////////////////////////////////////////////////
// A simple function to validate an email address
// It does not allow double byte characters
// strEmail = the email address string to be validated
//
// Return true if the email address is valid; false otherwise
//////////////////////////////////////////////////////////
function isValidEmail(strEmail){
	// check if email contains dbcs chars
	if (containsDoubleByte(strEmail)){
		return false;
	}
	
	if(strEmail.length == 0) {
		return true;
	} else if (strEmail.length < 5) {
             return false;
       	}else{
           	if (strEmail.indexOf(" ") > 0){
                      	return false;
               	}else{
                  	if (strEmail.indexOf("@") < 1) {
                            	return false;
                     	}else{
                           	if (strEmail.lastIndexOf(".") < (strEmail.indexOf("@") + 2)){
                                     	return false;
                                }else{
                                        if (strEmail.lastIndexOf(".") >= strEmail.length-2){
                                        	return false;
                                        }
                              	}
                       	}
              	}
       	}
      	return true;
}



//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string
// arg2 = the maximum number of bytes allowed in your input field
// Return false is this input string is larger then arg2
// Otherwise return true...
//////////////////////////////////////////////////////////
function isValidUTF8length(UTF16String, maxlength) {
    if (utf8StringByteLength(UTF16String) > maxlength) return false;
    else return true;
}

//////////////////////////////////////////////////////////
// This function will count the number of bytes
// represented in a UTF-8 string
//
// arg1 = the UTF-16 string you want a byte count of...
// Return the integer number of bytes represented in a UTF-8 string
//////////////////////////////////////////////////////////
function utf8StringByteLength(UTF16String) {
  if (UTF16String === null) return 0;
  var str = String(UTF16String);
  var oneByteMax = 0x007F;
  var twoByteMax = 0x07FF;
  var byteSize = str.length;

  for (i = 0; i < str.length; i++) {
    chr = str.charCodeAt(i);
    if (chr > oneByteMax) byteSize = byteSize + 1;
    if (chr > twoByteMax) byteSize = byteSize + 1;
  }  
  return byteSize;
}

//////////////////////////////////////////////////////////
// DOJO based Ajax calls for retrieving remote data 
// Example of use: dojo.query("#title p").forEach(loadFromQuery("url_for_remote_data_here.jsp"));
// var result = loadFromQuery("script.php");
// dojo.query("#title p").forEach(result);
// dojo.query("#section .subsection").forEach(result);
//////////////////////////////////////////////////////////
function loadFromQuery(url){
	var get = dojo.xhrGet({ url: url });
    return function(node){
     	get.addCallback(function(data){node.innerHTML = data;return data;});
    }
  }

function submitSearchForm() {	
	var form = document.forms['CatalogSearchForm'];
	var searchTerm = form.searchTerm.value;
	//alert('Calling New method! Search term is ' + searchTerm);
	if (searchTerm == 'Search scheels.com' || searchTerm == '') {
		alert('Please enter a search term before proceeding.');
		return false;
	} else {
		return true;
  	}
}	


function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block') {
   	// Set tickerDisplay cookie to false
   	  Set_Cookie( 'tickerDisplay', 'false', '', '/', '', '' );   	  
      e.style.display = 'none';
    } else {
    // Set tickerDisplay cookie to true
      Set_Cookie( 'tickerDisplay', 'true', '', '/', '', '' ); 
      e.style.display = 'block';
    }
}

function toogle_visibility_base_on_cookie(id) {
   var e = document.getElementById(id);
   var cookieDisplay = Get_Cookie( 'tickerDisplay' );
   if(cookieDisplay == 'true') {
   	// Set tickerDisplay cookie to true
   	 // Set_Cookie( 'tickerDisplay', 'true', '', '/', '', '' ); 
      e.style.display = 'block';   	 
    } else {
    // Set tickerDisplay cookie to false
      Set_Cookie( 'tickerDisplay', 'false', '', '/', '', '' );   	  
      e.style.display = 'none';
    }
}
function toogle_visibility_base_on_scheme(id,scheme) {
   var e = document.getElementById(id);
   var cookieDisplay = Get_Cookie( 'tickerDisplay' );
   if(scheme == 'http')
   {
   		if(cookieDisplay == 'true') {
	   	// Set tickerDisplay cookie to true
	   	 // Set_Cookie( 'tickerDisplay', 'true', '', '/', '', '' ); 
	      e.style.display = 'block';   	 
	    } else {
	    // Set tickerDisplay cookie to false
	      Set_Cookie( 'tickerDisplay', 'false', '', '/', '', '' );   	  
	      e.style.display = 'none';
	    }
    } else
    {
    	// Set tickerDisplay cookie to false
	      Set_Cookie( 'tickerDisplay', 'false', '', '/', '', '' );   	  
	      e.style.display = 'none';
    }
}
function toogle_visibility_base_on_cookie(id) {
   var scheme=window.location.protocol +"//";
   var e = document.getElementById(id);
   var cookieDisplay = Get_Cookie( 'tickerDisplay' );
   //alert('scheme='+scheme);
   if(scheme == 'http://')
   {
   		if(cookieDisplay == 'true') {
	   	// Set tickerDisplay cookie to true
	   	 // Set_Cookie( 'tickerDisplay', 'true', '', '/', '', '' ); 
	      e.style.display = 'block';   	 
	    } else {
	    // Set tickerDisplay cookie to false
	      Set_Cookie( 'tickerDisplay', 'false', '', '/', '', '' );   	  
	      e.style.display = 'none';
	    }
    } else
    {
    	// Set tickerDisplay cookie to false
	      Set_Cookie( 'tickerDisplay', 'false', '', '/', '', '' );   	  
	      e.style.display = 'none';
    }
}


function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( 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;
	}
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



  