
/*
 * Popup window script 
 *  
 * Example markup 
 * <a href="mypage.html" target="popUpWin" onclick="popUpWin(this.href,'console',660,520);return false;" title="Open popup window">Link text</a>
 */
 
 //pageTracker._trackPageview("/Goal1.html");

var newWindow = null;
function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	closeWin();
	if (type == "fullScreen"){
		strWidth = screen.availWidth - 10;
		strHeight = screen.availHeight - 160;
	}
	var tools="";
	if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}

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];
}

$(document).ready(function() {

    $('#search-term').keydown(function(e){
        if (e.keyCode == 13) {
            document.location.href = jsAppPath + "search.aspx?q=" + $('#search-term').val() +"&st=all";
            return false;
        }        
    });
    
    $('#searchText').keydown(function(e){
        if (e.keyCode == 13) {
            document.location.href = jsAppPath + "search.aspx?q=" + $('#searchText').val() +"&st=all";
            return false;
        }        
    });
	
	// search text box watermarks
	$("#search-term").focus(function() {
		if((this).value=="Enter a keyword") {
			$(this).attr("value","");
		}
	});

	$("#search-term").blur(function() {
		if((this).value=="") {
			$(this).attr("value","Enter a keyword");
		}
	});


	// search text box watermarks
	$("#keywords").focus(function() {
		if((this).value=="Enter the product name or keyword") {
			$(this).attr("value","");
			$(this).addClass("active");
		}
	});

	$("#keywords").blur(function() {
		if((this).value=="") {
			$(this).attr("value","Enter the product name or keyword");
			$(this).removeClass("active");
		}
	});



	// contact watermarks
	$("input[value='Area Code']").addClass("watermark");

	$("#contact-areacode").focus(function() {
		if((this).value=="Area Code") {
			$(this).attr("value","");
			$(this).removeClass("watermark");
		}
	});

	$("#contact-areacode").blur(function() {
		if((this).value=="") {
			$(this).attr("value","Area Code");
			$(this).addClass("watermark");
		}
	});


	$("input[value='Telephone']").addClass("watermark");

	$("#contact-telephone").focus(function() {
		if((this).value=="Telephone") {
			$(this).attr("value","");
			$(this).removeClass("watermark");
		}
	});

	$("#contact-telephone").blur(function() {
		if((this).value=="") {
			$(this).attr("value","Telephone");
			$(this).addClass("watermark");
		}
	});
	
	// accoridan

	$(".product-specs h3:not(:first)").removeClass("active");	

	$(".product-specs h3.active").next().show();	

	$(".product-specs h3").click(function() {
		$(this).next().slideToggle("normal");
		$(this).toggleClass("active");
	});	
});


function setPrintReseller(currentReseller) {
	printHTML = $("#list-resellers li").eq(currentReseller).html();	
	
	$("#hidden-reseller-details").html(printHTML);
}

function doSearch()
{
    document.location.href = jsAppPath + "search.aspx?q=" + $('#search-term').val() +"&st=all";
    return false;
}



function setSpotlight() {
	
	// fix to make sure that the spotlight sits at bottom
	$("#spotlight").css({bottom:"0"});

	var currentNum = 1;
	var nextNum = currentNum + 1;
	var prevNum = currentNum - 1;


	$("#spotlight").find(".item").each(function(i) {
		i += 1;
		$(this).addClass("item" + i);
	});		

	setBase();
	arrowUpdate(currentNum);	

	function setBase() {
		$(".item").css({left: -1000});
		$(".item" + prevNum).css({left: 1000});
		$(".item" + currentNum).css({left: 125});
	}
	
	function playOutForward(s) {
		$(s).animate({left: 1000}, 200);
	}

	function playIn(s) {
		$(s).animate({left: 125}, 200);
	}

	function playOutBackward(s) {
		$(s).animate({left: -1000}, 200);		
	}

	
	function arrowUpdate(currentNum) {
		if (currentNum == 3) {
			$("#spotlight .btn-next").hide();
		}

		else if (currentNum == 1) {
			$("#spotlight .btn-prev").hide();
		}
		else {
			$("#spotlight .btn-next").show();
			$("#spotlight .btn-prev").show();
		}
	}	
	
	$("#spotlight .btn-next a").click(function() {
		playOutForward(".item" + currentNum);
		playIn(".item" + nextNum);

		currentNum += 1;
		prevNum += 1;
		nextNum += 1;

		arrowUpdate(currentNum);
	});

	$("#spotlight .btn-prev a").click(function() {
		playOutBackward(".item" + currentNum);
		playIn(".item" + prevNum);

		currentNum -= 1;
		prevNum -= 1;
		nextNum -= 1;

		arrowUpdate(currentNum);
	});

	}


// Form Validation
//browser detection
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

function GetMyObjectByID(itemID)
{
// In the future will need to build in the ability to handle v4 netscape browsers.
	if (document.getElementById) // Good browsers
		return document.getElementById(itemID)
	else if (document.all) // medium browsers
		return document.all[itemID];
		
	// Need to handle v4 browsers in the future.
}
function CheckField(itemID)
{
	var o = GetMyObjectByID(itemID);
	if(o.value.length<1)
		return false;
	else
		return true;
}

