function ValidateEmail(obj, msg)
{
	var splitted = obj.value.match("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");    
    
    if(splitted == null) {
		alert(msg);
		obj.focus();
		return false;
	}
    
    return true;
}

function ValidateField(obj, msg)
{
	var retun_value = true;
	
	if(obj.type == "text" && obj.value == "") {
		retun_value = false;
	}
	else if(obj.type == "select-one" && obj.selectedIndex == 0) {
		retun_value = false;
	}
	else if(obj.type == "checkbox" && !obj.checked) {
		retun_value = false;
	}
		
	if(!retun_value) { alert(msg); obj.focus(); }		
	return retun_value;
}
		
function _page_ClientValidate(orderForm, submitObject)
{
	// disable submit button
	submitObject.disabled = true;
	
	//terms & conditions box mast be checked
	if( !ValidateField(orderForm.chkTermsAndConditions, "Please, check our terms & conditions box.") ) { return false; }	
	
	//Customer Shipping Information
	if( !ValidateField(orderForm.txtCompany, "Company is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtFirstName, "First Name is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtLastName, "Last Name is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtAddress1, "Address 1 is required field.") ) { return false; }	
	if( !ValidateField(orderForm.txtCity, "City is required field.") ) { return false; }	
	if( !ValidateField(orderForm.ddlState, "Please select a state.") ) { return false; }
	if( !ValidateField(orderForm.txtZip, "Zip Code is required field.") ) { return false; }	
	if( !ValidateField(orderForm.txtPhone, "Phone is required field.") ) { return false; }	
	if( !ValidateField(orderForm.txtEmail, "Email is required field.") ) { return false; }
	if( !ValidateEmail(orderForm.txtEmail, "Enter a valid Email address.") ) { return false; }
	//Customer Billing Information
	if( !ValidateField(orderForm.txtCompany_bill, "Company is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtFirstName_bill, "First Name is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtLastName_bill, "Last Name is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtAddress1_bill, "Address 1 is required field.") ) { return false; }	
	if( !ValidateField(orderForm.txtCity_bill, "City is required field.") ) { return false; }	
	if( !ValidateField(orderForm.ddlState_bill, "Please select a state.") ) { return false; }
	if( !ValidateField(orderForm.txtZip_bill, "Zip Code is required field.") ) { return false; }
	if( !ValidateField(orderForm.txtPhone_bill, "Phone is required field.") ) { return false; }	
	if( orderForm.txtEmail_bill.value != "" && !ValidateEmail(orderForm.txtEmail_bill, "Enter a valid Email address.") ) { return false; }	
	
	// check for products
	var numOfProd = 0; var tempObj;
	for(var i=0; i<orderForm.elements.length; i++) {		
		tempObj = orderForm.elements[i];
		if(tempObj.type=="checkbox" && tempObj.name.indexOf("chk_order_")==0 && tempObj.checked) {
			numOfProd++;
		}
	}
	if(numOfProd == 0) { alert("Please select at least one product."); return false; }
	
	return true;
}

function SameAsShipping(orderForm)
{
	orderForm.ddlState_bill.selectedIndex = -1;		
	orderForm.txtCompany_bill.value = orderForm.txtCompany.value;
	orderForm.txtFirstName_bill.value = orderForm.txtFirstName.value;
	orderForm.txtLastName_bill.value = orderForm.txtLastName.value;
	orderForm.txtAddress1_bill.value = orderForm.txtAddress1.value;
	orderForm.txtAddress2_bill.value = orderForm.txtAddress2.value;	
	orderForm.txtCity_bill.value = orderForm.txtCity.value;
	orderForm.ddlState_bill.selectedIndex = orderForm.ddlState.selectedIndex;
	orderForm.txtZip_bill.value = orderForm.txtZip.value;
	orderForm.txtPhone_bill.value = orderForm.txtPhone.value;
	orderForm.txtFax_bill.value = orderForm.txtFax.value;
	orderForm.txtEmail_bill.value = orderForm.txtEmail.value;
}

function _submitTheForm(_form)
{	
	if (typeof Server_Name!='undefined' && (Server_Name.toLowerCase()=='dodge.construction.com' || Server_Name.toLowerCase()=='www.dodge.construction.com') ) {
		_form.action = "http://greensource.construction.com/MediaDataAccess/WebOrderForm.aspx";
	}
	
	// submit the form
	_form.submit();
	
	return true;
}
