function checkMoveToCart(formId) {
	var form		= document.getElementById(formId);
	var allofem		= document.getElementsByName('move_to_cart[]');
	var flag		= false;
	var box			= document.getElementById('box');
	
	for (var i=0; i<allofem.length; i++) {
		if (!allofem[i].checked) {
			flag	= true;
		}
	}
	
	if (flag) {
		box.innerHTML	= '<div class="header">'+
							'<p>Error: please read the below</p>'+
							'<img class="close" src="images/close.gif" onclick="hideBox()" alt="Close" title="Close this window" />'+
						'</div>'+
						'<div class="text">'+
							'<p> &bull;&nbsp; Please select a product to add to your cart.</p>'+
						'</div>';
		showBox();
	} else form.submit();
}

function echeck(str) {
	var at= "@";
	var dot= ".";
	var lat= str.indexOf(at);
	var lstr= str.length;
	var ldot= str.indexOf(dot);
	
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	
	return true					
}

var commonPasswords= new Array('password', 'pass', '1234', '1246');
var numbers= "0123456789";
var lowercase= "abcdefghijklmnopqrstuvwxyz";
var uppercase= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var punctuation= "!.@$�#*()%~<>{}[]";

function checkPassword(password) {
    var combinations= 0;

    if (contains(password, numbers) > 0) {
        combinations+= 10;
    }

    if (contains(password, lowercase) > 0) {
        combinations+= 26;
    }

    if (contains(password, uppercase) > 0) {
        combinations+= 26;
    }

    if (contains(password, punctuation) > 0) {
        combinations+= punctuation.length;
    }

    // work out the total combinations
    var totalCombinations= Math.pow(combinations, password.length);

    // if the password is a common password, then everthing changes...
    if (isCommonPassword(password)) {
        totalCombinations= 75000 // about the size of the dictionary
    }

    // work out how long it would take to crack this (@ 200 attempts per second)
    var timeInSeconds= (totalCombinations / 200) / 2;

    // this is how many days? (there are 86,400 seconds in a day.
    var timeInDays= timeInSeconds / 86400

    // how long we want it to last
    var lifetime= 365;

    // how close is the time to the projected time?
    var percentage = timeInDays / lifetime;

    var friendlyPercentage = cap(Math.round(percentage * 100), 100);
    if (totalCombinations != 75000 && friendlyPercentage < (password.length * 5)) {
        friendlyPercentage += password.length * 5;
    }

    var progressBar = document.getElementById("progressBar");
    progressBar.style.width = friendlyPercentage + "%";
	progressBar.innerHTML= friendlyPercentage + "%";

    if (percentage > 1) {
        // strong password
        progressBar.style.backgroundColor = "#339933";
		progressBar.innerHTML= friendlyPercentage + "% (strong)";
        return;
    }

    if (percentage > 0.5) {
        // reasonable password
        progressBar.style.backgroundColor = "#ffd801";
		progressBar.innerHTML= friendlyPercentage + "% (okay)";
        return;
    }

    if (percentage > 0.10) {
        // weak password
        progressBar.style.backgroundColor = "orange";
		progressBar.innerHTML= friendlyPercentage + "% (weak)";
        return;
    }

    // useless password!
    if (percentage <= 0.10) {
        // weak password
        progressBar.style.backgroundColor = "#fe0000";
		progressBar.innerHTML= friendlyPercentage + "% (weak)";
        return;
    }
}

function cap(number, max) {
    if (number > max) {
        return max;
    } else {
        return number;
    }
}

function isCommonPassword(password) {
    for (i=0; i<commonPasswords.length; i++) {
        var commonPassword= commonPasswords[i];
        if (password == commonPassword) {
            return true;
        }
    }

    return false;

}

function contains(password, validChars) {
    var count= 0;

    for (i=0; i<password.length; i++) {
        var char= password.charAt(i);
        if (validChars.indexOf(char) > -1) {
            count++;
        }
    }

    return count;
}

