function confirmdelete() {
	return confirm('Deleting this entry can not be undone. Continue?');
}

function calcCharLeft(s, t, v) {
	// "s" is the source field, "t" is the target field, "v" is value of the blnallowed field length

		supportsKeys = true
		maxLength = v
        if (s.value.length > maxLength) {
	        s.value = s.value.substring(0,maxLength)
		    charleft = 0
        } else {
			charleft = maxLength - s.value.length
		}
		message = " characters left"
        t.value = charleft+message
}

function SetElementValue(elt,val){
      //manually overrides a form element value.  elt is the element name, val is new value
      elt.value = val
}

function SetMultiElementValue(myForm,myList) {
	// takes the name of a form and a list of elements & new values to be set within it
	// eg SetMultiElementValue(this.form,'txtName=Andrew,txtMobile=0439 881 392');
	
	var arrPairs = myList.split("|");
	var arrTemp;
	var myElement;
	var myValue;

	for (i = 0; i < arrPairs.length; i++) {
		arrTemp = arrPairs[i].split("=");
		myElement = arrTemp[0];
		myValue = arrTemp[1];
		myElement = eval("myForm." + myElement);
		myElement.value = myValue;
	}
}

function ValidateRegistration(myForm) {


    
}
//check to see whether a form element exists on the page used to determin whether to apply validation
function element_exists(element_name) {
        if(document.getElementsByName(element_name).length > 0) {
                return true;
        } else {
                return false;
        }
} 

// validateform(form) function
// by Andrew Thompson, Dec 2002
// validates all forms on the page, displays error message and highlights problem inputs
// fields should have an id attribute formatted id="FriendlyName,Required,DataType"
// for example <input type="text" name="txtBlahBlah" id="Text Box,1,txt">
// data types can be {txt,num,eml, dte}
// field names starting with 'num' will be checked that they're numeric

