/*
 * General Javascript for PKL
 * http://ventutec.com
 *
*/

$(document).ready(
	function()
	{
		if( $("#cssdropdown li.headlink") )
		{
			$("#cssdropdown li.headlink").hover(
				function() { $("ul", this).css( "display", "block" ); },
				function() { $("ul", this).css( "display", "none" ); }
			);
		}
		if( $("#site-map") )
		{
			$("#site-map").hide();
		}
		if( $("#sitemap-link") )
		{
			$("#sitemap-link").click(
				function()
				{
					$("#site-map").slideToggle("slow");
					$(this).toggleClass("active");
				}
			);
		}
		if( $("a[rel=example_group]") )		{			$("a[rel=example_group]" ).fancybox(			{				'transitionIn'    : 'none',				'transitionOut'   : 'none',				'titleShow'       : true,				'titlePosition'   : 'over',				'titleFormat'     : function(title, currentArray, currentIndex, currentOpts) { return '<span id="fancybox-title-over">'+((this.title=='')?'Image '+(currentIndex+1)+' / '+currentArray.length:this.title)+'</span>'; }			});		}		if( $("a[rel=favouredfootprint_group]") )		{			var hFFGroup = $("a[rel=favouredfootprint_group]");			for( var nItem = 0; nItem < hFFGroup.length; nItem++ )			{				$( hFFGroup[nItem] ).fancybox(				{					'transitionIn'    : 'fade',					'transitionOut'   : 'fade',					'titlePosition'   : 'over',					'type'            : 'iframe',					'href'            : hFFGroup[nItem].href.replace('.asp', '.asp?so=inline'),					'showNavArrows'   : false,					'autoDimensions'  : false,					'titleShow'       : false,					'padding'         : 5,					'margin'          : 5,					'height'          : 725,					'width'           : 675				});			}		}		if( $("a[rel=provenpackage_group]") )		{			var hPPGroup = $("a[rel=provenpackage_group]");			for( var nItem = 0; nItem < hPPGroup.length; nItem++ )			{				$( hPPGroup[nItem] ).fancybox(				{					'transitionIn'    : 'fade',					'transitionOut'   : 'fade',					'titlePosition'   : 'over',					'type'            : 'iframe',					'href'            : hPPGroup[nItem].href.replace('.asp', '.asp?so=inline'),					'showNavArrows'   : false,					'autoDimensions'  : false,					'titleShow'       : false,					'padding'         : 5,					'margin'          : 5,					'height'          : 725,					'width'           : 675				});			}		}		if( $("a[rel=kititem_group]") )		{			var hKitGroup = $("a[rel=kititem_group]");			for( var nItem = 0; nItem < hKitGroup.length; nItem++ )			{				$( hKitGroup[nItem] ).fancybox(				{					'transitionIn'    : 'fade',					'transitionOut'   : 'fade',					'titlePosition'   : 'over',					'type'            : 'iframe',					'href'            : hKitGroup[nItem].href.replace('.asp', '.asp?so=inline'),					'showNavArrows'   : false,					'autoDimensions'  : false,					'titleShow'       : false,					'padding'         : 5,					'margin'          : 5,					'height'          : 625,					'width'           : 650				});			}		}		if( $("a[rel=stdpopup_group]") )		{			var hLinkGroup = $("a[rel=stdpopup_group]");			for( var nItem = 0; nItem < hLinkGroup.length; nItem++ )			{				$( hLinkGroup[nItem] ).fancybox(				{					'transitionIn'    : 'fade',					'transitionOut'   : 'fade',					'titlePosition'   : 'over',					'type'            : 'iframe',					'href'            : (hLinkGroup[nItem].href.replace('.asp?', '.asp&')).replace('.asp', '.asp?so=inline'),					'showNavArrows'   : false,					'autoDimensions'  : false,					'titleShow'       : false,					'padding'         : 5,					'margin'          : 5,					'height'          : 625,					'width'           : 675				});			}		}		if( document.getElementById( 'sub-content' ) && document.getElementById( 'langcode' ) )
		{
			if( document.getElementById( 'langcode' ).value != 'en' )
			{
				var hDisclaimer = document.createElement( 'p' );
				hDisclaimer.setAttribute( 'id', 'google-disclaimer' );
				var hDisclaimerContent = document.createTextNode( 'This page has been generated using Google Translate' );
				hDisclaimer.appendChild( hDisclaimerContent );

				var hSubContent = document.getElementById( 'sub-content' );
				var hParentDiv = hSubContent.parentNode;
				hParentDiv.insertBefore( hDisclaimer, hSubContent );

				var strCookieDisclaimer = GetCookie( 'pkl-foodservices-translate-disclaimer' );
				if( strCookieDisclaimer == 'declined' )
				{
					var strLocation = document.location.toString();
					strLocation = strLocation.replace( String( '/'+document.getElementById( 'langcode' ).value+'/foodservices/' ), '/foodservices/' );
					document.location = strLocation;
				}
				else if( strCookieDisclaimer == 'accepted' )
				{
					// OK
				}
				else
				{
					if( $("#google-disclaimer") )
					{
						$("#google-disclaimer").fancybox(						{							'transitionIn'			: 'fade',							'transitionOut'			: 'fade',							'titlePosition'			: 'over',							'enableEscapeButton'	: false,							'hideOnContentClick'	: false,							'hideOnOverlayClick'	: false,							'showCloseButton'		: false,							'href'					: '../../foodservices/accept-google-translations.asp',							'showNavArrows'			: false,							'autoDimensions'		: true,							'content'				: '<div id="google-disclaimer-popup"><p>The following pages have been translated by Google Translate so may contain errors and inaccuracies.</p><input type="button" id="ba" value="Accept" onclick="javascript: TermsAccepted(); " /><input type="button" value="Decline" onclick="javascript: TermsDeclined(); " /></div>',							'titleShow'				: false,							'padding'				: 15						});
						$("#google-disclaimer").click();
					}
				}
			}
		}
	}
);

