//**********************************************************
//**************** UTILITY FUNCTIONS ***********************
//**********************************************************
function popIt(width, height, scrollbars){ 
	if(!width) width = "640";
	if(!height) height = "480";
	if(!scrollbars) scrollbars = 1;
	var windowString = 'width=' + width + ',height=' + height + ',resizable=1,status=0,toolbar=no,scrollbars=' + scrollbars + ',scrolling=auto';
	var popItWindow = window.open('',"popIt", windowString);
	return false;
}
function encodeOptions(input) {
	input = String(input).replace(/&/g, "!!amp!!");
	input = String(input).replace(/=/g, "!!eq!!");
	return input;
}

//**********************************************************
//**************** ADD TO FAVORITES BUTTON *****************
//**********************************************************
function addToFavorites(orderForm) {
	var optionString = '';
	var optionList = new Array();
	for(var i=0;i<orderForm.elements.length;i++) {
		if(orderForm.elements[i].type == "select-one" || String(orderForm.elements[i].name).indexOf("vw-inscription-") > -1) {
			var option = new Object();
			option.name = orderForm.elements[i].name;
			option.value = orderForm.elements[i].value;
			optionList.push(option);
		}
	}
	for (i = 0; i <= optionList.length - 1; i++) optionString += encodeOptions(optionList[i].name) + "=" + encodeOptions(optionList[i].value) + "&";
	optionString = optionString.substring(0, optionString.length - 1);	//*** TRIM TRAILING "&"
	popIt(394,400,0);
	var myInput = document.createElement("INPUT");
	myInput.type = "hidden";
	myInput.name = "product-option-list";
	myInput.value = optionString;
	orderForm.appendChild(myInput);
	var tempAction = orderForm.action;
	var copiedQueryString = "";
	var queryStringPosition = tempAction.indexOf("?");
	if(queryStringPosition > -1) copiedQueryString = tempAction.substring(queryStringPosition, tempAction.length);
	var tempTarget = orderForm.target;
	orderForm.action="http://myaccount.babygirlboutique.com/favoritesAddItem.php" + copiedQueryString;
	orderForm.target="popIt";
	orderForm.submit();
	orderForm.action = tempAction;
	orderForm.target = tempTarget;
	orderForm.removeChild(myInput);
	return false;
}
//**********************************************************
//****** COPY BILLING FROM SHIPPING ON REGISTRY FORM *******
//**********************************************************
function copyBillingInfo(form) {
	if (form.copy.checked) {
		//**********************************************************
		//********* SET SHIPPING FIELDS AS VARIABLES ***************
		//**********************************************************
		var billFirstName = form.billFirstName.value;
		var billLastName = form.billLastName.value;
		var billAddress1 = form.billAddress1.value;
		var billAddress2 = form.billAddress2.value;
		var billCity = form.billCity.value;
		var billState = form.billState.value;
		var billZip = form.billZip.value;
		var billCountry = form.billCountry.value;
		var billPhone = form.billPhone.value;
				
		//***********************
		//*** OPTIONAL FIELDS ***
		//***********************
		try {var billCompany = form.billCompany.value;} catch(e) {}
		try {var billFax = form.billFax.value;} catch(e) {}
		try {var businessInfoBillingEmail = form.businessInfoBillingEmail.value;} catch(e) {}	//*** CB ONLY

		//**********************************************************
		//****** POPULATE SHIPPING FIELDS WITH BILLING DATA ********
		//**********************************************************
		form.shipFirstName.value = billFirstName;
		form.shipLastName.value = billLastName;
		form.shipAddress1.value = billAddress1;
		form.shipAddress2.value = billAddress2;
		form.shipCity.value = billCity;
		form.shipState.value = billState;
		form.shipZip.value = billZip;
		form.shipCountry.value = billCountry;
		form.shipPhone.value = billPhone;
		
		//***********************
		//*** OPTIONAL FIELDS ***
		//***********************
		try {form.shipCompany.value = billCompany;} catch(e) {}
		try {form.shipFax.value = billFax;} catch(e) {}
		try {form.businessInfoShippingEmail.value = businessInfoBillingEmail;} catch(e) {}	//*** CB ONLY

	} else {

		//**********************************************************
		//******************* RESET BILLING DATA *******************
		//**********************************************************
		try {form.shipCompany.value = "";} catch(e) {}
		form.shipFirstName.value = "";
		form.shipLastName.value = "";
		form.shipAddress1.value = "";
		form.shipAddress2.value = "";
		form.shipCity.value = "";
		form.shipState.value = "";       
		form.shipZip.value = "";
		form.shipCountry.value = "US";
		form.shipPhone.value = "";
		
		//***********************
		//*** OPTIONAL FIELDS ***
		//***********************
		try {form.shipCompany.value = "";} catch(e) {}
		try {form.shipFax.value = "";} catch(e) {}
		try {form.businessInfoShippingEmail.value = "";} catch(e) {}	//*** CB ONLY
	
	}
}

var pdMyAccountCopyBillingInfo = copyBillingInfo;	//*** FIX FOR REGISTRY OVERRIDING FUNCTION


function getUrl(method, url, data) {

	//*************************************************
	//*** MODIFY URL TO PREVENT CACHING  (IE 6 fix) ***
	//**********************************************
	var cacheBuster = (new Date()).getTime();
	if(url.indexOf("?") > -1) url += "&cb=" + cacheBuster;
	else url += "?cb=" + cacheBuster;

	//******************************************************************
	//*** INSTANTIATE CORRECT XMLHttpRequest (standard vs. older IE) ***
	//******************************************************************
	try {
		var xmlhttp = new XMLHttpRequest();
	} catch(e) {
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	//*** IE 6
	}
	
	xmlhttp.open(method, url, false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send(data);
	var responseText = String(xmlhttp.responseText);
	xmlhttp = null;
	
	//**************
	//*** RETURN ***
	//**************	
	return responseText;
}
