var firsterrorfld='';

function roll(img_name, img_src)
{
	document.getElementById(img_name).src = img_src;
}

function ittSetCookie(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" : "");
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function ittGetCookie(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;
    }
}

function ValidateAllForm(formname, sectionname) 
{
	var errorlist = '';
	var cellref = '';
	var bsection = true;
	
	firsterrorfld=null;

	var elem = document.getElementById(formname).elements;
	for(var i = 0; i < elem.length; i++)
	{
		var localInput = elem[i]
		
		if(localInput)
		{
			bsection=true;
			sectionFld=localInput.getAttribute('section')
			if (sectionFld==null)
			{
				sectionFld='';
			}
			if (sectionFld != sectionname)
			{
				bsection=false;
			}

			if (bsection)
			{

				var iValue='';

				localInput.style.borderColor=''
				localInput.style.backgroundColor=''

				var bCheckForNumeric=false;
				
				switch(localInput.tagName)
				{
					case "SELECT":
						var sel_ind = localInput.selectedIndex;
						if (sel_ind >= 0)
						{
							iValue = localInput[sel_ind].value;
						}
						break;
					case "TEXTAREA":
						iValue = localInput.value;
						bCheckForNumeric=true;
						break;
					case "INPUT":
						if(localInput.type == 'checkbox')
						{
							//iValue = (localInput.checked?'on':'off');
							iValue = (localInput.checked?localInput.value:'');
						}
						else
						{
							iValue = localInput.value;
							bCheckForNumeric=true;
						}
						break;
				}
				
				var bNumericCheckPassed=true;
				if (bCheckForNumeric)
				{
					var numericFld=localInput.getAttribute('numeric')
					if (numericFld)
					{
						iValue=cleanCommas(iValue);
						localInput.value=iValue;
						if (!IsItNumeric(iValue))
						{
							bNumericCheckPassed=false;
						}
					}
				}
				
				var bDateCheckPassed=true;
				if (bCheckForNumeric)
				{
					var dtFld=localInput.getAttribute('checkdate')
					if (dtFld)
					{
						if (!isDate(iValue))
						{
							bDateCheckPassed=false;
						}
					}
				}
			
				parentdiv=localInput.getAttribute('parentdiv')
				parentdiv2=localInput.getAttribute('parentdiv2')
				parentdiv3=localInput.getAttribute('parentdiv3')

				// Checks if the required attribute is present - if so, then checks this element has a value
				requiredFld=localInput.getAttribute('required')
				if (requiredFld)
				{
					var elemDisplay=localInput;
					var elemDisplay2=localInput;
					var elemDisplay3=localInput;
					if (parentdiv)
					{
						elemDisplay=document.getElementById(parentdiv);
						elemDisplay2=document.getElementById(parentdiv);
						elemDisplay3=document.getElementById(parentdiv);
					}
					if (parentdiv2)
					{
						elemDisplay2=document.getElementById(parentdiv2);
						elemDisplay3=document.getElementById(parentdiv2);
					}
					if (parentdiv3)
					{
						elemDisplay3=document.getElementById(parentdiv3);
					}

					if ((elemDisplay.style.display != 'none') && (elemDisplay2.style.display != 'none') && (elemDisplay3.style.display != 'none'))
					{
					
						var notzero=localInput.getAttribute('notzero')
						if (notzero == 'true')
						{
							if ((iValue == '0') || (iValue == '0.00') || (iValue == '0.0') || (iValue == '00') || (iValue == '000'))
							{
								iValue='';
							}
						}					
					
						if ((!bDateCheckPassed) || (!bNumericCheckPassed) || ((requiredFld != '') && (iValue == '')))
						{
							errorlist += '- ' + localInput.getAttribute('descr') + '<br>';
							localInput.style.borderColor='red'
							localInput.style.backgroundColor='#f8e6e7'

							if (firsterrorfld == null)
							{
								firsterrorfld = localInput;
							}
						}
					}
				}

				// Checks if the requiredif attribute is present - if so, then it makes this element required if the other has a value
				requiredFldIf=localInput.getAttribute('requiredif')
				if ((requiredFldIf) && (requiredFld != '') && (iValue == ''))
				{
					var elemDisplay=localInput;
					var elemDisplay2=localInput;
					if (parentdiv)
					{
						elemDisplay=document.getElementById(parentdiv);
						elemDisplay2=document.getElementById(parentdiv);
					}
					if (parentdiv2)
					{
						elemDisplay2=document.getElementById(parentdiv2);
					}

					if ((elemDisplay.style.display != 'none') && (elemDisplay2.style.display != 'none'))
					{
						iValue='';
						var requiredFldElement = document.getElementById(requiredFldIf)
						switch(requiredFldElement.tagName)
						{
							case "SELECT":
								var sel_ind = requiredFldElement.selectedIndex;
								if (sel_ind >= 0)
								{
									iValue = requiredFldElement[sel_ind].value;
								}
								break;
							case "TEXTAREA":
								iValue = requiredFldElement.value;
								break;
							case "INPUT":
								if(requiredFldElement.type == 'checkbox')
								{
									//iValue = (requiredFldElement.checked?'on':'off');
									iValue = (requiredFldElement.checked?localInput.value:'');
								}
								else
								{
									iValue = requiredFldElement.value;
								}
								break;
						}
						
						var notzero=localInput.getAttribute('notzero')
						if (notzero == 'true')
						{
							if ((iValue == '0') || (iValue == '0.00') || (iValue == '0.0') || (iValue == '00') || (iValue == '000'))
							{
								iValue='';
							}
						}

						//var reqvalue=localInput.getAttribute('requiredvalue')
						//if (reqvalue == null)
						//{
						//	reqvalue=''
						//}

						if ((!bDateCheckPassed) || (!bNumericCheckPassed) || ((requiredFldElement != '') && (iValue != '')))
						{
							errorlist += '- ' + localInput.getAttribute('descr') + '<br>';
							localInput.style.borderColor='red'
							localInput.style.backgroundColor='#f8e6e7'
							
							if (firsterrorfld == null)
							{
								firsterrorfld = localInput;
							}
						}
					}
				}
			}
		}
		else
		{
			break;
		}
	}
	return errorlist;
}

