// JavaScript Document

if (window.ActiveXObject) {
	var storeConnect=new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
	var storeConnect=new XMLHttpRequest();
}

function getTotal(obj){
	obj.action='';
	var sendStr='';
	for (i=0; i<obj.elements.length; i++){
		with (obj.elements[i]){
			if ((type=='radio' && checked==true) || type!='radio'){
				sendStr=sendStr+name+'='+escape(value)+'&';
			}
		}
	}
	storeConnect.open('post','store/_getPrice.cfm',0);
	storeConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	storeConnect.send(sendStr);
	document.getElementById('totalPrice').innerHTML=storeConnect.responseText;
	if (document.getElementById('addCart')) document.getElementById('addCart').focus();
	window.scrollTo(0,999999);
}

function valImages(){
	if (document.getElementById('artSmall').value=='' && document.getElementById('prevArtSmall').value=='none'){
		alert('You must upload an image for the small application before you can save this item to your cart.');
		return false;
	}
	if (document.getElementById('artLarge').value=='' && document.getElementById('prevArtLarge').value=='none'){
		alert('You must upload an image for the large application before you can save this item to your cart.');
		return false;
	}
}

function fillBilling(objStat){
	if (objStat){
		document.getElementById('BillAddress').value=document.getElementById('ShipAddress').value;
		document.getElementById('BillCity').value=document.getElementById('ShipCity').value;
		document.getElementById('BillState').value=document.getElementById('ShipState').value;
		document.getElementById('BillZip').value=document.getElementById('ShipZip').value;
		var billOpts=document.getElementById('billCountry').options;
		for (i=0;i<billOpts.length;i++){
			if (billOpts[i].value==document.getElementById('ShipCountry').value) billOpts[i].selected=true;
		}
	} else {
		document.getElementById('BillAddress').value='';
		document.getElementById('BillCity').value='';
		document.getElementById('BillState').value='';
		document.getElementById('BillZip').value='';
	}
}

var currentForm;
function getNewAddress(objForm){
	currentForm=objForm;
	if (objForm.shipTo.value!='') return;
	activateOverlay('newAddress','store/_newRecipient.cfm',false,'#000000',80);
}