function SearchBoxFocus(hSearchBox, bFocus)
{
	if( bFocus )
	{
//		User Focus on Search Box
		if( hSearchBox.alt.toUpperCase() == hSearchBox.value.toUpperCase() )
		{
			hSearchBox.value = '';
		}
	}
	else
	{
//		Focus lost on Search Box
		if( hSearchBox.value == '' )
		{
			hSearchBox.value = hSearchBox.alt;
		}
	}
}

function SubmitBrochureRequest(hBRForm)
{
	if( hBRForm )
	{
//		 Check Form Data
		if( ( hBRForm.receivebyemail.checked == false ) && ( hBRForm.receivebypost.checked == false ) )
			alert( hBRForm.receivebyemail.title );
		else if( hBRForm.brname.value == '' )
			alert( hBRForm.brname.title );
		else if( hBRForm.brcompany.value == '' )
			alert( hBRForm.brcompany.title );
		else if( hBRForm.brtel.value == '' )
			alert( hBRForm.brtel.title );
		else if( hBRForm.bremail.value == '' )
			alert( hBRForm.bremail.title );
		else if( hBRForm.braddr.value == '' )
			alert( hBRForm.braddr.title );
		else
		{
//			 Check "Hear About PKL" Section
			var nSelectedIndex = -1, nIndex = 0;
			for(nIndex = 0; nIndex < hBRForm.brhear.length; nIndex++)
			{
				if( hBRForm.brhear[nIndex].checked == true )
					nSelectedIndex = nIndex;
			}
//			 Check for item selection
			if( nSelectedIndex == -1 )
				alert( 'How did you hear about PKL?' );
			else if( ( nSelectedIndex == ( hBRForm.brhear.length - 1 ) ) && ( hBRForm.brheardetail.value == '' ) )
				alert( hBRForm.brheardetail.title );
			else
				return( true );
		}
	}
	return( false );
}

function SubmitContactUs(hCUForm)
{
	if( hCUForm )
	{
//		 Check Form Data
		if( hCUForm.ctname.value == '' )
			alert( hCUForm.ctname.title );
		else if( hCUForm.ctcompany.value == '' )
			alert( hCUForm.ctcompany.title );
		else if( hCUForm.cttel.value == '' )
			alert( hCUForm.cttel.title );
		else if( hCUForm.ctemail.value == '' )
			alert( hCUForm.ctemail.title );
		else
		{
//			 Check "Hear About PKL" Section
			var nSelectedIndex = -1, nIndex = 0;
			for(nIndex = 0; nIndex < hCUForm.cthear.length; nIndex++)
			{
				if( hCUForm.cthear[nIndex].checked == true )
					nSelectedIndex = nIndex;
			}
//			 Check for item selection
			if( nSelectedIndex == -1 )
				alert( 'How did you hear about PKL?' );
			else if( ( nSelectedIndex == ( hCUForm.cthear.length - 1 ) ) && ( hCUForm.ctheardetail.value == '' ) )
				alert( hCUForm.ctheardetail.title );
			else
				return( true );
		}
	}
	return( false );
}

