function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function moneyFilter(key, textbox) {			
	var dFilterNum = textbox.value;
	var period = 0;
	if(dFilterNum.indexOf(".") != -1) {
			period++;
	}	
	if(key == 9) {
        return true;
    }
	else if(key == 8 && textbox.value.length != 0) {
		if(dFilterNum.indexOf(".") == (dFilterNum.length-1)) {
			period--;
		}
		dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);		
	}
	if(dFilterNum.indexOf(".") != -1 && (dFilterNum.length - dFilterNum.indexOf(".")) == 3) {
		return false;
	}	
 	else if((key > 47 && key < 58) || (key > 95 && key < 106) || key == 110  || key == 190) {
		switch(key) {
            case 96:
                dFilterNum += 0;
                break;
            case 97:
                dFilterNum += 1;
                break;
            case 98:
                dFilterNum += 2;
                break;
            case 99:
                dFilterNum += 3;
                break;
            case 100:
                dFilterNum += 4;
                break;
            case 101:
                dFilterNum += 5;
                break;
            case 102:
                dFilterNum += 6;
                break;
            case 103:
                dFilterNum += 7;
                break;
            case 104:
                dFilterNum += 8;
                break;
            case 105:
                dFilterNum += 9;
                break;
			case 110:
			case 190:
				if(period == 0) {
					dFilterNum += ".";
					//period++;
				}				
				break;
            default:
                dFilterNum += String.fromCharCode(key);
                break;
		}
	}    
    textbox.value = dFilterNum;
    return false;
}

function showImg(id) {
	document.getElementById("tn" + id).style.display = "block";
}

function hideImg(id) {
	document.getElementById("tn" + id).style.display = "none";
}

function changePage(page) {
	var url = window.location + "";
	if(url.indexOf("page=") != -1) {
		var right = url.substring(url.indexOf("page="), url.length);
		right = right.substring(right.indexOf("&"), url.length);
		var left = url.substring(0, url.indexOf("page=")) + "page=";
		url = left + page + right;
	}
	else {
		url += "?page=" + page;	
	}
	window.location = url;
}

function writeEmail(name, domain, ext) {
	var email = name + "@" + domain + "." + ext;
	document.write('<a href="mailto:' + email + '" title="' + email + '">' + email + '</a>');
}

function ajaxload(url, containerid, condition) {
	try { // Browser object detection
		
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");		
	}
	catch (e){alert(e.description)} // browser doesn't support ajax. handle however you want
	xmlhttp.onreadystatechange = function(){statuschanged(containerid,condition)}; // every time ready status changes, statuschanged() function executes
	xmlhttp.open("GET", url, true); // Usage: open(HTTP method, url, and asynchronous = true or false)
	xmlhttp.send(null); // send the request.
}

