/* -- THESE THREE FUNCTIONS SHOULD BE (INLINED?) ONLY WHERE THEY ARE NEEDED -- */

//Configurable Function for Popups
function ConfigurablePopup(url,w,h,winName,scroll,resiz,topPos,leftPos)
{
	$.popup(url, winName, { "centered": 1, "width": w, "height": h, "left": leftPos, "top": topPos, "scrollbars": scroll, "resizable": resiz });
}

//Generic Function for Popups
function popup(url,w,h) {
	$.popup(url, "AEO", { "centered": 1, "width": w, "height": h });
}

// to limit characters in a textarea or text input field
// limitField : ID of form element to limit (string)
// limitCount : ID of element to place characters remaining count (string)
// limitNum : the maximum number of characters to allow in field (int)
function limitChars(limitField, limitCount, limitNum) {
	limitField = $('#'+limitField);
	limitCount = $('#'+limitCount);
	if (limitField.val().length > limitNum) {
		limitField.val(limitField.val().substring(0, limitNum));
		limitCount.html('0');
	} else {
		var count = limitNum - limitField.val().length;
		count = count === 0 ? '0' : count; // jQuery won't put the number value of 0 in the element using html(), so make a string
		limitCount.html(count);
	}
}


(function() {
	/* -- THIS DOESN'T BELONG HERE -- NOT SURE WHERE? --*/

	// use a modal for myBag flyout checkout link
	$(document).ready(function() {
		// use a modal for international options
		$(".internationalModalLink").bind("click", function( evt ) {
			evt.preventDefault();
			evt.stopPropagation();
			var opener_page = "";
			var currentURLParameters = Array();
			var opener_page_temp_array = location.href.split("?");
			opener_page = opener_page_temp_array[0];
			if(opener_page_temp_array[1] != undefined){
				currentURLParameters = opener_page_temp_array[1].split("&");
				opener_page += "?";
			}
			for(var i=0, j=currentURLParameters.length; i < j; i++){
				if((currentURLParameters[i].indexOf("_requestid=") != 0)&&(currentURLParameters[i] != "")){
					opener_page += currentURLParameters[i];
					if (i != j-1) {
						opener_page += "&";
					}
				}
			}
			(new Modal({
				id: 'internationalOptionsModal',
				useAjax: true,
				url: '/' + jsContextRoot + '/modals/international.jsp',
				ajaxData: {
					name: "InternationalOptions",
					openerPage: opener_page
				},
				width: 375,
				closeSelector: '#checkoutModalCloseLink, #international_splash_cancel'
			})).open();
		});
	});
})();

/* -- THIS SHOULD BE MOVED TO CHECKOUT.JS -- */
function linkCountryToCountryType( countrySelector, countryTypeSelector ) {
	var countryTypeEl = $(countryTypeSelector);
	$(countrySelector).change(function( evt ) {
		var countryTypeVal = '',
			country = $(this),
			countryVal = '',
			countryText = '';
		
		if (country.is("select")) {
			countryVal = country.val();
			countryText = this.options[this.selectedIndex].text;
		} else {
			countryVal = this.replacerMicro.get();
			countryText = country.val();
		}
		
		switch (countryVal) {
			case "US":
				if (countryText == 'United States') {
					countryTypeVal = 'usa';
				} else {
					countryTypeVal = 'military';
				}
				break;
			case "CA":
				countryTypeVal = 'can';
				break;
			default:
				countryTypeVal = 'international';
		}
		
		if (countryTypeEl.is("select")) {
			countryTypeEl.val(countryTypeVal);
			countryTypeEl.triggerHandler("change");
		} else {
			countryTypeEl.get(0).replacerMicro.set(countryTypeVal);
		}
	});
}
