function showdiv(name){
	var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
	if(obj.style.display=="none"){		// if the div is currently closed
		obj.style.display="";		    
		if(name == 'srchcontainerimgadv') {
			ajaxpage('set_session_variables.html?advanced=1','');
			ajaxpage('set_session_variables.html?showoptions=1','');			
			document.getElementById("options").innerHTML = 'Fewer Options';
			// reset colourSearch
			$('#cancelColorSearch').click();
		}
		if(name == 'hideshow'){
			ajaxpage('set_session_variables.html?openlightbox=1','');
			ajaxpage('lightbox.html','hideshow');
			document.getElementById("img_lb_show").src = "/images/icons/lb_hide.gif";
		}
		if (name == 'hideshowbin'){
			ajaxpage('set_session_variables.html?openclipbin=1','');
			ajaxpage('clipbin.html','hideshowbin');			
			document.getElementById("img_cb_show").src = "/images/icons/cb_hide.gif";
		}
	}
	else {
		obj.style.display="none";	  		
		if(name == 'srchcontainerimgadv') {
			ajaxpage('set_session_variables.html?advanced=0','');
			ajaxpage('set_session_variables.html?showoptions=0','');
			document.getElementById("options").innerHTML = 'More Options';
			// reset colourSearch
			$('#cancelColorSearch').click();
		}
		if(name == 'hideshow'){
			ajaxpage('set_session_variables.html?openlightbox=0','');	
			document.getElementById("img_lb_show").src = "/images/icons/lb_show.gif";
		}
		if(name == 'hideshowbin'){
			ajaxpage('set_session_variables.html?openclipbin=0','');
			document.getElementById("img_cb_show").src = "/images/icons/cb_show.gif";
		}
	}
}
 
// specifically to show a div 
function show(id) {	 	
	document.getElementById(id).style.display = '';
	switch(id){
		case "hideshow":
			if(document.getElementById("img_lb_show") != null){
				document.getElementById("img_lb_show").src = "/images/icons/lb_hide.gif";
			}
		break;
		case "hideshowbin":
			if(document.getElementById("img_cb_show") != null){
				document.getElementById("img_cb_show").src = "/images/icons/cb_hide.gif";
			}
		break;
	}
}

// specifically to hide/show purchase options
function showhide(show,hide) {	 	
	document.getElementById(show).style.display='';
	document.getElementById(hide).style.display='none';
}

function hidediv(name){
	var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
	if(obj.style.display == "none"){
		obj.style.display = "";
	}
	else {
		obj.style.display = "none";
	}
}

// generic hide and show - please do notr edit.. this is the basic hide and show functions - Ben Spencer
function hdiv(vDiv) {	 	
	document.getElementById(vDiv).style.display="none";
}