function validateform(myform, prefix) {
	var elementname;						// name attribute
	var elementvalue						// value attribute
	var valid = true;						// becomes false when any error is encountered
	var errData = "";						// names of bad Data fields
	var errReq = "";						// names of missing required fields
	var id, arrid;							// array with contents of form element's id attribute
	var req;								// temp variable for required field check
	var friendly;							// friendly field name string
	var datatype;							// data type, string, eg txt, num, eml, dte
	var strError = "";
	var iTmp1;
	var sTmp1;
	var sTmp2;
	var sTmp3;
	var arremail;

        for (j = 0; j < myform.elements.length; j++) {
		elementname = myform.elements[j].name;
		elementvalue = myform.elements[j].value;

		req = false;
		friendly = "";
		datatype = "";
		

		// check for an id attribute, and draw out friendly name, etc.
		if (myform.elements[j].id != "") {
			id = myform.elements[j].id;
			arrid = id.split(",")
			friendly = arrid[0];
			if (parseInt(arrid[1])) req = true;
			datatype = arrid[2];
		}

		// reset background colours in case fields were previously highlighted
		myform.elements[j].style.backgroundColor = "";
        
        if (element_exists('cmdTerms') ) {
            if(! myform.elements["cmdTerms"].checked){
                valid = false;
                errData = "You must agree with the Terms & Conditions of Definitely Chocolate if you wish to register with us.\n";
                alert(errData)
                return false
            }
        }

        // check for invalid data type if type was specified and field is not blank
		// as this will be handled later.
		if ((datatype != "") && !(elementvalue == "")) { 
			if (datatype == "pwd") {
				if (elementvalue.length < 6) {
					valid = false;
					errData += " -  " + friendly + " must be at least 6 characters in length.\n";
					myform.elements[j].style.backgroundColor ="#CCCCFF";
				}else if (element_exists('cmdPasswordConfirm') ) {
					if(myform.elements["cmdPasswordConfirm"].value != elementvalue){
						valid = false;
						errData += " -  " + friendly + " Your password does not match your confirmation password.\n";
						myform.elements[j].style.backgroundColor ="#CCCCFF";
						myform.elements["cmdPasswordConfirm"].style.backgroundColor ="#CCCCFF";
					}
				}
			}
			if (datatype == "num") {
				elementvalue = elementvalue.replace("$", "");
				elementvalue = elementvalue.replace(",", "");
				if (isNaN(elementvalue)) {
					valid = false;
					errData += " -  " + friendly + " must be numeric.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
			}
			if (datatype == "num+") {
				elementvalue = elementvalue.replace("$", "");
				elementvalue = elementvalue.replace(",", "");
				if (isNaN(elementvalue)) {
					valid = false;
					errData += " -  " + friendly + " must be numeric.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
				else if (parseFloat(elementvalue) <= 0) {
					valid = false;
					errData += " -  " + friendly + " must be a positive number.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
			}
			if (datatype == "int") {
				elementvalue = elementvalue.replace("$", "");
				elementvalue = elementvalue.replace(",", "");
				if (isNaN(elementvalue) || (parseInt(elementvalue) != parseFloat(elementvalue))) {
					valid = false;
					errData += " -  " + friendly + " must be a whole number.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
			}
			if (datatype == "int+") {
				elementvalue = elementvalue.replace("$", "");
				elementvalue = elementvalue.replace(",", "");
				if (isNaN(elementvalue) || (parseInt(elementvalue) != parseFloat(elementvalue))) {
					valid = false;
					errData += " -  " + friendly + " must be a positive whole number.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
				else if (parseFloat(elementvalue) <= 0) {
					valid = false;
					errData += " -  " + friendly + " must be a positive whole number.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
			}
			if (datatype == "dte") {
				if (isNaN(Date.parse(elementvalue))) {
					valid = false;
					errData += " -  " + friendly + " must be a valid date.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
			}
			if (datatype == "eml") {
				// put into an array to enable test for multiple email addresses
				arremail = elementvalue.split(",")
				for (var loop=0; loop < arremail.length; loop++) {
					// new and improved RegExp
					myRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (arremail[loop].search(myRegExp) == -1  && arremail[loop] !='') {
						valid = false;
						errData += " -  " + friendly + " has an invalid e-mail address:'" + arremail[loop] + "'\n";
						if (loop > 0) {
							errData += "    (separate multiple addresses with ',')\n";
						}
						// highlight this problem field
						myform.elements[j].style.backgroundColor = "#CCCCFF"; 
					}
				} 
			}
			if (datatype == "tme") {
  				iTmp1 = elementvalue.indexOf(":") 
  				if (iTmp1 == -1) { 
					valid = false;
					errData += " -  " + friendly + " must be a valid time.\n";
					// highlight this problem field
					myform.elements[j].style.backgroundColor = "#CCCCFF"; 
				}
  				else { 
					sTmp1 = elementvalue.substr(0 , iTmp1) 
					sTmp2 = elementvalue.substr(iTmp1 + 1, 2) 
					sTmp3 = elementvalue.substr(iTmp1 + 4, 2) 
					sTmp4 = elementvalue.substr(iTmp1 + 7)
  				  	if (!((!isNaN(sTmp1)) && (!isNaN(sTmp2)) && (!isNaN(sTmp3)) && (sTmp1>=0) && (sTmp1<24) && (sTmp2>=0) && (sTmp2<60) && (sTmp3>=0) && (sTmp3<60))) {
						valid = false;
						errData += " -  " + friendly + " must be a valid time.\n";
						// highlight this problem field
						myform.elements[j].style.backgroundColor = "#CCCCFF"; 
					}
				} 
			}
		}
		// check for required field being blank
		if (req && (elementvalue == "")) {
			valid = false;
			errReq += " Please enter your  " + friendly + "\n";
			// highlight this problem field
			myform.elements[j].style.backgroundColor = "#CCCCFF";
		}
		myRegExp = /^([\'])+/;
		if (elementvalue.search(myRegExp) != -1){
			valid = false;
			errReq += "Please do not enter \' at the begining of your " + friendly + "\n";
			// highlight this problem field
			myform.elements[j].style.backgroundColor = "#CCCCFF"; 
		}
		myRegExp = /([\'])$/;
		if (elementvalue.search(myRegExp) != -1){
			valid = false;
			errReq += "Please do not enter \' at the end of your " + friendly + "\n";
			// highlight this problem field
			myform.elements[j].style.backgroundColor = "#CCCCFF"; 
		}
		myRegExp = /^([ ])+/;
		if (elementvalue.search(myRegExp) != -1){
			valid = false;
			errReq += "Please do not enter Space at the begining of your " + friendly + "\n";
			// highlight this problem field
			myform.elements[j].style.backgroundColor = "#CCCCFF"; 
		}
	}
    
    if (!valid) {	// if an error occurred, generate the error report display

		strError = ""
		if (errReq != "") strError += errReq;
		if (errData != "") strError += errData;
		strError += "\nThe fields requiring attention have been highlighted.\n";
		alert(strError);
	}
	return valid;
}

// selectdate takes a reference to the form in which an element sits, and the name of the target field
// NB: this function always returns false, so for image inputs, you can put onclick="return selectdate(...);"
function selectdate(form, fieldname) {
	querystring = "?field=document." + form.name + "." + fieldname;
	myfield = eval(form.name + "." + fieldname);
	// sometimes we have two fields with the same name to separately edit the date and time
	// in this case we select the first one to work with
	if (myfield.length) myfield = myfield[0]
	if (myfield.value != "") {
		var myDate = new Date(myfield.value)
		querystring += "&d=" + myDate.getDate() + "&m=" + (myDate.getMonth() + 1) + "&y=" + myDate.getFullYear()
	}
	window_popup = window.open('/includes/calendar.asp' + querystring, 'selectdate', 'width=210,height=203');
	window_popup.focus();
	return false;
}

// displayhelp takes a Page_ID and displays a help window containing help.asp?pageid={Page_ID}

var new_window

function displayhelp(numpageID) {
	new_window = window.open('', 'help_window', 'scrollbars=1,resizable=1');
	if (new_window.location == "about:blank") {
		new_window.moveTo(0,0);
		new_window.resizeTo(screen.availWidth,150);
		window.resizeTo(screen.availWidth, screen.availHeight-150);
		window.moveTo(0,150);
	}
	new_window.location='/help/help.asp?pageID=' + numpageID;
	new_window.focus();
	return false;
}

function makeRemote(strURL) {
    remote = window.open("","remotewin","width=725,height=380,scrollbars=1");
    remote.location.href = strURL;
    remote.focus();
        if (remote.opener == null) remote.opener = window; remote.opener.name = "opener"; 
}

//Credit card luhn check
function LuhnCheck(str) {


  var sum = 0; 
  var mul = 1;
  var strLen = str.length;
  if(str == "" || strLen < 13){
	alert("Please enter a valid credit card number (insufficient digits).");
	return false;
  }
  for (i = 0; i < strLen; i++)  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0){
		alert("Please enter a valid credit card number.")
		return false;
  }

  //return false;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}