
function createAJAX() {
	try {
		return window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
		alert('Object creation falied');
		return false;
	}
}

function loadAJAX(sURL, sProcessor) {
	var aHTTP = createAJAX();
	
	aHTTP.onreadystatechange = function() {
		if ((aHTTP.readyState == 4) && (aHTTP.status == 200))
			eval(sProcessor + '(aHTTP.responseXML);');
	}
	aHTTP.open("GET", sURL, true);
	aHTTP.send(null);
}

function cartAdd(oForm) {
	var sURL = '/scripts/xml_call.asp?CallID=3&OrderCodeID=' + oForm.OrderCodeID.value + 
				  '&SourceCodeID=' + oForm.SourceCodeID.value +
				  '&SubDefinitionID=' + oForm.SubDefinitionID.value +
				  '&Quantity=' + oForm.Quantity.value;

	var sXML = loadAJAX(sURL, 'cartRefresh');
}

function cartRefresh(oXML) {
	alert(oXML.getElementsByTagName('result')[0].innerHTML);

	document.getElementById('basket_price').innerHTML = '&pound;' + oXML.getElementsByTagName('result')[0].xml;
}

function toggleAddress(sLayerName, oForm) {
	var sLabel, strAddressTag, intPos;


	//Netscape
	if (document.layers) {
		oLayer = document.layers[sLayerName];
	}
	//IE and Firefox
	else {
		oLayer = document.getElementById(sLayerName);
	};
	
	if (oLayer.style.display == 'none') {
		oLayer.style.display = 'block';
		sLabel = 'remove this address.';
		oForm.addresses.value = oForm.addresses.value + sLayerName + '|'
		//alert('adding: ' + oForm.addresses.value)
	} 
	else {
		oLayer.style.display = 'none';
		sLabel = 'add a ' + sLayerName + ' address different from your details.';
		intPos = oForm.addresses.value.indexOf(sLayerName + '|')
		while (intPos != -1) {
			strAddressTag = oForm.addresses.value
			strAddressTag = strAddressTag.replace(sLayerName + '|', '')
			oForm.addresses.value = strAddressTag
			intPos = oForm.addresses.value.indexOf('|' + sLayerName + '|')
		}
		//alert('removing: ' + oForm.addresses.value)
	}
	
	if (document.layers) {
		oLayer = document.layers[sLayerName + '_label'];
	}
	else {
		oLayer = document.getElementById(sLayerName + '_label');
	}
		
	oLayer.innerHTML = sLabel;
}

