function validemail(txt) {
	var str = txt.value; // email string
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
		return true;
	}
	return false;
}

function validateContact() {
	//contact_form: tbName tbEmail tbPhone tbPhoneCode tbFax tbFaxCode
	if (contact_form.tbName.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide a name.");
		contact_form.tbName.focus();
		return false;
	}
	if (contact_form.tbEmail.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide an email address.");
		contact_form.tbEmail.focus();
		return false;
	} else {
		if (!validemail(contact_form.tbEmail)) {
			alert("Please provide a valid email address.");
			contact_form.tbEmail.focus();			
			return false;
		}		
	}
	if (contact_form.tbPosition.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide a position.");
		contact_form.tbPosition.focus();
		return false;
	}
	if (contact_form.tbPhoneCountryCode.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide an international dialing code for the phone number.");
		contact_form.tbPhoneCountryCode.focus();
		return false;
	}
	if (contact_form.tbPhoneCode.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide a dialing code for the phone number.");
		contact_form.tbPhoneCode.focus();
		return false;
	}
	if (contact_form.tbPhone.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide a phone number.");
		contact_form.tbPhone.focus();
		return false;
	}
	if (contact_form.tbFaxCountryCode.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide an internationl dialing code for the fax number.");
		contact_form.tbFaxCountryCode.focus();
		return false;
	}
	if (contact_form.tbFaxCode.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide a dialing code for the fax number.");
		contact_form.tbFaxCode.focus();
		return false;
	}
	if (contact_form.tbFax.value.replace(/^\s+|\s+$/g, '') == "") {
		alert("Please provide a fax number.");
		contact_form.tbFax.focus();
		return false;
	}	
	return true;
}

function txtCheck(txt) {
    var trimmed = txt.value.replace(/^\s+|\s+$/g, '') ;
    if (trimmed == "") {
        alert("Please provide all required fields.");
        txt.focus();
        return false;
    }

    return true;
}
function txtCheckHighlight(txt) {
    var trimmed = txt.value.replace(/^\s+|\s+$/g, '') ;
    if (trimmed == "") {
        txt.focus();
        txt.style.backgroundColor='yellow'; 
        return false;
    }
     txt.style.backgroundColor='white';
    return true;
}

function rbCheck(rb1,rb2) {
    if (!rb1.checked && !rb2.checked) {
        alert("Please select all required options.");
        rb1.focus();
        return false;
    }
    

    return true;
}

function rbCheckHighlight(rb1,rb2) {
    if (!rb1.checked && !rb2.checked) {
        rb1.focus();
        rb1.style.backgroundColor='yellow';
        rb2.style.backgroundColor='yellow';
        return false;
    }
    rb1.style.backgroundColor='#efefef';
    rb2.style.backgroundColor='#efefef';

    return true;
}

//Validation for linear_actuators_ques.htm
function validateLinier() {
	//linear_form: tbCompany, tbPrimary, tbContact, tbPosition, tbEmail, tbCell, tbTel1, tbTel2, tbFax1, tbFax2, tbApplication, rbPush, rbPull
	//tbQuantity, tbWhen, tbStroke, tbAc, tbDc, tbMaxPush, tbMaxPull, tbStrokePush, tbStrokePull, tbActivations, rbClutchYes, rbClutchNo
    //rbLimitYes, rbLimitNo, rbSpaceYes, rbSpaceNo, tbEnvironment, tbOther, tbPerceived
	var toReturn = true;
       
        
	if (toReturn) toReturn = txtCheck(linear_form.tbCompany);
	if (toReturn) toReturn = txtCheck(linear_form.tbPrimary);
	if (toReturn) toReturn = txtCheck(linear_form.tbContact);
	if (toReturn) toReturn = txtCheck(linear_form.tbPosition);
	if (toReturn) {
	    toReturn = txtCheck(linear_form.tbEmail);
	    //Check valid email
	    if (toReturn) {
            if (!validemail(linear_form.tbEmail)) {
                alert("Please provide a valid email address.");
                linear_form.tbEmail.focus();
                toReturn = false;
            }
	    }
	}
	if (toReturn) toReturn = txtCheck(linear_form.tbCell);
	if (toReturn) toReturn = txtCheck(linear_form.tbTel1);
	if (toReturn) toReturn = txtCheck(linear_form.tbTel2);
	if (toReturn) toReturn = txtCheck(linear_form.tbApplication);
	if (toReturn) toReturn = rbCheck(linear_form.rbPush,linear_form.rbPull);
	if (toReturn) toReturn = txtCheck(linear_form.tbQuantity);
	if (toReturn) toReturn = txtCheck(linear_form.tbWhen);
	if (toReturn) toReturn = txtCheck(linear_form.tbStroke);
	if (toReturn) toReturn = txtCheck(linear_form.tbAc);
	if (toReturn) toReturn = txtCheck(linear_form.tbDc);
	if (toReturn) toReturn = txtCheck(linear_form.tbMaxPush);
	if (toReturn) toReturn = txtCheck(linear_form.tbMaxPull);
	if (toReturn) toReturn = txtCheck(linear_form.tbStrokePush);
	if (toReturn) toReturn = txtCheck(linear_form.tbStrokePull);
	if (toReturn) toReturn = txtCheck(linear_form.tbActivations);
	if (toReturn) toReturn = rbCheck(linear_form.rbClutchYes,linear_form.rbClutchNo);
	if (toReturn) toReturn = rbCheck(linear_form.rbLimitYes,linear_form.rbLimitNo);
	if (toReturn) toReturn = rbCheck(linear_form.rbSpaceYes,linear_form.rbSpaceNo);
	if (toReturn) toReturn = txtCheck(linear_form.tbEnvironment);
	
	return toReturn;
}