function showTab(obj){
	if (document.getElementById('message')) document.getElementById('message').style.display='none';
	var tab=obj.parentNode;
	var tabs=document.getElementById('tabs').getElementsByTagName('div');
	for (i=0;i<tabs.length;i++) {
		if (tabs[i].className.indexOf('tabCard')!=-1) tabs[i].className='tabCard';
	}
	tab.className='tabCardSelect';
}
function updateShipVia(obj){
	var str='act=6&rid=' + obj.getAttribute('recipID') + '&shipVia=' + obj.value;
	var resp=getXML(str,'store/_doStoreAjax.cfm');
	var chk=resp.getElementsByTagName('success')[0].firstChild.data;
	if (chk!='true'){
		confirm('An error occurred while updating your quantities. Please click "OK" to try again. If the problem continues, please click "Cancel" and contact McCall\'s for assistance');
	}
}
function updateQty(obj){
	var str='act=4&id=' + obj.getAttribute('recId') + '&qty=' + obj.value;
	var resp=getXML(str,'store/_doStoreAjax.cfm');
	var chk=resp.getElementsByTagName('success')[0].firstChild.data;
	if (chk=='true'){
		document.getElementById('ordTotal').innerHTML=resp.getElementsByTagName('newTotal')[0].firstChild.data;
		document.getElementById('cost' + obj.getAttribute('recID')).innerHTML=resp.getElementsByTagName('newExt')[0].firstChild.data;
	} else {
		confirm('An error occurred while updating your quantities. Please click "OK" to try again. If the problem continues, please click "Cancel" and contact McCall\'s for assistance');
	}
}	
function updateRecipients(id){
	var selects=document.getElementsByTagName('select');
	var str='act=5';
	var resp=getXML(str,'store/_doStoreAjax.cfm');
	var chk=resp.getElementsByTagName('success')[0].firstChild.data;
	if (chk=='true'){
		var recipArray=resp.getElementsByTagName('recipients')[0].getElementsByTagName('recipient');
		for (y=0;y<selects.length;y++){
			selects[y].options.length=0;
			for (x=0;x<recipArray.length;x++){
				var recipName=recipArray[x].getElementsByTagName('name')[0].firstChild.data;
				var recipID=recipArray[x].getElementsByTagName('id')[0].firstChild.data;
				var recipValue=recipID=='xxx'?'':recipID+ '||' + recipName;
				selects[y].options[x]=new Option(recipName,recipValue,false,selects[y].form==currentForm&&id==recipID);
			}
		}
	} else {
		confirm('An error occurred while updating your recipient list. Please click "OK" to try again. If the problem continues, please click "Cancel" and contact McCall\'s for assistance');
	}
}
function updateTaxShip(obj){
	if (obj.name=='shipCountry'){
		str='act=2&loc='+obj.value+'&amt='+document.goCheckout.sumPrice.value;
	} else if (obj.name=='ShipZip'){
		str='act=3&zip='+obj.value+'&amt='+document.goCheckout.sumPrice.value;
	}

	var resp=getXML(str,'store/_doStoreAjax.cfm');
	var chk=resp.getElementsByTagName('success')[0].firstChild.data;
	if (chk!='true'){
		document.getElementById('response').innerHTML=resp.getElementsByTagName('message')[0].firstChild.data;
		if (confirm("An error occurred while attempting to retrieve tax and shipping information. To try again, click 'OK'. If this problem persists, please click 'Cancel' and  contact McCall's for assistance.")) obj.focus();
	} else if (resp.getElementsByTagName('taxChg')[0]!=null){
		var tax=parseFloat(resp.getElementsByTagName('taxChg')[0].firstChild.data).toFixed(2);
		document.getElementById('taxChrgs').innerHTML='$'+tax;
		document.goCheckout.tax.value=tax;
		if (tax==0) {
			document.getElementById('taxRow').style.display='none';
		} else {
			document.getElementById('taxRow').style.display='block';
		}
	} else if (resp.getElementsByTagName('shipChg')[0]!=null){
		var shipping=parseFloat(resp.getElementsByTagName('shipChg')[0].firstChild.data).toFixed(2);
		document.getElementById('shipChrgs').innerHTML='$'+shipping;
		document.goCheckout.shipCharge.value=shipping;
	}
	document.goCheckout.ordTotal.value=parseFloat(document.goCheckout.sumPrice.value)+parseFloat(document.goCheckout.tax.value)+parseFloat(document.goCheckout.shipCharge.value);
	document.getElementById('ordTotal').innerHTML='$'+parseFloat(document.goCheckout.ordTotal.value).toFixed(2);
}

function validateRecip(obj){
	if (obj.shipTo.value=='') {
		if (!confirm('You must input address information for the new recipient. Click "OK" to enter new information. Click "Cancel" to choose another recipient.')){
			return false;
		} else {
			getNewAddress(obj);
			return false;
		}
	}
}
<!-----------------------AJAX -------------------------------->

/*
function getXML(sendStr,template){
	srvrConnect.open('post',template,0);
	srvrConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	srvrConnect.send(sendStr);
	//alert(srvrConnect.responseText);
	var resp=srvrConnect.responseXML;
	var notWhitespace = /\S/
	var xmlData=resp.getElementsByTagName('data')[0];
	//REMOVE white spaces in XML file. Intended mainly for NS6/Mozilla
	try {
		for (i=0;i<xmlData.childNodes.length;i++){
			if ((xmlData.childNodes[i].nodeType == 3)&&(!notWhitespace.test(xmlData.childNodes[i].nodeValue))) {
				// that is, if it's a whitespace text node
				xmlData.removeChild(xmlData.childNodes[i])
				i--
			}
		}
		return xmlData;
	} catch(err) {
		document.getElementById('response').innerHTML=srvrConnect.responseText;
		var xml=document.createDocumentFragment();
		var node=xml.createElement('success');
		xml.appendChild(node);
		var contents=xml.createTextNode('false');
		node.appendChild(contents);
		var node2=xml.createElement('message');
		var contents2=xml.createTextNode(srvrConnect.responseText);
		node2.appendChild(contents2);
		xml.appendChild(node2);
	 	return xml;
	}
}
*/