function SubmitContingencyPlanRequest(hCPSForm)
{
	if( hCPSForm )
	{
//		 Check Form Data
		if( ( hCPSForm.receivebyemail.checked == false ) && ( hCPSForm.receivebypost.checked == false ) )
			alert( hCPSForm.receivebyemail.title );
		else if( hCPSForm.cpsname.value == '' )
			alert( hCPSForm.cpsname.title );
		else if( hCPSForm.cpscompany.value == '' )
			alert( hCPSForm.cpscompany.title );
		else if( hCPSForm.cpstel.value == '' )
			alert( hCPSForm.cpstel.title );
		else if( hCPSForm.cpsemail.value == '' )
			alert( hCPSForm.cpsemail.title );
		else if( hCPSForm.cpsaddr.value == '' )
			alert( hCPSForm.cpsaddr.title );
		else if( hCPSForm.cpspeople.value == '' )
			alert( hCPSForm.cpspeople.title );
		else if( hCPSForm.cpsserve.value == '' )
			alert( hCPSForm.cpsserve.title );
		else if( hCPSForm.cpsadditional.value == '' )
			alert( hCPSForm.cpsadditional.title );
		else if( hCPSForm.cpswork.value == '' )
			alert( hCPSForm.cpswork.title );
		else
			return( true );
	}

	return( false );
}

function SubmitProfitBreakdownRequest(hPCForm)
{
	if( hPCForm )
	{
//		 Check Form Data
		if( hPCForm.pcname.value == '' )
			alert( hPCForm.pcname.title );
		else if( hPCForm.pccompany.value == '' )
			alert( hPCForm.pccompany.title );
		else if( hPCForm.pctel.value == '' )
			alert( hPCForm.pctel.title );
		else if( hPCForm.pcemail.value == '' )
			alert( hPCForm.pcemail.title );
		else if( hPCForm.pcaddr.value == '' )
			alert( hPCForm.pcaddr.title );
		else
			return( true );
	}

	return( false );
}

function SubmitFeedbackForm(hFBForm)
{
	if( hFBForm )
	{
		return( true );
	}
	return( false );
}

function SubmitContactRepresentative(hCRForm)
{
	if( hCRForm )
	{
		if( hCRForm.cpname.value == '' )
			alert( hCRForm.cpname.title );
		else if( hCRForm.cpcompany.value == '' )
			alert( hCRForm.cpcompany.title );
		else if( hCRForm.cptel.value == '' )
			alert( hCRForm.cptel.title );
		else if( hCRForm.cpemail.value == '' )
			alert( hCRForm.cpemail.title );
		else
			return( true );
	}
	return( false );
}

// v4.01
function FindObject(n, d)
{
	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 = FindObject( n, d.layers[i].document );
	if( !x && d.getElementById )
		x = d.getElementById( n );

	return( x );
}