function CheckDD(itemID)
{
	var o = GetMyObjectByID(itemID);
	var select = o.options[o.selectedIndex].value;
	
	if(select.length<1 || select == "0")
		return false;
	else
		return true;
}

function CheckCheckbox(itemID)
{
	var o = GetMyObjectByID(itemID);
	
	return (o.checked);
}

function CheckRadio(FormName,itemID)
{	
	//var o = GetMyObjectByID(itemID);
	var o = document.forms[FormName][itemID];
	
	var cnt = -1;
	for (var i=0; i < o.length; i++) {
		if (o[i].checked) {
	   	cnt = i; 
		i = o.length;
	   }
	}
	if (cnt > -1) 
		return true;
	else 
		return false;
}

function CheckEmail(itemID) 
{
	var o = GetMyObjectByID(itemID);	
	if (o != null)
	{	
		// Make sure we let an email go through that is empty... The required field catcher will get that
		if (o.value.length < 1)
		{
			return false;
		}
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;		
		if (!regex.test(o.value))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function CheckABN(abn)
{

	//Check if there is a valid length first
	if (abn.length != 11) {return false;}

	//Weighting
	var abnWeight = new Array();
	abnWeight[0] = 10;
	abnWeight[1] = 1;
	abnWeight[2] = 3;
	abnWeight[3] = 5;
	abnWeight[4] = 7;
	abnWeight[5] = 9;
	abnWeight[6] = 11;
	abnWeight[7] = 13;
	abnWeight[8] = 15;
	abnWeight[9] = 17;
	abnWeight[10] = 19;
	
	//ABN Numbers
	var abnNumber = new Array();
	for(i=0; i<abn.length; i++) 
	{
		abnNumber[i] = abn.charAt(i);
	}
	
	//Subtrack 1 from first digit
	abnNumber[0] = abnNumber[0] - 1;
	
	//Apply Weighting
	var results = 0;
	
	//Apply Math
	for(j=0; j<abnWeight.length; j++)
	{
		results += abnWeight[j] * abnNumber[j];
	}
	
	//Apply Check
	if (results % 89 == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isSpecialKey(strValue){
// Returns true if one of these special key codes is pressed
//backspace Ctrl + C,enter, Ctrl + X, Ctrl + V, Ctrl + Z
var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	return reKeyboardChars.test(strValue);
}

/*
To filter the max length here is some example code 
to catch both key down and to catch paste events
onkeypress="return FilterMaxLength(event,this,250)" onpaste="return CheckPaste(this,250)"
*/
function FilterMaxLength(e,o,max){
// we must allow spaces , backspace etc to go through 
	var iKeyCode, strKey;  
	
	if (isIE) {
		iKeyCode = e.keyCode;
	} else {
		iKeyCode = e.which;
	}	
	strKey = String.fromCharCode(iKeyCode);	
	// Make sure we let special keys go through.
		if (isSpecialKey(strKey))
			return true;	
		var maxLength = max;
     	if(o.value.length > maxLength-1){
	 		alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
        	return false;
     	}
    	return true
	}
function CheckPaste(o,max){
	var maxLength = max;
	// Get from clipboard
	var data = window.clipboardData.getData("Text");

	var CurrentLength = o.value.length + data.length;
    if (CurrentLength > maxLength)
    {
		alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
		return false;
	}
	else
		return true;
}
//mask input to allow integer only
function CheckNumeric(objEvent) {
	var iKeyCode, strKey;  
	var reValidChars = /\d/;	
	var reKeyboardChars =       /[\x00\x03\x08\x0D\x16\x18\x1A]/;
	if (isIE) {
		iKeyCode = objEvent.keyCode;
	} else {
		iKeyCode = objEvent.which;
	}
	
	strKey = String.fromCharCode(iKeyCode);
	
	if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey)) {
		return false;
	}
}

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

 return true;
}

//Generic Function to Activate Google Code
function ActivateGACode(keyCode, selectedValue)
{
	switch(keyCode)
	{
		case 1:
		  pageTracker._trackPageview("/Find-Supplies-Home-Page/" + selectedValue + ".html");
		  break;    
		case 2:
		  pageTracker._trackPageview("/Find-Help-Support-Home-Page/" + selectedValue + ".html");
		  break;
		case 3:
		  pageTracker._trackPageview("/Find-Drivers-Downloads-Home-Page/" + selectedValue + ".html");
		  break;
		case 4:
		  pageTracker._trackPageview("/Quicklinks-Support/" + selectedValue + ".html");
		  break;
		case 5:
		  pageTracker._trackPageview("/Quicklinks-Drivers/" + selectedValue + ".html");
		  break;
		case 6:
		  pageTracker._trackPageview("/Drivers/" + selectedValue + ".html");
		  break;
		case 7:
		  pageTracker._trackPageview("/Downloads/" + selectedValue + ".html");
		  break;
		default:
		  //Do Nothing
		   break;
	}
}