function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    var valid = (reLong.test(entry)) || (reShort.test(entry));
    if (valid) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        day = parseInt(entry.substring(0, delim1), 10);
        mo = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        // handle two-digit year
        if (yr < 100) {
            var today = new Date();
            // get current century floor (e.g., 2000)
            var currCent = parseInt(today.getFullYear() / 100) * 100;
            // two digits up to this year + 15 expands to current century
            var threshold = (today.getFullYear() + 15) - currCent;
            if (yr > threshold) {
                yr += currCent - 100;
            } else {
                yr += currCent;
            }
        }
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    // fill field with database-friendly format
                    //fld.value = mo + "-" + day + "-" + yr;
                    return true;
                }
            }
        }
    }
    return false;
}

function initCap(word) {
	word= word.substr(0, 1).toUpperCase() + word.substr(1);
	word= word.replace(/_/g, ' ');
	return word;
}

//Run through all the fields in the form.  Check only the ones that have 'req' string in the id.
function checkForm(formId) {
	var form		= document.getElementById(formId);
	var box			= document.getElementById('box');
	var error_msg	= '<div class="header">'+
						'<p>Error: please read the below</p>'+
						'<img class="close" src="../- Manchester Warehouse_files/images/close.gif" onclick="hideBox()" alt="Close" title="Close this window" />'+
						'</div>'+
						'<div class="text">';
	var theTruth	= true;
	
	for (var i=0; i<form.elements.length; i++) {
		switch (form.elements[i].type) {
			case 'text':
				if ((form.elements[i].value == '') && (form.elements[i].id.indexOf('req') != -1)) {
					error_msg	+= '<p> &bull;&nbsp; '+initCap(form.elements[i].name)+" - Empty</p>\n";
					theTruth	= false;
				} else if (form.elements[i].id.indexOf('wedding_date') != -1) { //Check wedding date field
					if (form.elements[i].value == '') {
						error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Please Enter Date</p>\n";
						theTruth	= false;
					} else if (!checkDate(form.elements[i])) {
						error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Incorrect Format</p>\n";
						theTruth	= false;
					}
				} else if (form.elements[i].id.indexOf('email_address') != -1) { //Check email address fields
					if (form.elements[i].value == '') {
						error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Empty</p>\n";
						theTruth	= false;
					} else if (!echeck(form.elements[i].value)) {
						error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Incorrect Format</p>\n";
						theTruth	= false;
					}
				}
				break;
			case 'password':
				var pass1	= document.getElementById('req_password').value;
				var pass2	= document.getElementById('req_confirm_password').value;
				
				if ((form.elements[i].value == '') && (form.elements[i].id.indexOf('req') != -1)) {
					error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Empty</p>\n";
					theTruth	= false;
				} else if (pass1 != pass2) {
					error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Doesn\'t Match</p>\n";
					theTruth	= false;
				}
				break;
			case 'checkbox':
				if ((!form.elements[i].checked) && (form.elements[i].id.indexOf('req') != -1)) {
					error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Please Read</p>\n";
					theTruth	= false;
				}
				break;
			case 'select-one':
				if ((form.elements[i].value == '') && (form.elements[i].id.indexOf('req') != -1)) {
					error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Empty</p>\n";
					theTruth	= false;
				}
				break;
			case 'textarea':
				if ((form.elements[i].value == '') && (form.elements[i].id.indexOf('req') != -1)) {
					error_msg	+= '<p> �� '+initCap(form.elements[i].name)+" - Empty</p>\n";
					theTruth	= false;
				}
				break;
			case 'hidden':
				break;
		}
	}
	
	if (!theTruth) {
		box.innerHTML	= error_msg+'</div>';
		showBox();
	} else form.submit();
}

function formReset(id) {
	var form= document.getElementById(id);
	form.reset();
	
	hideAddress('hide'); //Hide different address if showing.
	var progressBar= document.getElementById('progressBar').style.width= '0px'; //Password strength back to 0;
}

function copy(origId, copyId) {
	var el= document.getElementById(origId);
	var el2= document.getElementById(copyId);
	
	el2.value= el.value;
}

function hideAddress(val) {
	var hide= document.getElementById('hidden');
	if (val == 'hide') {
		hide.style.display= 'none';
		//Reset the gift delivery address to the above.
		copy('req_street_address', 'req_gift_delivery_street_address');
		copy('req_suburb', 'req_gift_delivery_suburb');
		copy('req_postcode', 'req_gift_delivery_postcode');
	} else {
		hide.style.display= 'block';
	}
}