// This is a simple function that trims leading and trailing spaces in a string.
// Usage is: stringObject.trim()
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


// This will take two strings and the current value of a text field then determine whether it needs to be one value, the other, or remain the same.
reset_form_value = function(str1, str2, val) {
	if(str1 == val.value.trim())
		val.value = str2;
}


// Submits a form.
formSubmit = function(f_name) {
	document.f_name.submit();
}


// Accepts arrays for ID and classes and will apply them in turn.
swapClass = function(ids, classes) {
	ids = ids.split(',');
	classes = classes.split(',');
	for(var i = 0; i < ids.length; i++) {
		document.getElementById(ids[i].trim()).className = classes[i].trim();
	}
}

//performs the search for the 'not found' page:
function doNotFoundSearch() {
	document.prod_search.q.value = document.prod_search2.ps_text.value;
	document.prod_search.submit();
}

// Toggles element display. Can do multiple elements at once if IDs are comma delimited.
toggleDisplay = function(show, hide) {
	if(show.length > 0) {
		show = show.split(',');
	}
	if(hide.length > 0) {
		hide = hide.split(',');
	}
	for(var i = 0; i < show.length; i++) {
		document.getElementById(show[i].trim()).style.display = 'block';
	}
	for(var i = 0; i < hide.length; i++) {
		document.getElementById(hide[i].trim()).style.display = 'none';
	}
}


// Toggle tabbed menu items on and off.
toggleTabs = function(selected_item, menuid) {
	var show = new Array();
	var hide = new Array();
	navRoot = document.getElementById(menuid);
	for(var i = 0; i < navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if(node.nodeName == 'LI') {
			if(node.id == selected_item) {
				node.className = 'selected';
				show.push(node.id.split("_")[0]);
			} else {
				node.className = 'normal';
				hide.push(node.id.split("_")[0]);
			}
		}
	}
	toggleDisplay(show.join(','), hide.join(','));
}



// Try to keep people from adding items with no specs to the cart
check_cart = function() {
	form = document.getElementById('add_cart');
	qty = form.qty.value;
	error_arr = new Array();

	if(qty <= 0)
		error_arr.push('\t- You must order at least 1 item.');

	for(var i = 0; i < form.length; i++) {
		if(form.elements[i].name.indexOf('option_id') != -1) {
			if(form.elements[i].value == 'null' || form.elements[i].value == '') {
				error_arr.push('\t- Please make a selection for each option on this item.');
				break;
			}
		}
	}
	
	if(error_arr.length > 0) {
		alert('There was a problem processing your order:\n\n'+error_arr.join('\n'));
	} else {
		form.submit();
	}
}


// Remove items from the cart.
remove_item = function(id) {
	form = document.getElementById('remove_item_form');
	form.ciid.value = id;
	document.remove_item_form.submit();
}

// Remove promo code.
remove_promo = function(prid, apid) {
	form = document.getElementById('remove_promo');
	form.prid.value = prid;
	form.apid.value = apid;
	document.remove_promo.submit();
}


// Keep email safe from spammers.
function noSpam(user, domain, subject) {
	locationstring = "mailto:" + user + "@" + domain + "?subject=" + subject;
	window.location = locationstring;
}


// Download Custom templates
download_template = function() {
	var form = document.getElementById('download');
	var f = form.format.value;
	var s = form.styles.value;
	var err = new Array();
	switch(f) {
		case 'ai8':
			var e = '_AI8.ai';
		break;
		case 'cs3':
			var e = '_CS3.ai';
		break;
		case 'pdf':
			var e = '.pdf';
		break;
		default:
			err.push('Select a file format.');
		break;
	}
	if(s == '')
		err.push('Select a style.');
	
	if(err.length > 0)
		alert(err.join('\n'));
	else
		window.location = 'http://files.sugoi.com/custom/sugoi_custom_templates/'+s+'_client'+e;
}


// AJAX stuff for dealer page
var request = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
	request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e2) {
	request = false;
	}
}
@end @*/
if (!request && typeof XMLHttpRequest != 'undefined') {
	request = new XMLHttpRequest();
}

function fillSelect(country) {
	var x = document.createElement('option');
	var y = document.createTextNode('loading...');
	x.appendChild(y);
	document.getElementById('region').appendChild(x);
	
	var url = "/csa_ajax.php?csa=" + escape(country);
	request.open("GET", url, true);
	request.onreadystatechange = go;
	request.send(null);
}
	
function go() {
	if(request.readyState == 4) {
		if(request.status == 200) {
			var response = request.responseText;
			var list = document.getElementById('region');
			var regions = response.split('|');
			
			list.removeChild(list.childNodes[0]);
			
			list.disabled = regions.length < 2 ? true : false;
			for(i = 1; i < regions.length; i++) {
				var x = document.createElement('option');
				var y = document.createTextNode(regions[i]);
				x.appendChild(y);
				list.appendChild(x);
			}
		}
	}
}

