function loadGPtabs(target, section, subsection, model) {
	new Ajax.Request('/includes/custom_includes/e2_GetGPtabs.asp?section='+section+'&subsection='+subsection+'&model='+model+'&sid='+Math.random(), {
		method: 'get',
		onFailure: function(transport) {
			alert(transport.responseText);
			//alert('Error occurred !!\nPlease contact support.');
		}, 
		onSuccess: function(transport) {
			//alert(transport.responseText);
			if (transport.responseText != '') {
				$(target).update(transport.responseText);
			}
		}
	});
}
function addCartNoRefresh() {
	var url="includes/addtocart.asp";
	var h="ic="+document.frmAdditem.ic.value+"&type="+document.frmAdditem.type.value+"&EqCode="+document.frmAdditem.EqCode.value+"&qty="+document.frmAdditem.qty.value;
	new Ajax.Request(url, {
	 method: 'post',
	 parameters: h,
	 onFailure: function(transport) {
		 alert('Error during add to cart !!\nPlease contact support.');
	 }, 
	 onSuccess: function(transport) {
		 alert('Item added to cart');
	 }
	});
}
function popCountries() {
	new Ajax.Request('/includes/custom_includes/e2_GetDealerCountries.asp?sid='+Math.random(), {
		method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText != '') {
				$('divSelCountry').update(transport.responseText)
			}
		}
	});
}
function popStates(ctr) {
	$('divSelState').update('&nbsp;');
	$('divSelCity').update('&nbsp;');
	new Ajax.Request('/includes/custom_includes/e2_GetDealerStates.asp?ctr='+ctr+'&sid='+Math.random(), {
		method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText != '') {
				$('divSelState').update(transport.responseText);
				if(ctr == "UNITED STATES" || ctr == "CANADA" || ctr == "AUSTRALIA")
					document.getElementById("divSelState").style.display = '';
				else{
					document.getElementById("divSelState").style.display = 'none';
					popCities(document.getElementById('selCountry')[document.getElementById('selCountry').selectedIndex].value,document.getElementById('selState')[document.getElementById('selState').selectedIndex].value);
				}
			}
		}
	});
}
function popCities(ctr,sta) {
	$('divSelCity').update('&nbsp;');
	new Ajax.Request('/includes/custom_includes/e2_GetDealerCities.asp?ctr='+ctr+'&sta='+sta+'&sid='+Math.random(), {
		method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText != '') {
				$('divSelCity').update(transport.responseText);
			}
		}
	});
}
function popContDets(ctr,sta,cit) {
	new Ajax.Request('/includes/custom_includes/e2_GetDealerContDets.asp?ctr='+ctr+'&sta='+sta+'&cit='+cit+'&sid='+Math.random(), {
		method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText != '') {
				$('divContDets').update(transport.responseText);
			}
		}
	});
}
function nothing(){}
function findPosX(obj){
	var curleft = 0; 
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} 
	else 
		if (obj.x) curleft += obj.x;
	return curleft;
}
function findPosY(obj){
	var curtop = 0; 
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} 
	else 
		if (obj.y) curtop += obj.y;
	return curtop;
}
function popManufs(str) {
	var url;
	if (str != '')
		url = "/includes/custom_includes/getallmanufs.asp?" + str + "&sid="+Math.random();
	else
		url = "/includes/custom_includes/getallmanufs.asp?sid="+Math.random();
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText != '') {
				$('divManufs').update(transport.responseText);
			}
		}
	});
}
function popBrands(str) {
	var url;
	if (str != '')
		url = "/includes/custom_includes/getallbrands.asp?" + str + "&sid="+Math.random();
	else
		url = "/includes/custom_includes/getallbrands.asp?sid="+Math.random();
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			if (transport.responseText != '') {
				$('divBrands').update(transport.responseText);
			}
		}
	});
}
function loadSW(target, url, wid, hei, idAtt) {
	var playerVersion = swfobject.getFlashPlayerVersion();
	var output = playerVersion.major + "." + playerVersion.minor + "." + playerVersion.release ;

	var flashvars = {
	}; 
	var params = {     //Setup param tag elements for the flash object to embed in the page 
		allowScriptAccess:"Always",
		allowfullscreen:"true",
		menu:"false",
		quality:"high",
		scale:"noscale",
		salign:"LT",
		bgcolor:"#FFFFFF",
		wmode:"opaque"
	};  
	var attributes = {  //Setup object tag elements for the flash object to embed in the page
		id : idAtt
	};
	swfobject.embedSWF(url, target, wid, hei, output, "/includes/resources/expressInstall/expressInstall.swf", flashvars, params, attributes);  //Add Viewer to page into the myContent div element, at 600 x 600 pixels, checking for flash version 6,0,65 before allowing initialization to proceed, otherwise show the default div content
}
function ytVdoPlay(id, ht, wd) {

	 var html  = '';

	 html += '<object height="'+ht+'" width="'+wd+'">';
	 html += '<param name="movie" value="http://www.youtube.com/v/'+id+'&amp;rel=0&amp;hl=en_US&amp;fs=1"> </param>';
	 html += '<param name="wmode" value="transparent"> </param>';
	 html += '<param name="allowfullscreen" value="true"> </param>';
	 html += '<embed src="http://www.youtube.com/v/'+id+'&amp;rel=0&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" wmode="transparent" height="'+ht+'" width="'+wd+'" allowfullscreen="true"></embed>';
	 html += '</object>';
	
	 return html;
	 
}

