//  Function to retrieve data returned by 'url' during the product upload (admin)
//  - uses the function defined by 'userDisplayFunction' to format output
//  - populates the document element defined by htmlelementid   
function ajaxProductUploadRead(htmlelementid, url, userDisplayFunction, userDisplayFunctionExtra, htmlelementidextra){

    var original_url = url;

    var url = url + '&' + 'random=' + Math.random();

    // Display rotating wait circle
    document.getElementById('imgCSVupload').innerHTML="<img src='/kernel/images/progress_loading.gif'/>"; 

    ajaxRead(htmlelementid, url, userDisplayFunction, userDisplayFunctionExtra, htmlelementidextra);

    setTimeout("ajaxProductUploadRead('" + htmlelementid + "','" + original_url + "','" + userDisplayFunction + "','" + userDisplayFunctionExtra + "','" + htmlelementidextra + "')", 3500);
}


//  Function to retrieve data returned by 'url'
//  - uses the function defined by 'userDisplayFunction' to format output
//  - populates the document element defined by htmlelementid   
function ajaxRead(htmlelementid, url, userDisplayFunction, userDisplayFunctionExtra, htmlelementidextra){
    var xmlObj = null;

    var url = url + '&' + 'random=' + Math.random();

    if(window.XMLHttpRequest){
        xmlObj = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return;
    }

    var wait_text = "Please wait...";

    xmlObj.onreadystatechange = function() {
        if (xmlObj.readyState == 4){
            eval(userDisplayFunction + "(htmlelementid, xmlObj.responseText)" + ";");
            if (userDisplayFunctionExtra && htmlelementidextra) {
                eval(userDisplayFunctionExtra + "(htmlelementidextra, xmlObj)" + ";");
                global_response = xmlObj.responseText;
            }
        } else {
            eval(userDisplayFunction + "(htmlelementid, wait_text)" + ";");
        }
    }

    xmlObj.open ('GET',url, true);
    xmlObj.send ('');
    
}

// use this when you don't want the Please wait... text to appear
function ajaxReadNoProgress(htmlelementid, url, userDisplayFunction, userDisplayFunctionExtra, htmlelementidextra){
    var xmlObj = null;

    var url = url + '&' + 'random=' + Math.random();

    if(window.XMLHttpRequest){
        xmlObj = new XMLHttpRequest();
    } else if(window.ActiveXObject){
        xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return;
    }

    var wait_text = "";

    xmlObj.onreadystatechange = function() {
        if (xmlObj.readyState == 4){
            eval(userDisplayFunction + "(htmlelementid, xmlObj.responseText)" + ";");
            if (userDisplayFunctionExtra && htmlelementidextra) {
                eval(userDisplayFunctionExtra + "(htmlelementidextra, xmlObj)" + ";");
                global_response = xmlObj.responseText;
            }
        }  else {
            eval(userDisplayFunction + "(htmlelementid, wait_text)" + ";");
        }
    }
    
    xmlObj.open ('GET',url, true);
    xmlObj.send ('');
    
} 

//  custom function for the shipping estimate that simply inserts data into the element defined by 'obj' on the user's html page
function updateShippingEstimateText(obj, data){
    var text = "<b>" + data + "</b>";
    document.getElementById(obj).innerHTML=text; 
}

//  custom function for the shipping estimate that simply inserts any extra data into the element defined by 'obj' on the user's html page
//  the xmlObj is also sent in so that the responseText can be parsed to ensure that there are no errors
function ShippingEstimateTextExtra(obj, xmlObj){
	var text = 'These costs are estimates based on shipping methods available to your ZIP code. You will be able to choose your shipping method later in the checkout process. Free Shipping offers and other discounts will also be shown later.';
    var errorRegExp = /error/i;
    if (!errorRegExp.test(xmlObj.responseText)) { 
        document.getElementById(obj).innerHTML=text; 
    }
}

//  custom function for the email me when this is in stock that simply inserts data into the element defined by 'obj' on the user's html page
function updateEmailText(obj, data){
    var text = data;
    document.getElementById(obj).innerHTML=text; 
}

//  custom function to update the marvel breadcrumb promo area to bypass the squid through an Ajax call
function updateMarvelPromoArea(obj, data){
    document.getElementById(obj).innerHTML=data;
}

function get_cookie (cookie_name) {
    var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

    if ( results ) {
        return ( unescape ( results[1] ) );
    } else {
        return null;
    }
}

function checkSKUOptionSelected(optionName) {
	if (document.getElementById('sku_option_id').value == "") {
		alert("Please select the " + optionName.toLowerCase()+ " you would like to add to your shopping cart.");
		return false;
	}
}
