// JK Pop up image viewer script- By JavaScriptKit.com
// Visit JavaScript Kit (http://javascriptkit.com)
// for free JavaScript tutorials and scripts
// This notice must stay intact for use

var popbackground="white" //specify backcolor or background image for pop window
var windowtitle="Image Window"  //pop window title

function detectexist(obj){
	return (typeof obj !="undefined")
}

function jkpopimage(imgpath, popwidth, popheight, textdescription){

	function getpos(){
		leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
		toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
		if (window.opera){
			leftpos-=screenLeft
			toppos-=screenTop
		}
	}

	getpos()
	var winattributes='width='+popwidth+',height='+popheight+',toolbar=no,location=no,resizable=no,status=no,menubar=no,scrollbars=no,left='+leftpos+',top='+toppos
	var bodyattribute=(popbackground.indexOf(".")!=-1)? 'background="'+popbackground+'"' : 'bgcolor="'+popbackground+'"'
	if (typeof jkpopwin=="undefined" || jkpopwin.closed)
	jkpopwin=window.open("","",winattributes)
	else{
		//getpos() //uncomment these 2 lines if you wish subsequent popups to be centered too
		//jkpopwin.moveTo(leftpos, toppos)
		//jkpopwin.resizeTo(popwidth, popheight+30)
	}
	jkpopwin.document.open()
	jkpopwin.document.write('<html><title>'+windowtitle+'</title><body oncontextmenu="return false;" '+bodyattribute+'><img src="'+imgpath+'" style="margin-bottom: 0.5em"><br />'+textdescription+'</body></html>')
	jkpopwin.document.close()
	jkpopwin.focus()
}

$(function() {
	load_epc_drop();
	load_epc_search2();
});

function load_epc_drop() {

	var epc_my		 		= document.getElementById("epc_my");
	var epc_make 			= document.getElementById("epc_make");
	var epc_year 			= document.getElementById("epc_year");


	if (epc_my != null) {

		var epc_make_text = "";
		epc_make_text = epc_make.options[0].text;
		epc_make.options[0].text = "Loading Makes...";

		$.get("/ajax/?ac=gms", {},
		function(makes){
			makes = $.trim(makes);
			makes = makes.split("@");
			epc_make.options.length=1;
			for (i=0; i<makes.length; i++) {
				epc_make.options[i+1] = new Option(makes[i], makes[i]);
			}
			epc_make.disabled = false;
			epc_make.options[0].text = epc_make_text;
		}
		);

		$(epc_make).change( function() {
			makeChange(this.value);
		});
		$(epc_year).change( function() {
			yearChange(epc_make.value, this.value);
		});


		if (!epc_year.disabled) {
			epc_year.disabled = true;
		}

		$(epc_my).submit( function() {
			return my_submit();
		});

		$(epc_my).attr("action", "/ym/");

	}

};

function makeChange(make) {

	var epc_make 			= document.getElementById("epc_make");
	var epc_year 			= document.getElementById("epc_year");


	if (!epc_year.disabled) {
		epc_year.disabled = true;
		epc_year.options.length=1;
	}

	if (epc_make.value != make) {
		epc_make.value = make;
	}

	if (make!="") {
		getYearsByMake(make);
	}

};

function yearChange(make, year) {

	var epc_my			 	= document.getElementById("epc_my");
	var epc_year 			= document.getElementById("epc_year");


	if(epc_year.value != year) {
		epc_year.value = year;
	}

	if (year!="") {
		$(epc_my).submit();
	}

};


function getYearsByMake(make) {

	var epc_year 			= document.getElementById("epc_year");

	var epc_year_text = "";
	epc_year_text = epc_year.options[0].text;
	epc_year.options[0].text = "Loading Years...";

	$.get("/ajax/?ac=gybm", { make: make },

	function(years){

		years = $.trim(years);
		years = years.split("@");

		epc_year.options.length=1;

		for (i=0; i<years.length; i++) {
			epc_year.options[i+1] = new Option(years[i], years[i]);
		}

		epc_year.disabled = false;
		epc_year.options[0].text = epc_year_text;

	}
	);

};


function my_submit() {

	var epc_my			 	= document.getElementById("epc_my");
	var epc_year 			= document.getElementById("epc_year");
	var epc_make 			= document.getElementById("epc_make");

	var year_select = true;
	var make_select = true;


	if (epc_year.value=="") {
		year_select = false;
	}
	if (epc_make.value=="") {
		make_select = false;
	}

	if (year_select && make_select) {
		$(epc_my).attr("action", "/ym/" + epc_year.value + '/' + epc_make.value.toLowerCase().replace(' ', '_') + '.html');
		//$(epc_year).remove();
		//$(epc_make).remove();
		return true;
	}
	if (!year_select && !make_select) {
		alert("You must select a Make and Year to continue!");
		return false;
	}
	if (!year_select && make_select) {
		alert("You must select a Year to continue!");
		return false;
	}

};

