// We want the AJAX function to have as much time to set cookie in place as possible, so we will call it immediately after function declarations below.
//addLoadListener(set_domestic);

function is_domestic(){
        var is_domestic = readCookie('is_domestic');
        if(is_domestic == null)
                return true;
        else if(is_domestic == 'true') return true;
        else return false;
}

function set_domestic(){
        var is_domestic = readCookie('is_domestic');
        if(is_domestic == null)
                asyncGet('/getcountry.php', 'handle_domestic', null);

}

function handle_domestic(response, arg){
        response = response.replace(/^\s+|\s+$/g,"");
        if(response == 'US' || response == 'CA' || response == 'JP' || response == 'AU' || response == 'NZ' || response == 'KR' || 
        	response == 'TW' || response == 'HK' || response == 'CL' || response == 'RU')
                createCookie('is_domestic', true, 30);
        else
                createCookie('is_domestic', false, 30);
}

// Lets call this immediately so the AJAX function will have more time to set the cookie
set_domestic();

function addLoadListener(fn) {

	if (typeof window.addEventListener != 'undefined') {

		window.addEventListener('load', fn, false);

	} else if (typeof document.addEventListener != 'undefined') {

		document.addEventListener('load', fn, false);

	} else if (typeof window.attachEvent != 'undefined') {

		window.attachEvent('onload', fn);

	} else {

		var oldfn = window.onload;

		if (typeof window.onload != 'function') {

			window.onload = fn;

		} else {

			window.onload = function() {

				oldfn();

				fn();

			};

		}

	}

}

/* this needs jquery */
function showDomesticInternationalContent() {
	var classname = '';
	
	if (is_domestic()) {
		classname = 'domestic_show';
	} else {
		classname = 'international_show';
	}
	
	var els = $('.' + classname);
	if (els) {
		els.css({display:''});
	}
}


function copyToElementInnerHtml_spyder(response, arg) {
        if(arg.from == 'search_product'){
                if(response.indexOf('Product not in stock') >= 0 || response.indexOf('Please select a size') >= 0)
                        $('#modalFindInStore-window').height('320px');
                else
                        $('#modalFindInStore-window').height('400px');


        }
        copyToElementInnerHtml(response, arg);
}

// this is killing me!! IE has problems with getElementSByName, 
// work around is to get by tag first, then filter through the list.
function getElementsByName_iefix(tag, name) {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
} 

function Comma(number) {
	number = '' + number;
	if (number.length > 6) {
		var output = number.substring(0, number.length-6);
		output = output + ',';
		output = output + number.substring(number.length-6)
		return (output);
	}
	else 
		return number;
}

// if response != -1, meaning user is logined, display the orig price, calculate the discounted price and display them 
function loadDiscountedPrices(arg){
	// response would be the percent off
	// arg should have the tag (ie: font) of the old and new price element to be displayed. and also the old and new price id
	// arg:tag, arg:old_price_id, arg:new_price_id
	var percent_off = readCookie('spyder_percent_off');
	
	// if percent_off cookie isn't set yet, we'll need to call the ajax function to set it before we continue
	// with the handleLoadDiscountedPrices() functionality.
	if (percent_off == null || percent_off == 'null' || percent_off <= 0){
		asyncGet(arg.context + '/templates/ajax/percent_off.jsp', 'addCookie', '');
		setTimeout(function(){handleLoadDiscountedPrices(arg);}, 2000);
		return;
	}else{
		handleLoadDiscountedPrices(arg);
	}
}	

function handleLoadDiscountedPrices(arg){
	var percent_off = readCookie('spyder_percent_off');
	if (percent_off != null && percent_off != '' && percent_off > 0){
		var dom = getElementsByName_iefix(arg.tag, arg.old_price_id);
		if (dom != null){
			for(i=0;i<dom.length;i++) {
				var list_price_range = dom[i].innerHTML; 
				list_price_range = list_price_range.replace(" ", "");
				list_price_range = list_price_range.replace(",", "");
				var delimiter_index = list_price_range.indexOf("-");
				if(delimiter_index > 0)
					min_price = list_price_range.substring(1, delimiter_index);
				else
					min_price = list_price_range.substring(1);
				//for old price, just get the minimum price and display it,
				var pr = min_price*1.0;
				pr = pr.toFixed(0);
				dom[i].innerHTML = '$'+Comma(pr);
				dom[i].style.display = '';
			}
		}
		
		dom = getElementsByName_iefix(arg.tag, arg.new_price_id);
		if (dom != null){
			for(i=0;i<dom.length;i++) {
				var list_price_range = dom[i].innerHTML; 
				list_price_range = list_price_range.replace(" ", "");
				list_price_range = list_price_range.replace(",", "");
				var delimiter_index = list_price_range.indexOf("-");
				if(delimiter_index > 0)
					min_price = list_price_range.substring(1, delimiter_index);
				else
					min_price = list_price_range.substring(1);
				
				// for discounted price, multiply old price by discount and display 
				var pr = min_price*(1.0-percent_off/100);
				pr = pr.toFixed(0);
				dom[i].innerHTML = ' | $'+ Comma(pr);
				dom[i].style.display = '';
			}
		}
	}
}
/* Focuses text cursor on the first error classed element, if none then the first element in general, of a form */
function focus_first(form_identifier){
    if($(form_identifier + " .error").length > 0){
            $(form_identifier + " .error:first").focus();
    }else{
            $(form_identifier + " input:first").focus();
    }
}
