﻿function changeStockLevel() {
    ddForm = document.getElementById('VariantDD');
    DDArray = ddForm.value.split(' _ ');
    if (document.getElementById('defaultvariantid') != null) {document.getElementById('defaultvariantid').value = DDArray[0];}; 
    document.getElementById('availability_value').innerHTML = DDArray[1];
}

function swapVariantImage() {

    ddForm = document.getElementById('VariantDD');
    DDArray = ddForm.value.split(' _ ');
    document.getElementById('product-full').src = "http://images.buyitsellit.com/" + DDArray[2] + "_290.jpg";

}

function setOptions() {
    
    if (trackinventory == true) {
        var sOpt1, sOpt2, sOpt3, sOpt4, sOpt5 = '';
        if (document.getElementById('btn_addtocart') != null) { sOpt1 = document.getElementById('btn_addtocart').disabled = true; };
        if (document.getElementById('opt1') != null) { sOpt1 = unescape(document.getElementById('opt1').value) } else { sOpt1 = 'null' };
        if (document.getElementById('opt2') != null) { sOpt2 = unescape(document.getElementById('opt2').value) } else { sOpt2 = 'null' };
        if (document.getElementById('opt3') != null) { sOpt3 = unescape(document.getElementById('opt3').value) } else { sOpt3 = 'null' };
        if (document.getElementById('opt4') != null) { sOpt4 = unescape(document.getElementById('opt4').value) } else { sOpt4 = 'null' };
        if (document.getElementById('opt5') != null) { sOpt5 = unescape(document.getElementById('opt5').value) } else { sOpt5 = 'null' };
 
        var hasmatch = false
        var variantid = '-1;'
        var stocklevel = 'Not in stock'
        var price = ''
        var saleprice = 'null'
        var assignedimageid = 'null'

        //alert(sOpt2 == VariationList[1].opt1);
        //alert(sOpt3 + ':' + VariationList[0].opt3);//
        for (var i = 0; i < VariationList.length; i++) {

            if (sOpt1 == VariationList[i].opt1.replace("&amp;", "&") && sOpt2 == VariationList[i].opt2.replace("&amp;", "&") && sOpt3 == VariationList[i].opt3.replace("&amp;", "&") && sOpt4 == VariationList[i].opt4.replace("&amp;", "&") && sOpt5 == VariationList[i].opt5.replace("&amp;", "&") && VariationList[i].id != '-1') {
              
                hasmatch = true                
                variantid = VariationList[i].id;
                stocklevel = VariationList[i].stocklevel;
                price = VariationList[i].price;
                saleprice = VariationList[i].saleprice;
                assignedimageid = VariationList[i].assignedimageid;
            }

        }
        //alert(hasmatch);
        if (hasmatch == true) {
            if (stocklevel == null || stocklevel <= 0) {
                    document.getElementById('availability_value').innerHTML = 'not in stock';
                    if (document.getElementById('btn_addtocart') != null) { document.getElementById('btn_addtocart').disabled = true; document.getElementById('btn_addtocart').value = 'not in stock'; };
                    document.getElementById('defaultvariantid').value = variantid;

            } else {
                if (document.getElementById('product-full') != null && assignedimageid != '0') {
                    document.getElementById('product-full').src = "http://images.buyitsellit.com/" + assignedimageid + "_290.jpg";
                }
                document.getElementById('availability_value').innerHTML = stocklevel + ' in stock';
                //Set The Variant ID
                document.getElementById('defaultvariantid').value = variantid;

                //Set The Price
                if (saleprice != 'null') {
                    document.getElementById('optionprice').innerHTML = '<s>' + price + '</s> ' + saleprice;
                }
                else {
                    document.getElementById('optionprice').innerHTML = price;
                }

                if (document.getElementById('btn_addtocart') != null) { sOpt1 = document.getElementById('btn_addtocart').disabled = false; document.getElementById('btn_addtocart').value = 'add to cart'; }

            }

        }
        else {
            document.getElementById('availability_value').innerHTML = 'not in stock';
            if (document.getElementById('btn_addtocart') != null) { document.getElementById('btn_addtocart').disabled = true; document.getElementById('btn_addtocart').value = 'not in stock'; };
            document.getElementById('defaultvariantid').value = variantid;
        }
    }
    else {
        price = VariationList[0].price;
        saleprice = VariationList[0].saleprice;
        //Set The Price
        if (saleprice != 'null') {
            document.getElementById('optionprice').innerHTML = '<s>' + price + '</s> ' + saleprice;
        }
        else {
            document.getElementById('optionprice').innerHTML = price;
        }    
           
        document.getElementById('availability_value').innerHTML = "in stock";
        document.getElementById('defaultvariantid').value = defaultvariantid;
    }
};

function emailSubscribe() {

    var sEmail;
    var sStoreID;

    if (document.getElementById('emailsub_emailaddress') != null) { sEmail = document.getElementById('emailsub_emailaddress').value };
    if (document.getElementById('emailsub_storeid') != null) { sStoreID = document.getElementById('emailsub_storeid').value };


    new Ajax.Request('/ajaxresponses/emailSubscribe.aspx?email=' + sEmail + '&storeid=' + sStoreID, {
        onComplete: function(transport) {

            if (200 == transport.status) {
                if (document.getElementById('emailsub_wrapper') != null) { document.getElementById('emailsub_wrapper').innerHTML = '<br/>Thank you for subscribing!' };
            }

            if (500 == transport.status) {
                alert(transport.responseText);
            }

        }
    });
};

function addToCart(formid) {
    window.status='Add product to cart.';
    document.getElementById('productform_' + formid).submit();

}

function changePage(page) {
    var pageArray
    pageArray = page.split(' _ ');
    location = pageArray[0] + '&pagesize=' + pageArray[1];

}

function showReviewForm() {
    document.getElementById('writereviewbutton').style.display = 'none';
    document.getElementById('reviewlist').style.display = 'none';
    document.getElementById('reviewform').style.display = 'block';
}

function hideReviewForm() {
    document.getElementById('writereviewbutton').style.display = 'block';
    document.getElementById('reviewlist').style.display = 'block';
    document.getElementById('reviewform').style.display = 'none';
}

function submitReviewForm() {
    var isValid = true;
    if (isValid && document.getElementById('revrating').value == '') { isValid = false; alert('Please select a rating.'); };
    if (isValid && document.getElementById('revtitle').value == '') { isValid = false; alert('Please enter a title for your review.'); };
    if (isValid && document.getElementById('revtext').value == '') { isValid = false; alert('Please enter some text for your review'); };
    if (isValid && document.getElementById('recaptcha_response_field').value == '') { isValid = false; alert('Please enter the code shown.'); };

    if (isValid) {
        //document.reviewform.submit();

        new Ajax.Request('/product/submitreview', {
            method: 'post',
            parameters: { action: 'postreview', productid: $('productid').value, revrating: $('revrating').value, revtitle: $('revtitle').value, revtext: $('revtext').value, revfromname: $('revfromname').value, recaptcha_response_field: $('recaptcha_response_field').value, recaptcha_challenge_field: $('recaptcha_challenge_field').value },
            onComplete: function(transport) {

                if (200 == transport.status) {
                    document.getElementById('reviewcomplete').style.display = 'block';
                    document.getElementById('reviewform').style.display = 'none'; 
                }

                if (500 == transport.status) {
                    alert("You've entered an invalid code.");
                }

            }
            
        });
    }    
}