//Valdiation for geared_motors_ques.htm
function validateGeared() {

    //geared_form: tbCompany, tbPrimary, tbContact, tbPosition, tbEmail, tbCell, tbTel1, tbTel2, tbFax1, tbFax2, tbApplication, tbNew, tbQuantity
    //tbWhen, rbS1, rbS2, slGearbox, tbPowerKw, tbPowerRpm, tbPowerVoltage, tbPowerPhase, slAcdc, tbAcdc, tbTorque, slTorque, slDuty, tbRunning, tbStop,
    //tbDutyCycle, slRotation, tbIp, slAmbient, slTypeWinding, slMounting, rbElectroYes, rbElectroNo, slThermal, tbAltitude, tbAbmientTemp
    //SpaceLimitYes, SpaceLimitNo, tbEnvironment, tbProtection, tbCooling, tbOther, tbPerceived
     var toReturn = false;
     
       var company=txtCheckHighlight(geared_form.tbCompany); 
       var primary=txtCheckHighlight(geared_form.tbPrimary);
       var contact=txtCheckHighlight(geared_form.tbContact);
       var position=txtCheckHighlight(geared_form.tbPosition);
       //check email
       var email = txtCheckHighlight(geared_form.tbEmail);
	    //Check valid email
	    if (email) {
            if (!validemail(geared_form.tbEmail)) {
                alert("Please provide a valid email address.");
                geared_form.tbEmail.focus();
                geared_form.tbEmail.style.background='yellow';
                email = false;
            }
	    }
      var cell=txtCheckHighlight(geared_form.tbCell);
      var tel1=txtCheckHighlight(geared_form.tbTel1);
      var tel2=txtCheckHighlight(geared_form.tbTel2);
      var app= txtCheckHighlight(geared_form.tbApplication);
      var quantity= txtCheckHighlight(geared_form.tbQuantity);
      var when=txtCheckHighlight(geared_form.tbWhen);
      var duty=rbCheckHighlight(geared_form.rbS1,geared_form.rbS2);
      var PowerKw=txtCheckHighlight(geared_form.tbPowerKw);
      var PowerRpm=txtCheckHighlight(geared_form.tbPowerRpm);
      var Torque=txtCheckHighlight(geared_form.tbTorque);
      var PowerVoltage=txtCheckHighlight(geared_form.tbPowerVoltage);
      var PowerPhase=txtCheckHighlight(geared_form.tbPowerPhase);
      var Acdc=txtCheckHighlight(geared_form.tbAcdc);
      var VSD = rbCheckHighlight(geared_form.rbVsdDrivenYes,geared_form.rbVsdDrivenNo);
	if (VSD) {
	    if (geared_form.rbVsdDrivenYes.checked) {
		VSD = rbCheckHighlight(geared_form.rbVsdQuoteYes,geared_form.rbVsdQuoteNo);
	    }
	}

      if(company && primary && contact && position && email && cell && tel1 && tel2 && app && quantity && when && duty && PowerKw && PowerRpm && Torque && PowerVoltage && Acdc && VSD)
        return true;
      else{
         alert("Please provide all highlighted fields.");
         return false; 
      }
    
}

function enableVsdQuote() {
	geared_form.rbVsdQuoteYes.disabled = false;
	geared_form.rbVsdQuoteNo.disabled = false;
}

function disableVsdQuote() {
	geared_form.rbVsdQuoteYes.disabled = true;
	geared_form.rbVsdQuoteNo.disabled = true;
}