function statuschanged(containerid, condition){
	if((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { // readyState codes: 0=Uninitialised 1=Loading 2=Loaded 3=Interactive 4=Completed; status code: 200=OK
		if(xmlhttp.responseText != "") {
			if(condition == "h") { // populating a hidden field
				document.getElementById(containerid).value = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}
			else if(condition == "a") { // if condition is append
				document.getElementById(containerid).innerHTML += xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}
			else {
				document.getElementById(containerid).innerHTML = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}			
		}
	}
	else { // else, readyState is not yet Completed and status is not yet OK, so here we display loading text
		document.getElementById(containerid).innerHTML = "Loading...";		
	}
}

function isIE() {
	if(navigator.userAgent.indexOf("MSIE") >= 0) {		
		return true;
	}
}

function viewItem(id) {
	if(!isIE()) {
		var width = 400;
		var height = 400;
		var url = "item.php?id=" + id;
		var popup = window.open(url,'Item','channelmode=no,directories=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height);
		popup.moveTo((window.screen.availWidth - width) / 2, (window.screen.availHeight - height) / 2);
		popup.focus();
	}
	else {
		parent.document.getElementById("item" + id).click();
	}
}

function showExtraInfo(user) {
	document.getElementById("extraInfo").style.display = "none";
	if(user == "1") {
		document.getElementById("extraInfo").style.display = "block";
	}
}

function countryChanged(country) {
	document.getElementById("ddlStateId").style.display = "inline";
	document.getElementById("txtState").style.display = "none";
	if(trim(country.toLowerCase()) != "united states") {
		document.getElementById("ddlStateId").style.display = "none";
		document.getElementById("txtState").style.display = "inline";
	}
}

function shipped(chk) {
	if(!confirm("Are you sure this order was shipped?")) {
		chk.checked = false;
		return;
	}
	document.getElementById("iframe").src = "orderShipped.php?id=" + chk.value;
}

function writeSwf(file,id,width,height,version,bgcolor,loop) {
    var html = 
        '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
        'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + ',0,0,0" ' +
        'width="' + width + '" height="' + height + '" id="top" align="middle">' +
        '<param name="allowScriptAccess" value="sameDomain" />' +
        '<param name="movie" value="' + file + '" />' +
        '<param name="quality" value="high" />' +
        '<param name="wmode" value="transparent" />' +
		'<param name="scale" value="noscale" />' +
	    '<param name="loop" value="' + loop + '" />' +
        '<param name="bgcolor" value="' + bgcolor + '" />' +
        '<embed src="' + file + '" quality="high" wmode="transparent" bgcolor="' + bgcolor + '" ' +
        'width="' + width + '" height="' + height + '" name="top" align="middle" allowScriptAccess="sameDomain" ' +
        'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
        '</object>';
    document.getElementById(id).innerHTML = html;
}

function writeVideoSwf(file,id,width,height,version,bgcolor,loop) {
    var html = 
        '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
        'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + ',0,0,0" ' +
        'width="' + width + '" height="' + height + '" id="top" align="middle">' +
        '<param name="allowScriptAccess" value="sameDomain" />' +
        '<param name="movie" value="' + file + '" />' +						
        '<param name="quality" value="high" />' +
        '<param name="wmode" value="transparent" />' +
		'<param name="scale" value="noscale" />' +
	    '<param name="loop" value="' + loop + '" />' +
        '<param name="bgcolor" value="' + bgcolor + '" />' +
        '<param name="FlashVars" value="flvURL=' + file +'.flv&ControlPanelColor=0xfffbf0&buttonColor=0x000000&ControlPanelPosition=0&showControlPanel=1&ShowtimeColor=0xfffbf0&bAutoPlay=1&bAutoRepeat=0&BufferTime=5&tmeColor=0xfffbf0&loaColor=0x666666&GroundColor=0x000000&conNum=1" />' +
		'<param name="salign" value="lt" />' +
		'<embed src="' + file + '.swf" width="' + width + '" height="' + height + '" align="middle" quality="high" bgcolor="' + bgcolor + '" scale="noscale" salign="lt" FlashVars="flvURL=' + file + '.flv&ControlPanelColor=0xfffbf0&buttonColor=0x000000&ControlPanelPosition=0&showControlPanel=1&ShowtimeColor=0xfffbf0&bAutoPlay=1&bAutoRepeat=0&BufferTime=5&tmeColor=0xfffbf0&loaColor=0x666666&GroundColor=0x000000&conNum=1" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    document.getElementById(id).innerHTML = html;
}

function changeVideo(id) {
	writeVideoSwf("/Videos/video" + id, "video", "360", "277", "8", "#000000", "false");
}

function populateInfo(show, page) {
	var suffix = "";
	if(page == "checkout") {
		if(document.getElementById("txtAddress1").value == "") {
			suffix = "2";
		}
	}
	
	var firstName = document.getElementById("txtBillingFirstName");
	var lastName = document.getElementById("txtBillingLastName");
	var address1 = document.getElementById("txtBillingAddress1");
	var address2 = document.getElementById("txtBillingAddress2");
	var city = document.getElementById("txtBillingCity");
	var state = document.getElementById("ddlBillingStateId");
	var postalCode = document.getElementById("txtBillingPostalCode");
	
	firstName.value = "";
	lastName.value = "";
	address1.value = "";
	address2.value = "";
	city.value = "";
	state[0].selected = true;
	postalCode.value = "";
	
	if(show) {
		firstName.value = document.getElementById("txtFirstName" + suffix).value;
		lastName.value = document.getElementById("txtLastName" + suffix).value;
		address1.value = document.getElementById("txtAddress1" + suffix).value;
		address2.value = document.getElementById("txtAddress2" + suffix).value;
		city.value = document.getElementById("txtCity" + suffix).value;
		for(var i=0; i<state.length; i++) {
			if(state[i].value == document.getElementById("ddlStateId" + suffix).value) {
				state[i].selected = true;
			}
		}
		postalCode.value = document.getElementById("txtPostalCode" + suffix).value;
	}
}

function validatePhone(obj) {
	if(obj.value == "() " || obj.value == "() -" || obj.value.length < 14) {
		obj.value = "";
	}
}

function validateCreditCard(obj) {
	if(obj.value == "---" || obj.value.length < 19) {
		obj.value = "";
	}
}

function selectCardType(txt) {
	var ddl = document.getElementById("ddlCardType");
	for(var i=0; i<ddl.length; i++) {
		if(ddl[i].text == txt) {
			ddl[i].selected = true;
		}
	}
}