function promoView(iPromoID) {
	openWindowMove('/promo.asp?PromoID=' + iPromoID, 'promo', '', '500', '300', 'middle', 'middle', false);
}
function emaf(sLink, iProductID) {
	openWindowMove('/emaf.asp?link=' + escape(sLink) + '&pid=' + iProductID, 'emaf', 'scrollbars=yes', '500', '450', 'middle', 'middle', false);
}
function tandc() {
	var pubid = document.getElementById("pubID").value;
	openWindowMove('/shared/includes/termsandprivacy.asp?todo=1&pubid=' + pubid, 'tancs', 'scrollbars=yes', 520, 700, 'middle', 'middle', false);
}
function privacyPolicy() {
	var pubid = document.getElementById("pubID").value;
	openWindowMove('/shared/includes/termsandprivacy.asp?todo=2&pubid=' + pubid, 'privacyPolicy', 'scrollbars=yes', '900', '500', 'middle', 'middle', false);
}
function reviewProduct(iProductID) {
	openWindowMove('/review.asp?ProductID=' + iProductID, 'review', 'scrollbars=yes', '500', '440', 'middle', 'middle', false);
}
function checkPassword(oForm) {
	if (!isFilled(oForm.uPasswordOld.value) || !isFilled(oForm.uPassword.value) || !isFilled(oForm.uPasswordConfirm.value)) {
		alert('Please fill in all fields.')
		return false;
	}
	
	if (oForm.uPassword.value != oForm.uPasswordConfirm.value) {
		alert('The new passwords do not match.')
		return false;
	}
	
	return true;
}
function addEMAF(oTable, iIndex) {
    
    var personIndex = document.getElementsByName('their_email').length;
    var rBeginIndex = iIndex + (personIndex -1) * 3
    var oRow = oTable.insertRow(rBeginIndex);
	oRow.insertCell(0).innerHTML = '';
	oRow.insertCell(1).innerHTML = '<input class="input01" type="text" name="their_email" />';
	oRow = oTable.insertRow(rBeginIndex + 1);
	oRow.insertCell(0).innerHTML = '';
	oRow.insertCell(1).innerHTML = '<input type="radio" checked="checked" name="rMessage' + personIndex + '" value="1">You may be interested in this';
	oRow = oTable.insertRow(rBeginIndex + 2);
	oRow.insertCell(0).innerHTML = '';
	oRow.insertCell(1).innerHTML = '<input type="radio" name="rMessage' + personIndex + '" value="2">Please purchase this for me';
}
function checkTandC(oForm) {
	if (!oForm.termsandconditions)
		return false;
		
	if (!oForm.termsandconditions.checked) {
		alert('\n * You cannot continue without agreeing to the terms and conditions.');
		return false;
	}
	
	return true;
}
function productDownload(sURL) {
//	var win = window.open(sURL, '_new');
//	if (win == null) {
//		alert('Please turn off your popup blocker, or click the button again to complete the download');
//	}
    window.location = sURL;
		
	//document.getElementById('download').innerHTML = '<iframe width="0" height="0" src="' + sURL + '"></iframe>';
}
function closeRedirect(sURL) {
    if (window.opener==null) {
        window.location = sURL;
    }
    else {
	    window.opener.location = sURL;
	    window.close();
	}
}
function emailWarning(oCheck) {
	if (oCheck.checked)
		openWindowMove('/stub.asp?StubID=455&popup=true', 'tandc', 'scrollbars=yes', '400', '225', 'middle', 'middle', false);
}
function showHideDiv(oDiv) {
	if(oDiv.style.display=='none')
		oDiv.style.display='block';
	else
		oDiv.style.display='none';
}
function checkEmail(oForm) {
	if (!isEmail(oForm.uEmailAddress.value)) {
		alert("please enter a valid email address.")
		return false;
	} else
		return true;
}

function checkFeedbackForm(oForm) {
    var msg = ''
    var val
    
    val = oForm.txtName.value
    if (val.length >100) {
        msg += 'Please limit your name to 100 characters' + '\n'
    }
    
    val = oForm.txtEmail.value
    if (val.length >100) {
        msg += 'Please limit your email to 100 characters' + '\n'
    }
    if (val.length >0 && !isEmail(val)) {
        msg += 'Please enter a valid email' + '\n'
    }
    
    val = oForm.txtComment.value
    if (val.length == 0 || val.length > 250) {
        msg += 'Please enter a comment between 1 to 250 characters' + '\n'
    }
    
    if (msg.length > 0) {
        alert(msg);
        return false;
    }
    
    return true;
}

/*function switchPersonalisedPostions(rowPrefix, positionID) {
	var targetPosID = rowPrefix + positionID
	var prefix = rowPrefix + '';
	var targetPos
	
	//Netscape
	if (document.layers) {
		targetPos = document.layers[targetPosID];
	}
	//IE and Firefox
	else {
		targetPos = document.getElementById(targetPosID);
	}
	
	
	var elems = document.getElementsByTagName('tr');

	for (var i=0; i<elems.length; i++) {
		if (elems[i].id!='' && elems[i].id.indexOf(prefix) == 0 && elems[i].id != targetPosID) {
			elems[i].style.display = 'none';	
		}
		else {
			elems[i].style.display = 'block';	
		}
	}

}*/

function promptEBook(oForm) {
	var pids = oForm.pcIDs.value.split(";");
	var chkID;
	var count=0;
	
	for (var i=0; i<pids.length; i++) {
		chkID = "PC_" + pids[i] + "Check";
		if (document.getElementById(chkID).checked) {
			count += 1;
		}
	}
	
	if (count > 4) {
		return confirm("You have selected items with a total price greater than the full book. \nTo proceed with your current choice click OK or to select the full book click Cancel.");
	}
	
	return true;
}
