//************************************************************
//				START COPIED
//************************************************************
function fixwidthheight(_3){
	
	if(navigator.userAgent.indexOf("Firefox/2")==-1){
		if(_3.width<_3.height){
			if(_3.parentNode != null){
				if(_3.parentNode.parentNode != null){
					_3.style.height= _3.parentNode.parentNode.offsetHeight - 40 + "px" ;
					_3.style.width="auto";
				}
			}
		}
	}
}
/*
function InitSize(){
		past_value = Get_Cookie("scroll_value")*10;
		if(past_value == null){
			past_value = 30;
		}
		
		s.setValue (past_value);
		
		var emNodes = document.getElementsByTagName('div');
		var max = emNodes.length;
		for(var i = 0;i < max;i++){
		   var nodeObj = emNodes.item(i);
		   if(nodeObj.getAttribute("id") == "img-container"){
				//alert(1);
				nodeObj.style.height = 115+past_value+'px';
				nodeObj.style.width = 80+past_value+'px';
				//width = 80
				//height = 115
				
				//306
				//341
				
		   }
		}
		
		var nodes =  document.getElementsByTagName('img');
		max = nodes.length;
		for(var j = 0;j < max;j++){
		   var nodeObj = nodes.item(j);
		   if(nodeObj.getAttribute("id") == "thumbnail"){
				fixwidthheight(nodeObj);				
		   }
		}

}*/
//************************************************************
//				END OF COPIED
//************************************************************



//*************************************************
//				START OF OTHER COOKIE FUNCTIONS
//*****************************************************

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 ;
}
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 FUNCTION READS THE GET PARAMETER VALUE
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//******************************************************
//			END OF OTHER (COOKIE) FUNCTIONS
//*********************************************************



//******************************************************
//			AJAX FUNCTIONS
//*********************************************************

function showResults(browsenodeid,searchindex,locale,total_count){
	var keywords = gup('keywords');
	if(browsenodeid == '' && searchindex == '' && keywords == ''){
		return;
	}
	document.getElementById('ajax_loading').style.visibility = 'visible';
	
	var per_page = 10;
	var total_pages = Math.ceil(total_count/10);
	if(total_pages < 10){
		per_page = total_pages ;
		//alert(per_page);
	}
	
	// do nothing and show message if last page found
	var this_page = parseInt(Get_Cookie('page_index'));
	if(this_page != "" && this_page != null){
		var this_total = this_page * per_page * 10 ;
		//alert(this_total+"  "+total_count);
		if(this_total *2 >= total_count){
			document.getElementById('table_more').innerHTML = "No more listings...";
			//return;
		}
	}
	for(var i=1;i<=10;++i){
		var page_index = Get_Cookie('page_index');
		if(page_index == "" || page_index == null){
			page_index = 0;
		}else{
			page_index = parseInt(page_index,10);
		}
		var page = 10*page_index+i ;
		
		ajaxFunction(page,browsenodeid,searchindex,locale);
	}
	document.getElementById('ajax_loading').style.visibility = 'hidden';
}

function ajaxFunction(page,browsenodeid,searchindex,locale){
	var xmlHttp;
	//alert(xmlHttp);
	try{
	  // Firefox, Opera 8.0+, Safari
	  if(xmlHttp==null){
	  	xmlHttp=new XMLHttpRequest();
	  }
	}catch (e){
	  // Internet Explorer
	  try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e){
		try{
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
		  alert("Your browser does not support AJAX!");
		  return false;
		 }
	   }
	}
		
		
		
	  xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
		  if(xmlHttp.status == 200){
			  //document.myForm.time.value=xmlHttp.responseText;
			  //CODE HERE
			  try{
			  	document.getElementById('listings').innerHTML += xmlHttp.responseText;
			  }catch(e){
				  
			  }
			  document.getElementById('ajax_loading').style.visibility = 'hidden';
			  delete xmlHttp;
		  }
		}
		
	  }
	  
	  var keywords = gup('keywords');
	  //alert(keywords);
	  if(locale == 'uk'){
		  var dir = "uk";
	  }else if(locale == 'us'){
		  var dir = "usa";
	  }else if(locale == 'ca'){
		  var dir = "canada";
	  }
	  if((searchindex != "" && browsenodeid != null) || keywords != ''){
		  	//alert(2);
			//General Url
			//var url = "http://uk.sustoo.com/amazon-browser/"+dir+"/getItems.php?searchindex="+searchindex+"&browsenodeid="+browsenodeid;
			var url = "http://uk.sustoo.com/amazon-browser/getItems.php?searchindex="+searchindex+"&browsenodeid="+browsenodeid;
			//alert(url);
			 if(gup('max')  !=  ""){
				 url += "&max="+gup('max');
			 }
			 if(gup('min')  !=  ""){
				 url += "&min="+gup('min');
			 }
			 if(gup('zip')  !=  ""){
				 url  +=  "&zip"+gup('zip');
			 }
			 if(gup('keywords')	!= ""){
				 url  += "&keywords=" + gup('keywords');
			 }
			 url = url+"&page="+page ;
			 //alert(url);
			 xmlHttp.open("GET",url,true);
		  	 xmlHttp.send(null);
			 document.getElementById('ajax_loading').style.visibility = 'visible';
		}
	  
	  //alert(url);
	  //for(;i<3;++i){
		  
	  //}
  }

//  xmlHttp.open("GET","",true);
//  xmlHttp.send(null);

//document.getElementById('listings').innerHTML += "kjh";
//******************************************************
//			END OF AJAX FUNCTIONS
//*********************************************************

//******************************************************
//			WATERMARK FUNCTIONS
//*********************************************************
/*
function OnFocus(elementId, defaultText)
{ 
   if (document.getElementById(elementId).value == defaultText)
   {
      document.getElementById(elementId).className = "normal";
      document.getElementById(elementId).value = "";
   }
}

function OnBlur(elementId, defaultText)
{
   var textValue = document.getElementById(elementId).value;

   if (textValue == defaultText || textValue.length == 0)
   {
      document.getElementById(elementId).className = "watermark";
      document.getElementById(elementId).value = defaultText;
   }
   else
      document.getElementById(elementId).className = "normal";
}
*/
function inputfocus(el,_10){
	if(el.value==_10){
		el.value="";
		el.style.color="#000000";
		el.select();
	}
}
function inputblur(el,_12){
	if(el.value==""){
		el.value=_12;
		el.style.color="#aaaaaa";
	}
}

function inputinit(el,_14){
	if(el.value==""||el.value==_14){
		el.value=_14;
	}else{
		el.style.color="#000000";
	}
}

//******************************************************
//			END OF WATERMARK FUNCTIONS
//*********************************************************



//******************************************************
//			VALID INTEGER VALIDATION FUNCTIONS
//*********************************************************

function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function ClearIfNonNumeric(elem){
	if(IsNumeric(elem.value)){
		return;
	}else{
		elem.value = "";	
	}
}


//******************************************************
//			END OF VALID INTEGER VALIDATION FUNCTIONS
//*********************************************************