function getYtInfo(ytid){
	$j.getJSON('http://gdata.youtube.com/feeds/api/videos?q=' + ytid + '&alt=json-in-script&callback=?', function(data) {
		$j.each(data.feed.entry, function(i, item) {
			var title = item['title']['$t'];
			var content = item['content']['$t'];
			var video = item['id']['$t'];
			var Duration =  item['media$group']['yt$duration']['seconds'];
			var Min = Math.floor(Duration/60);
			var Seconds = (Duration % 60).toFixed().pad(2, "0");
			Duration = Min + ":" + Seconds;
			var Views =  item['yt$statistics']['viewCount'];
			var vf1 = addCommas(Views.toString());
			video = video.replace('http://gdata.youtube.com/feeds/api/videos/','http://www.youtube.com/watch?v=');  //replacement of link
			videoID = video.replace('http://www.youtube.com/watch?v=',''); // removing link and getting the video ID
			$j('#' + ytid + '').append('<span class="video-thumb ux-thumb-94" id="video-thumb-TgUU4R6JOuM-9448902"><span class="img"><img src="http://img.youtube.com/vi/' + ytid + '/2.jpg" width="100px" height="70px" border="0px" title="' + title + '"/></span><span class="video-time">' + Duration + '</span></span>');
			$j('#' + ytid + '_info').append('<span style="font-weight:bold; text-decoration:underline; text-align:left;">'+title+'</span><p style="text-align:left; font-weight:normal;">'+content+' ('+Min+'m '+Seconds+'s)<br /><strong>'+vf1+' Views</strong></p>')
		});
	});
}
function getYtPopInfo(ytid){
	$j.getJSON('http://gdata.youtube.com/feeds/api/videos?q=' + ytid + '&alt=json-in-script&callback=?', function(data) {
		$j.each(data.feed.entry, function(i, item) {
			var title = item['title']['$t'];
			var content = item['content']['$t'];
			var video = item['id']['$t'];
			var Duration =  item['media$group']['yt$duration']['seconds'];
			var Min = Math.floor(Duration/60);
			var Seconds = (Duration % 60).toFixed().pad(2, "0");
			Duration = Min + ":" + Seconds;
			var Views =  item['yt$statistics']['viewCount'];
			var vf1 = addCommas(Views.toString());
			video = video.replace('http://gdata.youtube.com/feeds/api/videos/','http://www.youtube.com/watch?v=');  //replacement of link
			videoID = video.replace('http://www.youtube.com/watch?v=',''); // removing link and getting the video ID
			$j('#ytVideoHead').html('<h3>'+title+'</h3>');
			$j('#ytVideoInfo').html('<p style="text-align:left; font-weight:normal;">'+content+' ('+Min+'m '+Seconds+'s)<br /><strong>'+vf1+' Views</strong></p>');
		});
	});
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

String.prototype.pad = function(l, s){
	return (l -= this.length) > 0
		? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0, s.length) + this + s.substr(0, l - s.length)
		: this;
};

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


$j(document).ready(function() {
	$j('.tnmyaccount').click(function() { location.href = '/accinfo.asp'; });
	$j('.tnordtrack').click(function() { location.href = '/ordtrack.asp'; });
	$j('.tncart').click(function() { location.href = '/viewcart.asp'; });
	$j('.tnlogin').click(function() { location.href = '/acclogin.asp'; });
	$j('.tnlogout').click(function() { location.href = '/logout.asp'; });
	$j('.tnSearchSubmit > a').click(function() { e2Pub.submitMainsearch(); return false; });
});
