//**********************************************
//Function IsNumeric() checks if number
//**********************************************
function IsNumeric(r){
var x=r;
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
testresult=false
}
return (testresult)
}

//**************************************************
//Function getSum() returns sum of 2 input fields
//**************************************************
function getSum(x,y){
	var sum = 0;
	if (IsNumeric(x))
	{
		if (IsNumeric(y))
		{
			sum = x + y;
		}
		else
		{
			sum = x;
		}
	}
	else if (IsNumeric(y))
	{
		sum = y;
	}
	else
	{
		sum = 0;
	}
	return sum;
}
//**********************************************
//Function resetForms() resets the form
//**********************************************
function resetForms() {
	var i = 0;
   for (i=0; i < document.forms.length; i++) {
     document.forms[i].reset();
   }
}

//**********************************************
//Function checkForm() validates the form
//**********************************************
function checkForm(){
	var error1 = "";
	var error2 = "";
	var error3 = "";
	if (document.cir.companyname.value == null || document.cir.companyname.value == ""){
		error1 = "Please enter your Company Name to continue.\n"
	}
	regex = new RegExp("[\<|\>|\"|\'|\%|\;|\(|\)|\&]", "i");
	regex2 = new RegExp("[\<|\>|\"|\'|\%|\;|\(|\)|\&]", "i");
	regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	regex2=/^[a-zA-Z0-9._-]+@+(hotmail)|(yahoo)|(gmail)|(mail)|(mail2web)|(aol)|(mailinator)|(netaddress)|(lycos)|(fasmail)|(walla)|(postmaster)|(zzn)|(abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk)|(10minutemail)|(doramail)|(pookmail)|(dodgeit)|(xasamail)|(thatweb)|(myrealbox)|(excite)|(mail2world)|(emailanywhere)|(flashmail)|(hushmail)|(nameplanet)|(icqmail)|(earthlink)|(juno)|(orgoo)|(mailandnews)|(aventuremail)|(indiatimes)|(prontomail)|(fuzzmail)|(yifan)|(gurlmail)|(mauimail)|(thedoghousemail)|(emailaccount)|(returnreceipt)|(meowmail)|(personal)|(wongfaye)|(MyPersonalEmail)|(computermail)|(emailchoice)|(everymail)|(byke)|(kaxy)|(katchup)|(muchomail)|(kukamail)|(probemail)|(godmail)|(123vn)|(howlermonkey)|(jershie)|(bachelorgirl)|(zapakmail)|(aaemail)|(witchesbrew)|(iwon)|(phat)|(synacor)|(expatmail)|(stoolmail)|(nz11)|(merawalaemail)|(epenguin)|(galacmail)|(mikellmail)|(walkerware)|(zappo)|(att)|(everyone)|(usa)|(intermedia)|(123together)|(commontouch)|(softhome)|(namessecure)|(bluebottle)|(changeip)|(sailmail)|(cp)|(runbox)|(webmail)|(spamex)|(exchangemymail)|(didtheyreadit)|(mailblocks)|(hostopia)|(apptix)|(mi8)|(messagemedia)|(swishmail)|(exchangemailhosting)|(luxsci)|(pop3now)|(slawesome)|(mailstreet)|(imap4all)|(echomail)|(imailbox)|(webmail)|(mailsnare)|(electricmail)|(mail2world)|(3rsoft)|(entergroup)|(mollymail)|(smtp2go)|(lifefx)|(141)|(NetSpace1)|(usermail)|(datapimp)|(networksolutions)|(trustybox)|(greatmail)|(forevermail)|(emailtoday)|(2ndmail)|(link2exchange)|(123mail)|(ownmail)|(teamon)|(domains)|(airmail)|(one800)|(mboxes)|(thedomainworks)|(pinmail)|(cgaux)|(automailer)|(lansoft)|(yourass)|(mailcall)|(socialshake)|(slamdunknetworks)|(edson)|(inetmessaging)|(usemail)|(box100)|(postbox)|(gmsi1)|(zzbox)|(pinboard)|(mail1)|(abetteroutlook)|(ponymail)|(123m)|(javeo)|(navyweb)|(smtp-pop3-email)|(dombox)|(arrowmail)|(net-us)|(shiremail)|(pope-email)|(emailaddressstore)|(liveoffice)|(rcn)+\.+[a-zA-Z0-9.-]{2,4}$/;
	
	if (regex.test(document.cir.email.value) == false){
		error2 = "Please enter a valid email address.\n\n";
	}
	if (regex2.test(document.cir.email.value))
	{
		error3 = "Please enter a business / company email address.\n\n";
	}
	
	/*if ((document.cir.w1_bf.value == "TRUE" || document.cir.w2_bf.value == "TRUE" || document.cir.w3_bf.value == "TRUE" || document.cir.w4_bf.value == "TRUE") && ((document.cir.envelope_back_flap1.value = null || document.cir.envelope_back_flap1.value = "") && (document.cir.envelope_back_flap2.value = null || document.cir.envelope_back_flap1.value = ""))){
		error3 = "You have selected a Window Location of \"Back\" for at least 1 of the envelope windows.\nPlease enter a size for the back flap.\n";
	}
	*/
	if (error1 == "" && error2 == ""&& error3 == ""){
		return true;
	}
	else{
		alert(error1+error2+error3);
		return false;
	}
}

