var flag = true;
var flagAdd = true;

function submitForm () {
	if (flag) document.forms[0].submit();
	return;
}

function checkPayment(flagPayment) {
	flagPayment = document.getElementById("Payment Type").value;
	if (flagPayment == 'PO') flagAdd = false; else flagAdd = true;
return;
}

function checkFields() {
	if (!flagAdd) return;
	if (!flag) return;
	numArg = checkFields.arguments.length;
  for (var i = 0; i < numArg; i++) {
    this[i] = checkFields.arguments[i];
		if (!setName(document.getElementById(this[i]).value,this[i])) {
		  flag = false;
			return;
		}
	}
	flag = true;
	return;
}

function checkEmail() {
	if (!flag) return;
	numArg = checkEmail.arguments.length;
  for (var i = 0; i < numArg; i++) {
    this[i] = checkEmail.arguments[i];
    if (!setMail(document.getElementById(this[i]).value)) { 
	    document.getElementById(this[i]).focus(); 
		  flag = false;
		  return; 
		}
	}
	flag = true;
	return;
}

function checkSelect() {
	if (!flag) return;
	numArg = checkSelect.arguments.length;
  for (var i = 0; i < numArg; i++) {
    this[i] = checkSelect.arguments[i];
    if (!setSelect(document.getElementById(this[i]).id,document.getElementById(this[i]).value)) {
			document.getElementById(this[i]).focus(); 
		  flag = false;
		  return; 
		}
	}
	flag = true;
	return;
}

function checkCheckbox() {
	if (!flag) return;
	numArg = checkCheckbox.arguments.length;
  for (var i = 0; i < numArg; i++) {
    this[i] = checkCheckbox.arguments[i];
		if (!setCheckbox(document.getElementById(this[i]).checked)) {
		  flag = false;
			return;
		}
	}
	flag = true;
	return;
}

function setName(data,nameOfField)           
{
	if(data =='') {
		alert('Enter ' + nameOfField); document.getElementById(nameOfField).focus();
		return false;
	}
	else return true;
}

function setCheckbox(data)           
{
	if(!data) { 
		alert(" \"I accept the terms...\" is a required field."); 
		return false;
	}
	else return true;
}

function setSelect(nameOfField,idField)           
{
	if(idField =='-- Please Make a Selection --' || idField == '--None--') {
		alert ("Please Make a Selection of \"" + nameOfField + "\"");
		return false;
	}
	else return true;
}

var eSimbol = new Array(), flag = 0;
eSimbol[0]  = '`'; eSimbol[1]  = '~'; eSimbol[2]  ='!'; eSimbol[3]   = '#'; eSimbol[4]  = '"'; eSimbol[5]  = '?'; 
eSimbol[6]  = '$'; eSimbol[7]  = ';'; eSimbol[8]  = '%'; eSimbol[9]  ='^'; eSimbol[10]  = ':'; eSimbol[11] = '&'; 
eSimbol[12] = '?'; eSimbol[13] = '*'; eSimbol[14] = '('; eSimbol[15] = ')'; eSimbol[16] = '\\'; eSimbol[17] = '+'; 
eSimbol[18] = '|'; eSimbol[19] = ' '; eSimbol[20] = '/'; eSimbol[21] = "'"; eSimbol[22] = '{'; eSimbol[23] = '}'; 
eSimbol[24] = '['; eSimbol[25] = ']'; eSimbol[26] = ','; eSimbol[27] = '<'; eSimbol[28] = ' >';  

function setMail(msg)           
{
var e='', es = '', ed = '', em = '';
if(msg !='' && msg != 'Enter Email') {
	if(msg.indexOf('@') ==-1){ e += 'There is no symbol @. '}
	if(msg.lastIndexOf( '.') < msg.indexOf('@')){ed+='\nEnter a name of the domain.'}
	if(msg.lastIndexOf( '@') > msg.indexOf('@')){em+='\nIt is too much symbols @.'}
	//if(msg.lastIndexOf( '.' , msg.indexOf('@')) != -1){e+='Point up to a symbol @. '}

	// All errors
	es += simbols(msg);  
	if ((e + es + ed + em)!='') {
		alert(e + es + ed + em);
		return false;
	}
	else return true;
}
else alert('Enter Email');

document.forms[0].email.focus();
return false;
}


function simbols(simb)  
{
var i , j, stringErrors = '';  
for(i = 0; i<=simb.length - 1; i++)
 {
    for(j= 0; j <= eSimbol.length - 1; j++)
   {
  //++
    if(simb.charAt(i) == eSimbol[j] )
   {
         stringErrors += simb.charAt(i);
    }
   //++
   }
  }
if(stringErrors !=''){
return ('\nErrors: ' + stringErrors);}
else{return ''}
}


function writeForm(param) {
	if(!param) document.write("<form name=\"DownloadForm\" ACTION=\"\" method=\"POST\">");
	else document.write("</form");
	return;
}

function checkFlag()
{
//	var numFields = document.forms[0].elements.length;
//	document.forms[0].elements[numFields-1].name == "Name") 
//		if (document.getElementById("textbot").value != "") document.forms[0].action = "";
//		else document.forms[0].action = actForm;
//	if (document.forms[0].action == "" && actForm != "") document.forms[0].action = actForm;
	
	if (!flag) flag = true;
//	alert(flag);
	return;
}

function checkNumeric()
{
	if (!flag) return;
	numArg = checkNumeric.arguments.length;
	minval = checkNumeric.arguments[numArg-3];
	maxval = checkNumeric.arguments[numArg-2];
	comma  = checkNumeric.arguments[numArg-1];

  for (var i = 0; i < numArg-3; i++) {
    this[i] = checkNumeric.arguments[i];
		objValue = document.getElementById(this[i]).value;
		objName = document.getElementById(this[i]).name;
		if(objValue =='') {
			alert('Enter ' + this[i]); document.forms[0].elements[objName].focus();
		  flag = false;
			return;
		}
		if (chkNumeric(objValue,minval,maxval,comma) == false) {
		  flag = false;
			document.forms[0].elements[objName].select();
			document.forms[0].elements[objName].focus();
			return;
		}
	}
	flag = true;
	return;
}


function chkNumeric(objName,minval,maxval,comma)
{
// only allow 0-9 be entered, plus any values passed
// (can be in any order, and don't have to be comma, period, or hyphen)
// if all numbers allow commas, periods, hyphens or whatever,
// just hard code it here and take out the passed parameters
var checkOK = "0123456789" + comma;
var checkStr = objName;
var allValid = true;
var decPoints = 0;
var allNum = "";

//for (i = 0;  i < checkStr.value.length;  i++)
for (i = 0;  i < checkStr.length;  i++)
{
//ch = checkStr.value.charAt(i);
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{	
alertsay = "Please enter only these values \""
alertsay = alertsay + checkOK + "\" in the field."
alert(alertsay);
return (false);
}

// set the minimum and maximum
var chkVal = allNum;
var prsVal = parseInt(allNum);
if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
{
alertsay = "Please enter a value greater than or "
alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
alertsay = alertsay + "equal to \"" + maxval + "\" in the field."
alert(alertsay);
return (false);
}
}