function mygup( 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];
}

var varLastNewsID=0;
var bInTransition=0;
var varMaxNews=0;

function setSelNewsMoveOver(obj)
{
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;


	if (is_chrome)
	{
		setSelNews(obj);
	}
}

function setSelNews(obj)
{
	for (var idx=1; idx<=varMaxNews; idx++)
	{
		try
		{
			var classes = document.getElementById("homenewsitem"+idx).className;
			classes=classes.replace('/\bselected\b/','');
			classes=classes.replace(' selected','');
			document.getElementById("homenewsitem"+idx).className=classes;
		} catch (ex) {}
	}
	
	if (bInTransition==0)
	{
		bInTransition=1;
		try
		{
			document.getElementById("homenewsitem"+obj).className += " selected";
		} catch (ex) {}
		try
		{
			if (varLastNewsID != obj)
			{
				$("#banner").fadeTo("fast", 0.7);
			}
		} catch (ex) {}
		try
		{
			document.getElementById("banner").innerHTML=document.getElementById("newsimage"+obj).innerHTML;
			document.getElementById("newsbox").innerHTML=document.getElementById("newscontent"+obj).innerHTML;
		} catch (ex) {}
		try
		{
			if (varLastNewsID != obj)
			{
					$("#banner").fadeTo("slow", 1.0);
					//$("#newsbox").fadeTo("slow", 1.0);
			}
		} catch (ex) {}
		varLastNewsID = obj;
		bInTransition=0;
	}
	
}

function gotoBrowseMarket()
{
	if (document.getElementById("market_selector").value != "")
	{
		if (document.getElementById("brand_selector").value != "")
		{
			document.location.href=document.getElementById("market_selector").value + "default.aspx?manufacturerid="+document.getElementById("brand_selector").value;
		}
		else
		{
		  document.location.href=document.getElementById("market_selector").value;
		}
	}
}

function gotoBrowseBrand()
{
	if (document.getElementById("brand_selector").value != "")
	{
		var manuid = document.getElementById("brand_selector").value;
		var manuurl = "";

		if (manuid == '4')
		{
			manuurl = "/alcon/";
		}
		else if (manuid == '2')
		{
			manuurl = "/flojet/";
		}
		else if (manuid == '1')
		{
			manuurl = "/jabsco/";
		}
		else if (manuid == '3')
		{
			manuurl = "/midland-acs/";
		}
		else if (manuid == '5')
		{
			manuurl = "/rule/";
		}

		document.location.href=manuurl;
	}
}



