// JavaScript Document
function alertmsg() {
	alert("Item now added to your basket");
}

function MM_popupMsg(msg) { //v1.0
	alert(msg);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function bookmarksite(){
	if (document.all) {
		window.external.AddFavorite(location.href, document.title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(document.title, location.href, "");
	}
}

function incrementCurrent() {
    current = parseInt(document.forms["form1"].QTY.value);
    maxlimit = parseInt(document.forms["form1"].TOT.value);
	if(current < maxlimit) { 
		document.forms["form1"].QTY.value = current + 1;
	} else { 
		alert("No more in stock");
	}
}
	  
function decrementCurrent() {
    current = parseInt(document.forms["form1"].QTY.value);
	if(current > 1) { 
		document.forms["form1"].QTY.value = current - 1;
	}
}

function setCount(target){
	document.form1.Submit5.value  = "Please wait..."; 
	document.form1.Submit5.disabled  = "True";
	document.form1.action="addtobasketdb.asp";
	document.form1.submit();
}

function multDD() {
    current = parseInt(document.forms["form1"].QTY.value);
	document.forms["form1"].QTY.value = current * 10;	
}

function validateSearch(theForm) {
	if (theForm.searchinput.value.length < 1) {
		alert("You have not entered a valid search");
		theForm.searchinput.focus();
		return false;
	} else if (theForm.search.value == "Search for products...") {
		alert("You have not entered a valid search");
		theForm.searchinput.focus();
		return false;
	}
}

function validate(theForm) {
	
	var emailRE = /^.+@.+\..{2,3}$/
	//theForm.HOW.value = theForm.HOWDID.value + " - " + theForm.HOWOTHER.value;
	
	// Card Number Modulus Check
	var cardNumber = theForm.CNUMBER.value;
	var ar = new Array( cardNumber.length );
	var i = 0; 
	var sum = 0;
   	
	for( i = 0; i < cardNumber.length; ++i ) { 
		ar[i] = parseInt(cardNumber.charAt(i)); 
	}
	for( i = ar.length -2; i >= 0; i-=2 ) {
   		ar[i] *= 2;
    	if( ar[i] > 9 ) { ar[i]-=9; }
	}
	for( i = 0; i < ar.length; ++i ) { 
		sum += ar[i]; 
	}
	if ( (sum%10) == 0 ) { cardNumber = 0 }
	
	// Name/Address Validation
	if (theForm.Contact.value.length < 1) {
		alert("Please enter a your title");
		theForm.Contact.focus();
		return false;
	} else if (theForm.Contact2.value.length < 1) {
		alert("Please enter your first name or initial");
		theForm.Contact2.focus();
		return false;
	} else if (theForm.Contact3.value.length < 1) {
		alert("Please enter your surname");
		theForm.Contact3.focus();
		return false;		
	} else if (theForm.ADD1.value.length < 1) {
		alert("Please enter the first line of your address");
		theForm.ADD1.focus();
		return false;
	} else if (theForm.TWN.value.length < 1) {
		alert("Please enter a town");
		theForm.TWN.focus();
		return false;
	} else if (theForm.PCD.value.length < 1) {
		alert("Please enter a postcode");
		theForm.PCD.focus();
		return false;
	} else if (theForm.TEL.value.length < 1) {
		alert("Please enter a contact telephone number");
		theForm.TEL.focus();
		return false;
	} else if (!emailRE.test(theForm.EML.value)) {
		alert("Please enter a valid email address");
		theForm.EML.focus();
		return false;
	} else if (theForm.HOWDID.value == "Please Select...") {
		alert("Please specify how you found this website");
		theForm.HOWDID.focus();
		return false;
	
	// Card Validation
	} else if (cardNumber != 0) {
		alert("Please check your card number and re-enter");
		theForm.CNUMBER.focus();
		return false;
	} else if (theForm.CNUMBER.value.length < 15) {
		alert("Please check your card number and re-enter");
		theForm.CNUMBER.focus();
		return false;
	} else if (isNaN(theForm.CNUMBER.value)) {
		alert("Please enter your full card number without spaces or dashes");
		theForm.CNUMBER.focus();
		return false;
	//} else if (theForm.SMONTH.value == "00") {
	//	alert("Please enter your card Start Month");
	//	theForm.SMONTH.focus();
	//	return false;		
	//} else if (theForm.SYEAR.value == "0000") {
	//	alert("Please enter your card Start Year");
	//	theForm.SYEAR.focus();
	//	return false;		
	} else if (theForm.EMONTH.value == "00") {
		alert("Please enter your card Expiry Month");
		theForm.EMONTH.focus();
		return false;		
	} else if (theForm.EYEAR.value == "0000") {
		alert("Please enter your card Expiry Year");
		theForm.EYEAR.focus();
		return false;
	} else if (theForm.SECNUM.value.length < 3) {
		alert("Please enter your card security number");
		theForm.SECNUM.focus();
		return false;			
	
	// Discount Check 		
	} else {
		var EMLTST = theForm.EML.value;
		EMLTST = EMLTST.substring(EMLTST.length - 16);
		if (EMLTST == "barnardos.org.uk") {
			document.forms["form1"].DISTOT.value = (theForm.STKTOT.value - theForm.SAVTOT.value) * 0.20;
			document.forms["form1"].ORDTOT.value = (theForm.ORDTOT.value - theForm.DISTOT.value);
		}
	}
}

function validatejt(theForm) {
	
	var emailRE = /^.+@.+\..{2,3}$/
	//theForm.HOW.value = theForm.HOWDID.value + " - " + theForm.HOWOTHER.value;

	// Name/Address Validation
	if (theForm.Contact.value.length < 1) {
		alert("Please enter a your title");
		theForm.Contact.focus();
		return false;
	} else if (theForm.Contact2.value.length < 1) {
		alert("Please enter your first name or initial");
		theForm.Contact2.focus();
		return false;
	} else if (theForm.Contact3.value.length < 1) {
		alert("Please enter your surname");
		theForm.Contact3.focus();
		return false;		
	} else if (theForm.ADD1.value.length < 1) {
		alert("Please enter the first line of your address");
		theForm.ADD1.focus();
		return false;
	} else if (theForm.TWN.value.length < 1) {
		alert("Please enter a town");
		theForm.TWN.focus();
		return false;
	} else if (theForm.PCD.value.length < 1) {
		alert("Please enter a postcode");
		theForm.PCD.focus();
		return false;
	} else if (theForm.TEL.value.length < 1) {
		alert("Please enter a contact telephone number");
		theForm.TEL.focus();
		return false;
	} else if (!emailRE.test(theForm.EML.value)) {
		alert("Please enter a valid email address");
		theForm.EML.focus();
		return false;
	} else if (theForm.HOWDID.selectedIndex == 0) {
		alert("Please specify how you found this website");
		theForm.HOWDID.focus();
		return false;		
	} else if (theForm.HOWOTHER.value.length < 1) {
		alert("Please specify the OTHER way that you found this website");
		theForm.HOWOTHER.focus();
		return false;	
	// Discount Check 		
	
	} else {
		var EMLTST = theForm.EML.value;
		EMLTST = EMLTST.substring(EMLTST.length - 16);
		if (EMLTST == "barnardos.org.uk") {
			var DISTOT = (theForm.STKTOT.value - theForm.SAVTOT.value) * 0.20;
			document.forms["form1"].DISTOT.value = DISTOT.toFixed(2);
			document.forms["form1"].ORDTOT.value = (theForm.ORDTOT.value - theForm.DISTOT.value);
		}
	}
}

// Validate Contact Us Input
function validate2(theForm) {
	var emailRE = /^.+@.+\..{2,3}$/
	if (theForm.NAM.value.length < 1) {
		alert("Please enter your name");
		theForm.NAM.focus();
		return false;
	} else if (theForm.SUB.value.length < 1) {
		alert("Please enter the subject of your query");
		theForm.SUB.focus();
		return false;	
	} else if (!emailRE.test(theForm.EML.value)) {
		alert("Please enter a valid email address");
		theForm.EML.focus();
		return false;
	} else if (theForm.MSG.value.length < 1) {
		alert("Please enter a message describing your problem\nPlease include order numbers or stock codes if possible");
		theForm.MSG.focus();
		return false;
	} else {
		theForm.Submit.value 	= "Sending...";
		theForm.Submit.disabled = "True";
	}
}

// Validate Donation Input
function validate3(theForm) {
	var DON2 = theForm.DONOTHER.value;
	var DON1 = ""
	
	LEN = theForm.DON.length

	for (i = 0; i <LEN; i++) {
		if (theForm.DON[i].checked) {
			DON1 = theForm.DON[i].value
		}
	}
	
	if ( (!/^\$?\d+(\.(\d{2}))?$/.test(DON2)) && (DON1 == "OTHER")) {
		alert("Please enter a valid amount (eg. 5.00)");
		theForm.DONOTHER.focus();
		return false;
	} else {
		theForm.Submit.value 	= "Please wait...";
		theForm.Submit.disabled = "True";
	}
}


// Validate Catalogue Requests
function validateCR(theForm) {

	// Name/Address Validation
	if (theForm.NAM.value.length < 1) {
		alert("Please enter a your name");
		theForm.NAM.focus();
		return false;	
	} else if (theForm.AD1.value.length < 1) {
		alert("Please enter the first line of your address");
		theForm.AD1.focus();
		return false;
	} else if (theForm.TWN.value.length < 1) {
		alert("Please enter a town");
		theForm.TWN.focus();
		return false;
	} else if (theForm.PCD.value.length < 1) {
		alert("Please enter a postcode");
		theForm.PCD.focus();
		return false;
	} else if (theForm.HOWDID.selectedIndex == 0) {
		alert("Please specify how you found this website");
		theForm.HOWDID.focus();
		return false;
	}
}

// Check OTHER if other field entered
function CheckOther() {
	document.form1.DON[4].checked = true;	
}

// Menu
function MenuUpdate(menu) {
	var FirstMax = document.getElementById('firstmax').value;
	for (var i = 1; i <= FirstMax; i++){
		document.getElementById('li'+i).style.display = "none";
	}
	var menObj = document.getElementById('li' + menu);
	menObj.style.display="";
}

//------------------------------------------------------------------------------------------------------------------------
var smooth_timer;
var OrigHeightArray	= new Array();
function servOC(i) {
	var MaxHeight = 0;
	var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
	var ifObj = (document.getElementById) ? document.getElementById('ihif' + i) : eval("document.all['ihif" + i + "']");

	if (trObj != null) { //trObj is the id of the table row
		MaxHeight = OrigHeightArray[i];
		if (trObj.style.display=="none") {
			//Show the new row
			ifObj.style.height = "0px";
			trObj.style.display="";

			smoothHeight('ihif' + i, 0, MaxHeight, 10, 'o');
			document.getElementById('MoreInfoLnk'+i).innerHTML = "<a href='#' onclick='return false;'>Less Info...</a>";
		}else{     
			//Hide the new row
			smoothHeight('ihif' + i, MaxHeight, 0, 30, 'ihtr' + i);
			document.getElementById('MoreInfoLnk' + i).innerHTML = "<a href='#' onclick='return false;'>More Info...</a>";
		}
	}
}

//------------------------------------------------------------------------------------------------------------------------
function smoothHeight(id, curH, targetH, stepH, mode) {
	diff = targetH - curH;
	if (diff != 0) {
		newH =  diff > 0  ? curH + stepH : curH - stepH;
		if (diff > 0){
			newH = newH > targetH ? targetH : newH;
		}else{
			newH = newH < targetH ? targetH : newH;
		}
		((document.getElementById) ? document.getElementById(id) : eval("document.all['" + id + "']")).style.height = newH + "px";
		if (smooth_timer){
			window.clearTimeout(smooth_timer);
		}
		smooth_timer = window.setTimeout( "smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 50 );
	}else if (mode != "o"){
		((document.getElementById) ? document.getElementById(mode) : eval("document.all['" + mode + "']")).style.display="none";
	}
}
//------------------------------------------------------------------------------------------------------------------------

function HideAllRows(){
	var NumDivs = document.getElementById('NumDivs').value;
	for (var i = 1; i <= NumDivs; i++){
		OrigHeightArray[i]	= document.getElementById('ihif' + i).offsetHeight;
		//alert("Setting OrigHeightArray for "+i+" to "+OrigHeightArray[i]);
		document.getElementById('ihtr'+i).style.display = "none";
	}
}

function ScaleToFit(id) {
 	var obj= document.getElementById(id);
 	resize(obj.scrollWidth, obj.scrollHeight, 0, id);
}

function resize(width, height, current, id) {
	 var obj = document.getElementById(id);
	 if (current <= width) {
		 i = current + 10;
		 obj.style.width = i;
	 }
	 if (current <= height) {
		 i = current + 10;
		 obj.style.height = i;
	 }
	 if (current <= width || current <= height) {
		var t = window.setTimeout("resize("+width+", "+height+", "+i+", '"+id+"')", 10);
	 } else { obj.style.height = height; obj.style.width = width; }
}

function showdiv(txt,size) {
	document.getElementById('preview').style.display = "block";
	document.getElementById('preview').innerHTML = "<div style='height: 360px; width: 360px; background: url(Stock/Large/" + txt + ".jpg) center center no-repeat;'></div>"
	ScaleToFit('preview');
}

function hidediv() {
	document.getElementById('preview').style.display = "none";
}	

function greyout() {
	document.getElementById("darkLayer").style.display = "block";
}