function sdiv(vDiv) {	 	
	document.getElementById(vDiv).style.display="";
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if(a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* AM check for object existence*/
function testIsValidObject(objToTest) {
	if(null == objToTest){
		return false;
	}
	if("undefined" == typeof(objToTest)){
		return false;
	}
	return true;
}

function updateSearchType(){
	if(testIsValidObject(document.search.rf_brand_id) && testIsValidObject(document.search.rp_brand_id) && testIsValidObject(document.search.sp_brand_id))
		if(document.search.rf_brand_id.selectedIndex == -1 && document.search.rp_brand_id.selectedIndex == -1 && document.search.sp_brand_id.selectedIndex == -1)
			document.search.search_type.value = 'keyword';		
		else
			document.search.search_type.value = 'keyword';			
}

function selectChange(){
	if(testIsValidObject(document.search.rf_brand_id) && testIsValidObject(document.search.rp_brand_id) && testIsValidObject(document.search.sp_brand_id)){
		document.search.rp_search.checked = 0;
		document.search.rf_search.checked = 0;
		document.search.rpe_search.checked = 0;
		updateSearchType();	
	}	
}

function checkBoxChange1(){
		if(testIsValidObject(document.search.rp_brand_id))			
			document.search.rp_brand_id.selectedIndex = -1;
		if(testIsValidObject(document.search.sp_brand_id))			
			document.search.sp_brand_id.selectedIndex = -1;			
		updateSearchType();
}
	
function checkBoxChange2(){
		if(testIsValidObject(document.search.rf_brand_id))
			document.search.rf_brand_id.selectedIndex = -1;
		updateSearchType();
}

function go_page(page_to_jump,image_page,total_page,urlparam) {
	if( page_to_jump.length < 1 || image_page.length < 1 || total_page.length < 1
		|| isNaN(page_to_jump) || isNaN(image_page) || isNaN(total_page))
		{
		return false;
		}
	var c = (eval(page_to_jump)-1)*eval(image_page)+1;
	var t = (eval(total_page)-1)*eval(image_page)+1;
	var ct = 0;
	if (c > t)
		ct = t;
	else if (c <= 0)
		ct = 1;
	else
		ct = c;
	if (urlparam.indexOf("images.html") != -1)
		str = urlparam.replace('images.html','images_ajax.html');
	else if (urlparam.indexOf("footage.html") != -1)
		str = urlparam.replace('footage.html','footage_ajax.html');
	ajaxpage(''+str+'&counter='+ct+'','results');
}

function deselect(select_box) {
	if(!select_box)return;
	var r = new Array();
	for(var i = 0; i < select_box.options.length; i++) {
		if(select_box.options[i].selected)
			select_box.options[i].selected = false;
	}
}

function clear_filters(box1,box2,box3){
	deselect(box1);
	deselect(box2);
	deselect(box3);
	document.search.rp_search.checked = 1;
	document.search.rpe_search.checked = 1;
	document.search.rf_search.checked = 1;	
}

/* START ajax */
function ajaxpage(url, containerid){
	var page_request = false;
	if (containerid == 'results') {
		if (document.getElementById('loading_sheader1'))
			document.getElementById('loading_sheader1').style.display = '';
		if (document.getElementById('loading_sheader2'))
			document.getElementById('loading_sheader2').style.display = '';
	}
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try {
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else
		return false;
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid);
	}
	nocache = Math.random();
	n_url = url;
	if (url.indexOf("?") == -1)
		n_url = n_url + '?nocache='+nocache;
	else
		n_url = n_url + '&nocache='+nocache; 
	page_request.open('GET', n_url, true);
	page_request.send(null);
}

function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
		if (containerid != '') {							
			document.getElementById(containerid).innerHTML=page_request.responseText;			
			if (containerid == 'results') {
				document.getElementById('within').style.display = '';
				document.getElementById('lf').scrollTop = 0;
				if (document.getElementById('loading_sheader1'))
					document.getElementById('loading_sheader1').style.display = 'none';
				if (document.getElementById('loading_sheader2'))
					document.getElementById('loading_sheader2').style.display = 'none';
				if (location.hostname.indexOf("dev") == -1)					
					pageTracker._trackPageview("/SearchResultsAjax" );
			}
			if(document.getElementById("loader")){
				removeLoading();
			}	
				
		}
}
/* END  */

function toggle_div(thisdiv,totaldivs,divname,jbid,type){
	
	for(i=1;i<=totaldivs;i++){
		if (i!= thisdiv) {			
			document.getElementById(divname+i).style.display = 'none';							
			document.images['arrow_'+i].src = "images/lightbox/lb_dnarrow.gif";
		}
	}
	if (document.getElementById(divname+thisdiv).style.display=="none") {
		document.getElementById(divname+thisdiv).style.display="";
		document.images['arrow_'+thisdiv].src = "images/lightbox/lb_uparrow.gif";
		if(type == 'img')
			ajaxpage('job_bag.html?event=set_default_lb&set_firstlb=0&job_bag_id='+jbid+'&jbrownum='+thisdiv,'item'+thisdiv);
		else
			ajaxpage('clipbin_ajax.html?event=set_default_lb&set_firstlb=0&bin_id='+jbid+'&jbrownum='+thisdiv,'item'+thisdiv);
	}
	else {
		document.getElementById(divname+thisdiv).style.display="none";
		document.images['arrow_'+thisdiv].src = "images/lightbox/lb_dnarrow.gif";			
		if(type == 'img')
			ajaxpage('job_bag.html?event=set_default_lb&set_firstlb=1&jbrownum='+thisdiv,'item'+thisdiv);
		else
			ajaxpage('clipbin_ajax.html?event=set_default_lb&set_firstlb=1&jbrownum='+thisdiv,'item'+thisdiv);
	}
}
					