// v4.66 - Copyright (c)1998,2002 Yaromat.com
function CheckManualForm() {
  var args = CheckManualForm.arguments; var myDot=true; var myV=''; var myErr='';var addErr=false;var myReq;
  for (var i=1; i<args.length;i=i+4){
    if (args[i+1].charAt(0)=='#'){myReq=true; args[i+1]=args[i+1].substring(1);}else{myReq=false}
    var myObj = FindObject(args[i].replace(/\[\d+\]/ig,""));
    myV=myObj.value;
    if (myObj.type=='text'||myObj.type=='password'||myObj.type=='hidden'){
      if (myReq&&myObj.value.length==0){addErr=true}
      if ((myV.length>0)&&(args[i+2]==1)){ //fromto
        var myMa=args[i+1].split('_');if(isNaN(myV)||myV<myMa[0]/1||myV > myMa[1]/1){addErr=true}
      } else if ((myV.length>0)&&(args[i+2]==2)){
          var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");if(!rx.test(myV))addErr=true;
      } else if ((myV.length>0)&&(args[i+2]==3)){ // date
        var myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);
        if(myAt){
          var myD=(myAt[myMa[1]])?myAt[myMa[1]]:1; var myM=myAt[myMa[2]]-1; var myY=myAt[myMa[3]];
          var myDate=new Date(myY,myM,myD);
          if(myDate.getFullYear()!=myY||myDate.getDate()!=myD||myDate.getMonth()!=myM){addErr=true};
        }else{addErr=true}
      } else if ((myV.length>0)&&(args[i+2]==4)){ // time
        var myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);if(!myAt){addErr=true}
      } else if (myV.length>0&&args[i+2]==5){ // check this 2
            var myObj1 = FindObject(args[i+1].replace(/\[\d+\]/ig,""));
            if(myObj1.length)myObj1=myObj1[args[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!myObj1.checked){addErr=true}
      } else if (myV.length>0&&args[i+2]==6){ // the same
            var myObj1 = FindObject(args[i+1]);
            if(myV!=myObj1.value){addErr=true}
      }
    } else
    if (!myObj.type&&myObj.length>0&&myObj[0].type=='radio'){
          var myTest = args[i].match(/(.*)\[(\d+)\].*/i);
          var myObj1=(myObj.length>1)?myObj[myTest[2]]:myObj;
      if (args[i+2]==1&&myObj1&&myObj1.checked&&FindObject(args[i+1]).value.length/1==0){addErr=true}
      if (args[i+2]==2){
        var myDot=false;
        for(var j=0;j<myObj.length;j++){myDot=myDot||myObj[j].checked}
        if(!myDot){myErr+='* ' +args[i+3]+'\n'}
      }
    } else if (myObj.type=='checkbox'){
      if(args[i+2]==1&&myObj.checked==false){addErr=true}
      if(args[i+2]==2&&myObj.checked&&FindObject(args[i+1]).value.length/1==0){addErr=true}
    } else if (myObj.type=='select-one'||myObj.type=='select-multiple'){
      if(args[i+2]==1&&myObj.selectedIndex/1==0){addErr=true}
    }else if (myObj.type=='textarea'){
      if(myV.length<args[i+1]){addErr=true}
    }
    if (addErr){myErr+='* '+args[i+3]+'\n'; addErr=false}
  }
  if (myErr!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+myErr)}
  document.MM_returnValue = (myErr=='');
}


/************************************************************************************************************************************
*	
************************************************************************************************************************************/
function TermsTicked(hElement)
{
	if( document.getElementById( 'ba' ) )
	{
		document.getElementById( 'ba' ).disabled = !hElement.checked;
	}
}
/************************************************************************************************************************************
*	
************************************************************************************************************************************/
function TermsAccepted()
{
	SetCookie( 'pkl-foodservices-translate-disclaimer', 'accepted', 30 );
	parent.document.location = parent.document.location;
}
/************************************************************************************************************************************
*	
************************************************************************************************************************************/
function TermsDeclined()
{
	SetCookie( 'pkl-foodservices-translate-disclaimer', 'declined', 30 );
	parent.document.location = parent.document.location;
}
/************************************************************************************************************************************
*	Get Cookie
************************************************************************************************************************************/
function GetCookie(strName)
{
	if( document.cookie.length > 0 )
	{
		var chStart = document.cookie.indexOf( strName + '=' );
		if( chStart != -1 )
		{
			chStart = chStart + ( strName.length + 1 );
			var chEnd = document.cookie.indexOf( ';', chStart );
			if( chEnd == -1 )
				chEnd = document.cookie.length;

			return( unescape( document.cookie.substring( chStart, chEnd ) ) );
		}
	}
	return( '' );
}
/************************************************************************************************************************************
*	Set Cookie
************************************************************************************************************************************/
function SetCookie(strName, strValue, nExpireDays)
{
	var hExpireDate = new Date();
	hExpireDate.setDate( hExpireDate.getDate() + nExpireDays );
	document.cookie = strName+'='+escape( strValue )+( ( hExpireDate == null ) ? '' : ';expires='+hExpireDate.toGMTString() );
}
/************************************************************************************************************************************
*	Set Cookie
************************************************************************************************************************************/
function SFBR(strID)
{
	if( $('input[rel="'+strID+'"]') )	{		var hFBGroup = $('input[rel="'+strID+'"]');		for( var nItem = 0; nItem < hFBGroup.length; nItem++ )		{			$( hFBGroup[nItem] ).click();		}	}}