function initCs() {
	var country = document.getElementById('country');
	var list = document.getElementById('region');
	country.onchange = function() {
		while(list.childNodes[0]) {
			list.removeChild(list.childNodes[0])
		}
		fillSelect(this.value);
	}
	if(country.value == '' || country.value == 'country...') {
		list.disabled = true;
	}
}


// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela
var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#FFFF33";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}

/*window.onload = function () {
	Fat.fade_all();
}
*/

/* pop window */
function popupWindow(pageURL,windowName,w,h,features)

{
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'width='+w+',height='+h+',top='+wint+',left='+winl+','+features
  var popupWindow = window.open( pageURL ,windowName,winprops);
  if (popupWindow)
  {
    popupWindow.focus();
  }
  else
  {
    msg = "A popup blocker may be preventing the new page from opening.\n"
    + "Click OK to load the new page in the current browser.\n"
    + "Click Cancel to return to the current page.\n";
    if (confirm(msg))
      top.location.href = pageURL;
  }
}

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[(function(e){return d[e]})];e=(function(){return'\\w+'});c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('7=5(14,b,w,h){2.A=14;2.b=b;2.l=w;2.n=h;2.H="";2.12=c.N.13.1f("?")[1]||"";2.F="1A 1h 1p 1j x 1W. <a 11=\'1l://1U.1m.1S/Y/1q/\'>1Q x 1b</a>.";2.K="<p>1r 1t x 1b? <a 11=\'?R=r&"+2.12+"\'>1K 1v.</a></p>";2.t=1a 1y();2.Q=16("R")};7.d.15=5(X,S){2.t[X]=S};7.d.D=5(){6 2.t};7.d.C=5(V){6 2.t[V]};7.d.y=5(){3 m="";u(3 E M 2.D()){m+="<1B 19=\\""+E+"\\" 1C=\\""+2.C(E)+"\\" />"}4(m==""){m=z}6 m};7.d.J=5(){3 8="";4(e.f&&e.f.g){8+="<Z 1E=\\"1F/Y\\" O=\\""+2.A+"\\" l=\\""+2.l+"\\" n=\\""+2.n+"\\" b=\\""+2.b+"\\"";u(3 I M 2.D()){8+=" "+I+"=\\""+2.C(I)+"\\""}8+="></Z>"}9{8+="<U 1I=\\"1J:1L-1N-1O-1P-1R\\" l=\\""+2.l+"\\" n=\\""+2.n+"\\" b=\\""+2.b+"\\">";2.15("O",2.A);4(2.y()!=z){8+=2.y()}8+="</U>"}6 8};7.d.1V=5(){3 s=1a 1X();u(3 G M 2.1c()){s.1e(G+"="+1M(2.1i(G)))}4(s.g>0){6 s.1n("&")}9{6 z}};7.d.B=5(j){4(10()||2.Q=="r"){4(j){c.T(j).W=2.J()}9{c.B(2.J())}}9{4(2.H!=""){c.N.1x(2.H)}9{4(j){c.T(j).W=2.F+""+2.K}9{c.B(2.F+""+2.K)}}}};5 10(){3 v=r;L=r;4(e.f&&e.f.g){u(3 i=0;i<e.f.g;i++){3 17=e.f[i];4(17.19.o("x")>-1){v=1d}}}9{1g("1k 1o 1s 1u: L = 1w(1z(\\"1D.1G.1\\"))","1H");v=L}6 v}5 16(P){3 q=c.N.13;3 k=q.o(P);3 18=(q.o("&",k)!=-1)?q.o("&",k):q.g;4(q.g>1&&k!=-1){6 q.1T(q.o("=",k)+1,18)}9{6""}}',62,122,'||this|var|if|function|return|QTObject|_a|else||id|document|prototype|navigator|plugins|length|||_e|_14|width|_8|height|indexOf|||false|_c|params|for|_f||QuickTime|getParamTags|null|mov|write|getParam|getParams|_9|altTxt|_d|redirect|_b|getHTML|bypassTxt|qtObj|in|location|src|_12|doDetect|detectqt|_6|getElementById|object|_7|innerHTML|_5|quicktime|embed|isQTInstalled|href|sq|search|_1|addParam|getQueryParamValue|_11|_15|name|new|Player|getVariables|true|push|split|execScript|content|getVariable|the|on|http|apple|join|error|requires|download|Already|resume|have|next|here|IsObject|replace|Object|CreateObject|This|param|value|QuickTimeCheckObject|type|video|QuickTimeCheck|VBScript|classid|clsid|Click|02BF25D5|escape|8C17|4B23|BC80|Download|D3488ABDDC6B|com|substring|www|getVariablePairs|Plugin|Array'.split('|'),0,{}))