jQuery.fn.nospam = function(settings) {
	settings = jQuery.extend({
		replaceText: false, 	// optional, accepts true or false
		filterLevel: 'normal' 	// optional, accepts 'low' or 'normal'
	}, settings);

	return this.each(function(){
		e = null;
		if(settings.filterLevel == 'low') { // Can be a switch() if more levels added
			if($(this).is('a[rel]')) {
				e = $(this).attr('rel').replace('//', '@').replace(/\//g, '.');
			} else {
				e = $(this).text().replace('//', '@').replace(/\//g, '.');
			}
		} else { // 'normal'
			if($(this).is('a[rel]')) {
				e = $(this).attr('rel').split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			} else {
				e = $(this).text().split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			}
		}
		if(e) {
			if($(this).is('a[rel]')) {
				$(this).attr('href', 'mailto:' + e);
				if(settings.replaceText) {
					$(this).text(e);
				}
			} else {
				$(this).text(e);
			}
		}
	});
};

function popWindow(url, windowname, width, height) {
	window.open(url, '', 'width='+width+',height='+height);
}
function popOffer(url) {
	
	popwidth = screen.availWidth * .80;
	popheight = screen.availHeight * .80;
	
	
	function getpos(){
		leftpos=(detectexist(window.screenLeft))? screenLeft+document.body.clientWidth/2-popwidth/2 : detectexist(window.screenX)? screenX+innerWidth/2-popwidth/2 : 0
		toppos=(detectexist(window.screenTop))? screenTop+document.body.clientHeight/2-popheight/2 : detectexist(window.screenY)? screenY+innerHeight/2-popheight/2 : 0
		if (window.opera){
			leftpos-=screenLeft
			toppos-=screenTop
		}
	}
	getpos()
	
	var winattributes='width='+popwidth+',height='+popheight+',toolbar=no,location=no,resizable=no,status=no,menubar=no,scrollbars=yes,left='+leftpos+',top='+toppos
	
	window.open(url, '', winattributes);
}

function sameShipping() {
	document.cartinfo.shipping_name.value           	= document.cartinfo.billing_fname.value + " " + document.cartinfo.billing_lname.value;
	document.cartinfo.shipping_street.value     			= document.cartinfo.billing_street.value;
	document.cartinfo.shipping_street2.value     			= document.cartinfo.billing_street2.value;
	document.cartinfo.shipping_city.value           	= document.cartinfo.billing_city.value;
	document.cartinfo.shipping_state.value          	= document.cartinfo.billing_state.value;
	document.cartinfo.shipping_country.selectedIndex	= document.cartinfo.billing_country.selectedIndex;
	document.cartinfo.shipping_zipcode.value       		= document.cartinfo.billing_zipcode.value;
	document.cartinfo.shipping_telephone.value				=	document.cartinfo.billing_telephone.value;
}

$(function() {
	$(".tml,.fml").each(function() {
		$(this).click(function () {
			window.location.href = "/" + $(this).attr("id");
		});
		$(this).hover(
		function() {
			$(this).css("text-decoration", "underline");
		}
		,
		function() {
			$(this).css("text-decoration", "none");
		}
		);
	});
});

function load_epc_search2() {
	
	var fpsearch = document.getElementById("epc_search2");
 	var psearch = document.getElementById("epc_product2");
 	
 	if (fpsearch != null) {
 	
	  	var ptext = 'Part # Search';
	  
	  	$(fpsearch).attr("action", "/search/");
	  	
	  	$(psearch).focus(function() { 
	  		var psearch = document.getElementById("epc_product2");
	  		if ($.trim(psearch.value) == ptext) {
	  			psearch.value = '';
	  			$(psearch).css('color', 'black');
			}
	  	});
	  
	  	$(psearch).blur(function() { 
	  		var psearch = document.getElementById("epc_product2");
	  		if ($.trim(psearch.value).length=="") {
	  			psearch.value = ptext;
	  			$(psearch).css('color', 'gray');
	  		}
	  	});
	  
	  	if ($.trim(psearch.value).length==0) {
	  		psearch.value = ptext;
	  		$(psearch).css('color', 'gray');
	  	}
	  
	  	$(fpsearch).submit( function() {
	    	var psearch = document.getElementById("epc_product2");
	    	
	    	if ($.trim(psearch.value).length==0 || $.trim(psearch.value) == ptext) {
	        	alert("Please enter a part number");
	        	return false;   
	    	}
	    	psearch.value = $.trim(psearch.value);
	    	return true;
	  	});
 	}
}