//***************************************************
//Function toggle() toggles a div's style between block & none
//***************************************************
function toggle(showHideDiv, switchTextDiv, windowNum) {
	var ele = document.getElementById(showHideDiv);
	var text = document.getElementById(switchTextDiv);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
			text.innerHTML = "Click Here to Expand Window #"+windowNum;
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "Collapse Window #"+windowNum;
	}
} 
//***************************************************
//Function toggle() toggles a div's style between block & none
//***************************************************
function toggleRadio(showHideDiv, button) {
	var ele = document.getElementById(showHideDiv);
	//var btn = document.getElementById(button);
	if(button == "No") {
    		ele.style.display = "none";
  	}
	else {
		ele.style.display = "block";
	}
} 

//***************************************************
//Function toggle() toggles a div's style between block & none
//***************************************************
function toggleOrder(button) {
	if(button == "No") {
		document.getElementById('divDieType').style.display='none';
		document.getElementById('divFlex').style.display='none';
  	}
	else {
		document.getElementById('divDieType').style.display='block';
	}
} 

//***************************************************
//Function toggle() toggles a div's style between block & none
//***************************************************
function toggleDie(button) {
	if(button == "Flexible Die") {
		document.getElementById('divFlex').style.display='block';
		document.getElementById('divFlex2').style.display='block';
		document.getElementById('divHolder').style.display='none';
  	}
	else if(button == "Hard Die") {
		document.getElementById('divFlex').style.display='block';
		document.getElementById('divHolder').style.display='block';
		document.getElementById('divFlex2').style.display='none';
	}
	else {
		document.getElementById('divFlex').style.display='none';
	}
} 

//***************************************************
//Function checkBackFlap() checks the Back Flap state
//***************************************************


//***************************************************
//Function checkWindowStyle() checks a bunch of stuff
//***************************************************