function edit_lb(name,rownum,jbid,totaljb,oldname,type) {
	if(name == ''){
		if(type == 'img')
			alert('Lightbox name cannot be blank.\nYour change has not been saved.');
		else
			alert('Clipbin name cannot be blank.\nYour change has not been saved.');
		document.getElementById('lbn_'+rownum).style.backgroundColor = "#cccccc";
		document.getElementById('lbn_'+rownum).value = oldname;
	}else{
		document.getElementById('lbn_'+rownum).style.backgroundColor = "white";
		if (type == 'img')	
			ajaxpage('job_bag.html?event=editlbname&jbrownum='+rownum+'&job_bag_id='+jbid+'&jb_newname='+name+'&totaljb='+totaljb,'jb');
		else
			ajaxpage('clipbin_ajax.html?event=editlbname&jbrownum='+rownum+'&bin_id='+jbid+'&jb_newname='+name+'&totaljb='+totaljb,'jb');
	}
}

function popupWindow(url,width,height,scrollbars,winName) {
	var handle = window.open(url,winName,'toolbar=no,location=no,directories=no,status=yes,scrollbars='+scrollbars+',resizable=yes,width='+width+',height='+height);
	handle.focus();
}

function getForm(fobj) {
 var str = "";
 var ft = "";
 var fv = "";
 var fn = "";
 var els = "";
 
 for(var i = 0;i < fobj.elements.length;i++) {
  els = fobj.elements[i];
  ft = els.title;
  fv = els.value;
  fn = els.name;
  
 switch(els.type) {
  case "text":
  case "hidden":
  case "password":
  case "textarea":
  str += fn + "=" + encodeURI(fv) + "&";
  break; 

  case "checkbox":
  case "radio":
   if(els.checked) str += fn + "=" + encodeURI(fv) + "&";
  break;	

  case "select-one":
	str += fn + "=" + els.options[els.selectedIndex].value + "&";
  break;
  
  case "select-multiple":
	str += fn + "=" + getSelectedValues(els) + "&";
  break;  

  } // switch
 } // for
 str = str.substr(0,(str.length - 1));
 return str;
}
function getSelectedValues (selectbox) {
  var r = new Array();
  for (var i = 0; i < selectbox.options.length; i++)
    if (selectbox.options[i].selected)
      r[r.length] = selectbox.options[i].value;
    return r;
}

function EnterPressed(e) {
// Code adapted from Jennifer Madden
// http://jennifermadden.com/162/examples/stringEnterKeyDetector.html

  var characterCode
  if(e && e.which){           // NN4 specific code
    e = e
    characterCode = e.which
  }
  else {
    e = event
    characterCode = e.keyCode // IE specific code
  }
  if (characterCode == 13) return true   // Enter key is 13
  else return false
}

function check_price(fm) {
	var ticked = false;
	for(var n = 0; n < fm.elements.length; n++){
		if(fm.elements[n].type == 'checkbox'){
			if(fm.elements[n].checked ){ // if any check boxes are selected 
				return true; 
			}
		}
	}
	return ticked;
}//[end check_price]

// load spinning icon to interact with user
// Must include jquery.js
function loadSpinnerIcon()
{
	$("#loader").show();

}
function removeLoading(){
		$("#loader").hide();
}

// Begin DidYouMean Object
	var DidYouMean = {
		useThisKeywordSearch:function(str){
			var originalStr = $('#search_string').val();
			$('#cancelColorSearch').click(); 		// send event to clear Button
			
			
		if(document.forms['search']){
			document.forms['search'].reset();		
		}
		$('#search_string').val(str);

			checkForm(document.forms['search']); 	// triger search action 
		}	
	};
