var type = "IE";	//Variable used to hold the browser name

BrowserCheck();

//detects the capabilities of the browser
function BrowserCheck() {
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";		//Opera
	else if (document.all) type="IE";														//Internet Explorer e.g. IE4 upwards
	else if (document.layers) type="NN";													//Netscape Communicator 4
	else if (!document.all && document.getElementById) type="MO";							//Mozila e.g. Netscape 6 upwards
	else type = "IE";		//I assume it will not get here
}

function show_pleasewait() {
	ShowPleaseWait("page", "hidden");
	//ShowPleaseWait("Layer1", "hidden");
	//ShowPleaseWait("Layer2", "hidden");
	//ShowPleaseWait("Layer3", "hidden");
	ShowPleaseWait('pleasewaitdiv','visible');
}

function ShowPleaseWait(id, action){
	if (type=="IE") 
	{ 
		if(eval("document.all."+ id) != null) {
			eval("document.all." + id + ".style.visibility='" + action + "'");
	}
	}

	if (type=="NN") 
	{
		if(eval("document." + id) != null)
			eval("document." + id + ".visibility='" + action + "'");
	}

	if (type=="MO" || type=="OP") 
	{
		if(eval("document.getElementById('" + id + "')") != null)
			eval("document.getElementById('" + id + "').style.visibility='" + action + "'");
	}
}

function do_submit() {
	show_pleasewait();
        return true;
}



function validate(form)
{
	if(form.person.value == "")
	{
		alert("You must enter valid Person Name");
		form.person.focus();
		return false;
	}
	if(form.company.value == "")
	{
		alert("You must enter valid Company Name");
		form.company.focus();
		return false;
	}
	if(form.phone.value == "")
	{
		alert("You must enter valid Phone Number");
		form.phone.focus();
		return false;
	}
	if(!emailCheck(form.email.value))
		return false;

	if(form.address.value == "")
	{
		alert("You must enter valid address");
		form.address.focus();
		return false;
	}
	if(form.city.value == "")
	{
		alert("You must enter valid City Name");
		form.city.focus();
		return false;
	}
	if(form.zipcode.value == "")
	{
		alert("You must enter valid zip code");
		form.zipcode.focus();
		return false;
	}
	if(form.file1.value == "" && form.file2.value == "" && form.file3.value == "" && form.file4.value == "") 
	{
		alert("You must enter valid file names in upload file field.");
		form.file1.focus();
		return false;
	}

	return true;
}

function emailCheck (emailStr) 
{
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}


function validate_bcards(form)
{
	var quantity;
	var i = 0;alert('0');
	if(validate(form))
	{
		for(i=0;i<form.bcards.length;i++)
			if(form.bcards[i].checked)
				quantity = form.bcards[i].value;
				
		alert('1');do_submit();alert('2');
		window.scrollTo(0,0);

		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + form.ordertype.value;
				
		alert(form.action);form.submit();
	}
	else
		return false;				
}



function validate_brochures(form)
{
	var quantity;
        var ordertype;
	var i = 0;

	if(validate(form))
	{
		
		for(i=0;i<form.brochure.length;i++)
			if(form.brochure[i].checked)
			{
				quantity = form.brochure[i].value;
				ordertype = form.ordertype.value;
				if(i<=3)
					ordertype = ordertype + " 80lb Gloass Book";
				else if(i>3 && i<=7)
					ordertype = ordertype + " 100lb Gloass Book";
				else
					ordertype = ordertype + "  80lb Cover";
			
				break;
			}
			

		do_submit();
		window.scrollTo(0,0);
			
	
		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + ordertype;
				
		//alert(form.action);
	}
	else
		return false;				
}

function validate_catalogs(form)
{
	var quantity;
	var ordertype;
	var i = 0;
	if(validate(form))
	{
		for(i=0;i<form.catalogs.length;i++)
			if(form.catalogs[i].checked)
			{
				quantity = form.catalogs[i].value;
				ordertype = form.ordertype.value;
				if(i>3)
					ordertype = ordertype + "-8 Pg. 100lb Gloss Book"
				else
					ordertype = ordertype + "-4 Pg. 100lb Gloss Book"
				break;
			}
				
		do_submit();
		window.scrollTo(0,0);
	
		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + ordertype;
				
		//alert(form.action);
	}
	else
		return false;				
}


function validate_letterheads(form)
{
	var quantity;1
	var i = 0;
	if(validate(form))
	{
		for(i=0;i<form.letterheads.length;i++)
			if(form.letterheads[i].checked)
				quantity = form.letterheads[i].value;
				
		//alert('1');
		do_submit();
		window.scrollTo(0,0);
		
		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + form.ordertype.value;
				
		//alert(form.action);
	}
	else
		return false;				
}


function validate_postcards(form)
{
	var quantity;
	var ordertype;
	var i = 0;
	if(validate(form))
	{
		for(i=0;i<form.postcards.length;i++)
			if(form.postcards[i].checked)
			{
				quantity = form.postcards[i].value;
				ordertype = form.ordertype.value;
				if(i<=3)
					ordertype = ordertype + " of size(4\"x6\")";
				else if(i>3 && i<=7)
					ordertype = ordertype + " of size(5\"x7\")";
				else
					ordertype = ordertype + " of size(5.5\"x8.5\")";
				break;
			}
				
	
		do_submit();
		window.scrollTo(0,0);
	
		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + ordertype;
				
		//alert(form.action);
	}
	else
		return false;				
}


function validate_posters(form)
{
	var quantity;
	var ordertype;
	var i = 0;
	if(validate(form))
	{
		for(i=0;i<form.posters.length;i++)
			if(form.posters[i].checked)
			{
				quantity = form.posters[i].value;
				ordertype = form.ordertype.value;
				if(i<=3)
					ordertype = ordertype + " of size(13\"x19\")";
				else if(i>3 && i<=7)
					ordertype = ordertype + " of size(18\"x24\")";
				else
					ordertype = ordertype + " of size(19\"x27\")";
				break;
			}
				
		do_submit();
		window.scrollTo(0,0);

		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + ordertype;
				
		//alert(form.action);
	}
	else
		return false;				
}

function validate_prefolders(form)
{
	var quantity;1
	var i = 0;
	if(validate(form))
	{
		for(i=0;i<form.prefolders.length;i++)
			if(form.prefolders[i].checked)
				quantity = form.prefolders[i].value;
				
		do_submit();
		window.scrollTo(0,0);

		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + form.ordertype.value;
				
		//alert(form.action);
	}
	else
		return false;				
}






function validate_singlesheets(form)
{
	var quantity;1
	var i = 0;
	if(validate(form))
	{
		for(i=0;i<form.singlesheets.length;i++)
			if(form.singlesheets[i].checked)
				quantity = form.singlesheets[i].value;
	
		do_submit();
		window.scrollTo(0,0);
				
	
		form.action = "http://99.61.76.146:8080/servlets-examples/servlet/Edit_four?person=" +
					form.person.value + "&company=" + form.company.value +
					"&quantity=" + quantity + 
					"&phone=" + form.phone.value + 
					"&email=" + form.email.value +
					"&address=" + form.address.value + 
					"&city=" + form.city.value +
					"&state=" + form.state.value +
					"&zipcode=" + form.zipcode.value +
					"&country=" + form.country.value +
					"&comments=" + form.comments.value +
					"&ordertype=" + form.ordertype.value;
				
		//alert(form.action);
	}
	else
		return false;				
}