function gotoBrowseProducts()
{
	if (document.getElementById("product_category").value != "")
	{
		if ((document.getElementById("brand_selector").value != "") && (document.getElementById("market_selector").value != ""))
		{
			document.location.href=document.getElementById("product_category").value + "default.aspx?manufacturerid="+document.getElementById("brand_selector").value;
		}
		else
		{
			document.location.href=document.getElementById("product_category").value;
		}
	}
	else if (document.getElementById("market_selector").value != "")
	{
		document.location.href=document.getElementById("market_selector").value + "default.aspx?manufacturerid="+document.getElementById("brand_selector").value;
	}
	else
	{
		var manuid = document.getElementById("brand_selector").value;
		var manuurl = "";

		if (manuid == '4')
		{
			manuurl = "/alcon/";
		}
		else if (manuid == '2')
		{
			manuurl = "/flojet/";
		}
		else if (manuid == '1')
		{
			manuurl = "/jabsco/";
		}
		else if (manuid == '3')
		{
			manuurl = "/midland-acs/";
		}
		else if (manuid == '5')
		{
			manuurl = "/rule/";
		}
		document.location.href=manuurl;
	}
}

function setupFilterSelectors()
{
	//alert(document.location.pathname);
	var curpagepath = document.location.pathname;
	
	curpagepath=curpagepath.replace("default.aspx","");
	
	var prods = document.getElementById("product_category");
	
	for(i=0;i<prods.length;i++)
	{
		if(prods.options[i].value==curpagepath)
		{
			prods.selectedIndex=i;
		}
	}
	
	var markets = document.getElementById("market_selector");

	for(i=0;i<markets.length;i++)
	{
		if(markets.options[i].value==curpagepath.substring(0,markets.options[i].value.length))
		{
			markets.selectedIndex=i;
		}
	}
	
	var brands = document.getElementById("brand_selector");

	for(i=0;i<brands.length;i++)
	{
		var vBrandID = brands.options[i].value;
		if (((vBrandID=="1") && (curpagepath.substring(0,8)=="/jabsco/")) ||
			((vBrandID=="2") && (curpagepath.substring(0,8)=="/flojet/")) ||
			((vBrandID=="3") && (curpagepath.substring(0,13)=="/midland-acs/")) ||
			((vBrandID=="4") && (curpagepath.substring(0,7)=="/alcon/")) ||
			((vBrandID=="5") && (curpagepath.substring(0,6)=="/rule/")) )
		{
			brands.selectedIndex=i;
		}
	}	
	
	var vManuID = mygup('manufacturerid');
	if (vManuID != null)
	{
		if (vManuID != "")
		{
			for(i=0;i<brands.length;i++)
			{
				var vBrandID = brands.options[i].value;
				if (vBrandID==vManuID)
				{
					brands.selectedIndex=i;
				}
			}	
		}
	}
}

function gotoBrowseBrands()
{
	if (document.getElementById("brand_selector").value != "")

	{
		document.location.href=document.getElementById("brand_selector").value;
	}
	else
	{
		document.location.href=document.getElementById("product_category").value;
	}
}

function gotoDistributorSearch(sDistrCountry)
{
	document.location.href='/distributor-locator/default.aspx?country='+escape(sDistrCountry);
}
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 Lvl_jumpMenuWin(el,y,q) { //v1.1 4LevelWebs
  var O=el.options[el.selectedIndex].value,ww,hh;if(y)el.selectedIndex=0;
  var er=O.charAt(O.length),as=O.substring(O.length,er),a=as.split('~');
  var tp=",top=",tr=",toolbar=",ts=",status=",ln=",location=",mb=",menubar=",ss=",scrollbars=",re=",resizable=",fs=",fullscreen=";
  var sw=(screen.width),sh=(screen.height);
  var u=a[0],n=a[1],WW=a[2],HH=a[3];
  var t=(a[6]==0)?tr+0:tr+1,l=(a[7]==0)?ln+0:ln+1,s=(a[8]==0)?ts+0:ts+1,z=(a[13]==0)?fs+0:fs+1;
  var m=(a[9]==0)?mb+0:mb+1;x=(a[10]==0)?ss+0:ss+1,r=(a[11]==0)?re+0:re+1,c=a[12];
   if (a[12]==0){ww=(sw)}else{ww=(sw-WW)/2}
   if (a[12]==0){hh=(sh)}else{hh=(sh-HH)/2}
  var j=(c==1)?tp+hh:tp+a[5],k=(c==1||c==2)?",left="+ww:",left="+a[4];if(q==0)q='top';
  var W=(a[2]==0)?0:",width="+WW,H=(a[3]==0)?0:",height="+HH,f=W+=H+=j+=k+=t+=l+=s+=m+=x+=r+=z;
   if(u)if(a[14]==1){u=eval("parent.frames[\'"+q+"\'].location='"+u+"'")}else{window.open(u,n,f)}el.blur();
}
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_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];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}