////////////////////////////////////////
///// Custom JS for Wardrobes Plus!
///// 2E-Volve
///// 26/2/2008
////////////////////////////////////////
///// Debug?
var debug = false;
////////////////////////////////////////

// When the pages load, perform this
function page_tasks() {
	// Reset Swatches if they exist on page load
	reset_swatch();
}

// Reset the swatch form, if it exists
function reset_swatch() {
	// Set the Field as a Variable
	var s_values = document.getElementById('samples');
	
	// If it exists, clear it
	if (s_values) {
		document.getElementById('samples').value = "";
		if(debug==true){alert("Hidden Field Was Reset");}
	} 
}

// When someone picks a swatch, add it to a field and chnge border color
// When clicked for the second time, remove it and change the border back
function swatch_pick(e_id) {
	// If the item is in the text box, change border to white and remove it
	
	if(debug==true){alert(document.getElementById('swatches_chosen').innerHTML);}
	
	if (document.getElementById('samples').value.match(e_id+",")) {
		// Set regex variable
		var sample_regex = e_id+", ";
		if(debug==true){alert("Replaced Item: " + sample_regex);}
		if(debug==true){alert("Sample ID: " + e_id);}
		
		// Remove the Item
		document.getElementById('samples').value = document.getElementById('samples').value.replace(sample_regex, "");
		var ihtml_rep = e_id + ", ";
		if(debug==true){alert("To Be Replaced: " + ihtml_rep);}
		document.getElementById('swatches_chosen').innerHTML = document.getElementById('swatches_chosen').innerHTML.replace(ihtml_rep, "");
		
		// Change Border to White (Not Selected)
		document.getElementById(e_id).style.borderColor = "#FFFFFF";
	} else {
		// Insert the Item
		if (document.getElementById('samples').value == "" || document.getElementById('samples').value == null) {
			// It's empty
			if(debug==true){alert("Field is empty.");}
			document.getElementById('samples').value = e_id + ", ";
			document.getElementById('swatches_chosen').innerHTML = e_id + ", ";
		} else {
			// It's not empty
			if(debug==true){alert("Field is NOT empty.");}
			document.getElementById('samples').value = document.getElementById('samples').value + e_id + ", ";
			document.getElementById('swatches_chosen').innerHTML = document.getElementById('swatches_chosen').innerHTML + e_id + ", ";
		}
		
		// Change Border to Red (Selected)
		document.getElementById(e_id).style.borderColor = "#FF0000";
	}
}


// Only Numbers
function numbersOnly(el) {
	el.value = el.value.replace(/[^0-9]/g, "");
}

// Submit some forms!
function submitform(thing1, thing2) {
	
	if (checkForm2(thing2) == false) {
		
	} else {	
		thing2.submit();
	}
}



// Order Wizard Specifics
function checkForm(stuff){
	// Initial Variables
	d1 = stuff;
	
	// Test
	//alert("w-"+form.gapWidth.value+"h-"+form.gapHeight.value);
	//alert(d1);
	
	////////////////////////////////////////////////////////////
	///// Order Wizard Start
	////////////////////////////////////////////////////////////
	// If value is less than 250mm
	if(d1.gapWidth.value.length == 0 || d1.gapWidth.value <= 249){
		alert('Gap width must have a value of 250mm or greater.');
		d1.gapWidth.focus();
		return false;
	}
	else if(d1.gapHeight.value.length==0 || d1.gapHeight.value <= 249){
		alert('Gap height must have a value of 250mm or greater.');
		d1.gapHeight.focus();
		return false;
	}
	
	// if value is greater than 5000mm
	if(d1.gapWidth.value >= 5001){
		alert('Gap width must not be greater than 5000mm.');
		d1.gapWidth.focus();
		return false;
	}
	else if(d1.gapHeight.value >= 2501){
		alert('Gap height must not be greater than 2500mm.');
		d1.gapHeight.focus();
		return false;
	}
}

// Samples Specifics
function checkForm2(stuff){
	// Initial Variables
	d1 = stuff;
	
	// Test
	//alert(d1);
	//alert(d1.delivery_address4.value);
	
	////////////////////////////////////////////////////////////
	///// Customer Information
	////////////////////////////////////////////////////////////
	// If it is empty
	if(d1.name.value == "" || d1.name.value == null){
		alert('Please fill in the Name field.');
		d1.name.focus();
		return false;
	}
	else if(d1.phone.value == "" || d1.phone.value == null){
		alert('Please fill in the Phone Number field.');
		d1.phone.focus();
		return false;
	}
	else if(d1.email.value == "" || d1.email.value == null){
		alert('Please fill in the Email field.');
		d1.email.focus();
		return false;
	}
	else if(d1.delivery_address1.value == "" || d1.delivery_address1.value == null){
		alert('Please fill in the Address 1 field.');
		d1.delivery_address1.focus();
		return false;
	}
	else if(d1.delivery_address3.value == "" || d1.delivery_address3.value == null){
		alert('Please fill in the Town/City field.');
		d1.delivery_address3.focus();
		return false;
	}
	else if(d1.delivery_address5.value == "" || d1.delivery_address5.value == null){
		alert('Please fill in the Post Code field.');
		d1.delivery_address5.focus();
		return false;
	}
	else if(d1.delivery_address5.length > 10){
		alert('The Post Code you entered is too long.');
		d1.delivery_address5.focus();
		return false;
	}
}