function checkWindowStyle(h1, h2, ctpi, checkWindow, checkRadiusDiv, checkRadius, checkTRadiusDiv, checkTRadius, wid1, wid2, wid3, postHeight, postWidth, postRadius)
{
	var c = 0; //holds the switch / case
	var e1 = document.getElementById(h1);
	var e2 = document.getElementById(h2);
	var htpi = document.getElementById(ctpi);
	var cwin = document.getElementById(checkWindow);
	
	var craddiv = document.getElementById(checkRadiusDiv);
	var crad = document.getElementById(checkRadius);
	var ctraddiv = document.getElementById(checkTRadiusDiv);
	var ctrad = document.getElementById(checkTRadius);
	var widid1 = document.getElementById(wid1);
	var widid2 = document.getElementById(wid2);
	var wtpi = document.getElementById(wid3);
	var pheight = document.getElementById(postHeight);
	var pwidth = document.getElementById(postWidth);
	var pradius = document.getElementById(postRadius);

	var width = 0;
	width = getSum(Number(widid1.value),Number(widid2.value));
	pwidth.value = width;
	
	var height = 0;
	height = getSum(Number(e1.value),Number(e2.value));
	pheight.value = height;
	
	switch (Number(cwin.value))
	{
		case 1:
		c = 1;
		//square end, round corner
		e1.disabled = false;
		e2.disabled = false;
		widid1.disabled = false;
		widid2.disabled = false;
		htpi.value = null;
		htpi.disabled = true;
		wtpi.value = null;
		wtpi.disabled = true;
		crad.value = null;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		if (0 == height)
		{
			ctrad.value = 0;
		}
		else if (height < 0.375)
		{
			if (height <= width)
			{
				ctrad.value = 0.09375;
			}
		}
		else if (height < 0.5625)
		{
			if (height <= width)
			{
				ctrad.value = 0.125;
			}
		}
		else if (height <= 0.6875)
		{
			if (height <= width)
			{
				ctrad.value = 0.1875;
			}
		}
		else
		{
			ctrad.value = 0.25;
		}
		pradius.value = ctrad.value;
		break;
		
		case 2:
		c = 2;
		//round end
		e1.disabled = false;
		e2.disabled = false;
		widid1.disabled = false;
		widid2.disabled = false;
		htpi.value = null;
		htpi.disabled = true;
		wtpi.value = null;
		wtpi.disabled = true;
		crad.value = null;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		ctrad.value = (height / 2);
		pradius.value = ctrad.value;
		break;
		
		case 3:
		c = 3;
		//sharp, 90 degree corner
		e1.disabled = false;
		e2.disabled = false;
		widid1.disabled = false;
		widid2.disabled = false;
		htpi.value = null;
		htpi.disabled = true;
		wtpi.value = null;
		wtpi.disabled = true;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		ctrad.value = 0;
		pradius.value = ctrad.value;
		break;
		
		case 4:
		c = 4;
		//circle
		e1.disabled = false;
		e2.disabled = false;
		widid1.value = e1.value;
		widid2.value = e2.value;
		widid1.disabled = true;
		widid2.disabled = true;
		htpi.value = null;
		htpi.disabled = true;
		wtpi.value = null;
		wtpi.disabled = true;
		
		crad.value = null;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		ctrad.value = (height / 2);
		pradius.value = ctrad.value;
		pwidth.value = height;
		break;
		
		case 5:
		c = 5;
		//vertical perf
		//deactivate width and set to 0
		htpi.value = null;
		htpi.disabled = true;
		wtpi.disabled = false;
		wtpi.options[4].selected = true;
		e1.disabled = false;
		e2.disabled = false;
		widid1.value = 0;
		widid2.value = 0;
		width = 0;
		widid1.disabled = true;
		widid2.disabled = true;
		//deactivate radius and set to 0
		crad.value = null;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		ctrad.value = "0";
		pradius.value = ctrad.value;
		break;
		
		case 6:
		c = 6
		//horizontal perf
		e1.value = 0;
		e2.value = 0;
		height = 0;
		htpi.disabled = false;
		wtpi.value = null;
		wtpi.disabled = true;
		htpi.options[4].selected = true;
		e1.disabled = true;
		e2.disabled = true;
		widid1.disabled = false;
		widid2.disabled = false;
		crad.value = null;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		ctrad.value = "0";
		pradius.value = ctrad.value;
		break;
		
		case 7:
		c = 7;
		//standard
		/*******************************
		e1.disabled = false;
		e2.disabled = false;
		widid1.disabled = false;
		widid2.disabled = false;
		ctrad.value = null;
		ctraddiv.style.display = "none";
		craddiv.style.display = "block";
		crad.options[0].selected = true;
		pradius.value = crad.value;
		********************************/
		e1.disabled = false;
		e2.disabled = false;
		widid1.disabled = false;
		widid2.disabled = false;
		crad.value = null;
		craddiv.style.display = "none";
		ctraddiv.style.display = "block";
		ctrad.readOnly = true;
		htpi.value = null;
		htpi.disabled = true;
		wtpi.value = null;
		wtpi.disabled = true;
		if (0 == height)
		{
			ctrad.value = 0;
		}
		else if (height < 0.375)
		{
			if (height <= width)
			{
				ctrad.value = 0.09375;
			}
		}
		else if (height < 0.5625)
		{
			if (height <= width)
			{
				ctrad.value = 0.125;
			}
		}
		else if (height <= 0.6875)
		{
			if (height <= width)
			{
				ctrad.value = 0.1875;
			}
		}
		else
		{
			ctrad.value = 0.25;
		}
		pradius.value = ctrad.value;
		break;
		
	}

}

//**********************************************************
//Function checkState() checks if the check box is checked
//     and displays hides a div accordingly
//**********************************************************
function checkState(w,x)
{
	var box = document.getElementById(w); //var box is the check box element being monitored
	var divid = document.getElementById(x); //var divid
	
	if  (box.checked)
	{
		divid.style.display = "block";
	}
	else
	{
		divid.style.display = "none";
	}
}

//**********************************************************
//Function getLocation()
//     
//**********************************************************
function addValues(x,y,z)
{
	var loc1 = document.getElementById(x);
	var loc2 = document.getElementById(y);
	var locPost = document.getElementById(z);
	
	locPost.value = getSum(Number(loc1.value), Number(loc2.value));
}

//**********************************************************
//Function setLeadEdge() checks values of "cylinder" and "die type"
//     and sets "LeadEdge" accordingly
//**********************************************************
function setLeadEdge(x,y,z,a)
{
	var cylinder = document.getElementById(x); //var cylinder
	var die = document.getElementById(y); //var die is the die_type
	var milli = document.getElementById(z);
	var lead = document.getElementById(a);
	
	if (cylinder.options[7].selected == true)
	{
		lead.value = .5;
		milli.value = "FALSE";
	}
	else if (cylinder.options[1].selected == true)
	{
		if (die.options[6].selected == true)
		{
			lead.value = (23/32);
			milli.value = "FALSE";
		}
		else if (die.options[5].selected == true)
		{
			lead.value = (7/8);
			milli.value = "FALSE";
		}
		else
		{
			lead.value = 12;
			milli.value = "TRUE";
		}
	}
	else if (cylinder.options[5].selected == true)
	{
		if (die.options[7].selected == true)
		{
			lead.value = (3/8);
			milli.value = "FALSE";
		}
		else
		{
			lead.value = 12;
			milli.value = "TRUE";
		}
	}
	else
	{
		lead.value = 12;
		milli.value = "TRUE";
	}
}