// JavaScript Document
window.addEvent('domready', function() {

	/**
	 * That CSS selector will find all <a> elements with the
	 * attribute rel="boxed" 	 * and href starting with a #.
	 *
	 * The second argument sets additional options
	 */
	SqueezeBox.assign($$('a[rel=boxed]'), {
		size: {x: 500, y: 475}

	});


});


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function str_replace ( search, replace, subject ) {

    var i, k = '';
    var searchl = 0;
    var reg;

    var escapeRegex = function(s) {
        return s.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, '\\$1');
    };

    search += '';
    searchl = search.length;
    if (!(replace instanceof Array)) {
        replace = [replace];
        if (search instanceof Array) {
            // If search is an array and replace is a string,
            // then this replacement string is used for every value of search
            while (searchl > replace.length) {
                replace[replace.length] = replace[0];
            }
        }
    }

    if (!(search instanceof Array)) {
        search = [search];
    }
    while (search.length>replace.length) {
        // If replace has fewer values than search,
        // then an empty string is used for the rest of replacement values
        replace[replace.length] = '';
    }

    if (subject instanceof Array) {
        // If subject is an array, then the search and replace is performed
        // with every entry of subject , and the return value is an array as well.
        for (k in subject) {
            if (subject.hasOwnProperty(k)) {
                subject[k] = str_ireplace(search, replace, subject[k]);
            }
        }
        return subject;
    }

    searchl = search.length;
    for (i = 0; i < searchl; i++) {
        reg = new RegExp(escapeRegex(search[i]), 'gi');
        subject = subject.replace(reg, replace[i]);
    }

    return subject;
}

function pagechange(page){
window.location = page;
}

function ajaxredirect(){
var anch = self.document.location.hash.substring(1);
if(anch > ''){
pagechange(anch);
}
}

function urlencode( str ) {                                 
    var ret = str;
    ret = ret.toString();
    ret = encodeURIComponent(ret);
    ret = ret.replace(/%20/g, '+');
    return ret;
}


function addtocart(id,name,price){

var url = 'includes/js/ajax/addtocart.php?pid=' + id + '&name=' + urlencode(name) + '&price=' + urlencode(price);
createRequestObject('cartnum');
sendReq(url,'','',addtocart2(name));
}

function addtocart2(obj){
alert (obj + "\nHas been added to your cart");
}

function updatecart(obj){
document.getElementById('promo').value = '';
document.getElementById('promo').disabled = false;
document.getElementById('promoanswer').innerHTML = '';
document.getElementById('f_promo').value = '';

var ship = document.getElementById('sp_ship').innerHTML;
var Q = document.getElementById('Q_' + obj).value;
var QS = obj + "," + Q + "," + ship ;
createRequestObjectCart('sp_total');
sendReqCart('includes/js/ajax/updatecart.php','scid,quan,ship',QS,function upcn(){ createRequestObject('cartnum');
sendReq('includes/js/ajax/updatecart_nav.php','','',enb()); });
var PPr = document.getElementById('PPr_' + obj).value;
var PPrT = Q*PPr;
PPrT = PPrT.toFixed(2);
document.getElementById('PPrT_' + obj).value = PPrT;
document.getElementById('sp_PPrT_' + obj).innerHTML = PPrT;
if(Q == 0){
document.getElementById('T_' + obj).style.display = 'none';
}
shiptoB();
}

function ship_js(obj, quan){
var totwship = parseFloat(str_replace(",","",document.getElementById("sp_total").innerHTML));
if(obj == 'f'){
totwship = totwship - document.getElementById('f_ship').value;
document.getElementById('sp_ship').innerHTML = '0.00';
document.getElementById('f_ship').value = '0.00';
document.getElementById('shiptype').value = 'Free';
}
if(obj == 'p'){
totwship = totwship - document.getElementById('f_ship').value;
totwship = totwship + 6.95;
document.getElementById('sp_ship').innerHTML = '6.95';
document.getElementById('f_ship').value = '6.95';
document.getElementById('shiptype').value = 'USPS Priority';
}
if(obj == 'o'){
totwship = totwship - document.getElementById('f_ship').value;
totwship = totwship + 23.50;
document.getElementById('sp_ship').innerHTML = '23.50';
document.getElementById('f_ship').value = '23.50';
document.getElementById('shiptype').value = 'USPS Express';
}
if(obj == 'u'){
totwship = totwship - document.getElementById('f_ship').value;
totwship = totwship + 39.95;
document.getElementById('sp_ship').innerHTML = '39.95';
document.getElementById('f_ship').value = '39.95';
document.getElementById('shiptype').value = 'UPS Overnight';
}
if(obj == 'i'){
totwship = totwship - document.getElementById('f_ship').value;
if(quan==1){
totwship = totwship + 8.50;
document.getElementById('sp_ship').innerHTML = '8.50';
document.getElementById('f_ship').value = '8.50';
}else if(quan>1){
var newship=8.50 + ((quan-1)*3.5);	
totwship = totwship + newship;
document.getElementById('sp_ship').innerHTML = newship.toFixed(2);
document.getElementById('f_ship').value = newship.toFixed(2);	
}else if(quan==0){
totwship = totwship + 0;
document.getElementById('sp_ship').innerHTML = '0.00';
document.getElementById('f_ship').value = '0.00';		
}
document.getElementById('shiptype').value = 'International';
}
totwship = totwship.toFixed(2);
document.getElementById('sp_total').innerHTML = number_format(totwship,2,".",",");
enb();
}

function disb(){
document.getElementById('co').style.backgroundColor = '#CCCCCC';
document.getElementById('co').disabled = true;
}

function enb(){
document.getElementById('f_promo').value = document.getElementById('promo').value;
document.getElementById('f_ship').value = parseFloat(str_replace(",","",document.getElementById("sp_ship").innerHTML));
document.getElementById('f_total').value = parseFloat(str_replace(",","",document.getElementById("sp_total").innerHTML));
document.getElementById('co').style.backgroundColor = '#0033FF';
document.getElementById('co').disabled = false;
}


function promocode(){
var code = 	document.getElementById('promo').value;
var ship = parseFloat(str_replace(",","",document.getElementById("sp_ship").innerHTML));
var totwship = parseFloat(str_replace(",","",document.getElementById("sp_total").innerHTML));
if((totwship - ship) > 0){
var addship = 0;
if(ship > 0){
addship = 1;
}
var Q = code + "," + totwship;
var url = 'includes/js/ajax/promocodes.php?c=' + code + '&t=' +totwship + '&as=' + addship;
if(code > ''){
createRequestObjectPromo('no_dis');
sendReqPromo(url,'','',enb());	
}
}else{
document.getElementById('promoanswer').style.color = '#CC0000';
document.getElementById('promoanswer').innerHTML = 'Price is Zero';	
}
}

function VF_f_checkout(){
document.getElementById('f_promo').value = document.getElementById('promo').value;
document.getElementById('f_ship').value = parseFloat(str_replace(",","",document.getElementById("sp_ship").innerHTML));
document.getElementById('f_total').value = parseFloat(str_replace(",","",document.getElementById("sp_total").innerHTML));
document.f_checkout.submit();
}

