var submitform=true;


var charexp = /./
var letterexp = /[a-z]/i
//var phonexp =  /^\d{10}$/
var memberexp = /^\d{3}$/
var zipexp = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/
var phoneexp = /^\d{10}$|^\d{3}[-]\d{3}[-]\d{4}$/
var emailexp = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]{2,3}$/i
var pledgexp = /^\d*$|^\d*\.\d{2}$/
var passexp=/[a-zA-Z{1,)]\d{1,}/

function match_email(str) {
	return emailexp.test(str)
}

function match_pass(str) {
	return (passexp.test(str) && (str.length > 4))
}

function match_phone(str) {
	return phoneexp.test(str)
}


function subSubmit(f,strProductInfo,msgid,ElementName)
{
	var arrEl=ElementName.split('|');
	
	for (var i=0;i<arrEl.length ;i++)
	{
		document.all(arrEl[i]).optional=false;
	}
	
	
	
	if (!verify(f,strProductInfo,msgid))
	{
		for (var i=0;i<arrEl.length ;i++)
		{
			document.all(arrEl[i]).optional=true;
		}
		submitform=false;
	}
	else
		submitform=true;
}		

function isSubmit()
{
	return (submitform);
}

function showMessage(strProductInfo,id)
{
	var arrMsg=new Array('','')
	arrMsg[0]='<TITLE>SampleRequested|You have requested a sample of this <B>'+strProductInfo+'</B>.'+
				'You will see the status of this sample on your Samles page after pressing OK.'+
				' You can jump directly to your Samples page by clicking on the Samples tab in the '+
				' upper right-hand corner of most pages on WorldWineTrade.com.'

	arrMsg[1]='<TITLE>SampleRe-requested|You have requested another sample of this <B>'+strProductInfo+'</B>.'+
				'You will see the status of this sample on your Samles page after pressing OK.'+
				' You can jump directly to your Samples page by clicking on the Samples tab in the '+
				' upper right-hand corner of most pages on WorldWineTrade.com.'				
	
	arrMsg[2]='<TITLE>SampleApproved|The seller has been notified of your sample approval.'				
	arrMsg[3]='<TITLE>SampleRejected|The seller has been notified of your sample rejection.'				
	arrMsg[4]='<TITLE>SampleFulFilled|Your sample fulfillment information has been sent to the buyer.'
	arrMsg[5]="<TITLE>SampleDenied|You have denied this buyer's request for a sample."
	showdialog(arrMsg[id]);
}

function isblank(s)
{
	
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function verify(f,strProductInfo,msgid)
{
  var msg="";
  var empty_fields = "";
  var errors = "";


	for (var i=0; i<f.length; i++) {
		var e = f.elements[i];
		
		if (((e.type == "text") || (e.type=="textarea") || (e.type=="password") || (e.type=="select-one")  ) && !eval(e.optional)) {
			if ((e.value == null) || (e.value == "") || (e.value=='NotSpecified') || isblank(e.value)) {
				empty_fields += "|"+e.errname;
				continue;
			}
			
			if (e.equal)
				if (e.value != eval(e.equal).value)
					errors += "" + e.errname + " must be the same as "+eval(e.equal).errname+"|";
			
			if (e.evalfunc)
				if (!eval(e.evalfunc))
					errors += "" + e.errname + " is not correct "+"|";
					
			if (e.numeric || (e.min != null) || (e.max != null)) {
				
				var v = parseFloat(e.value);
				
				if (isNaN(v) || 
				   ((e.min != null) && (v < e.min)) ||
				   ((e.max != null) && (v > e.max))) {
						
						errors += "" + e.errname + " must be a number";
						
						if (e.min != null)
						   errors += " that is greater than " + e.min;
						if (e.max != null && e.min != null)
						   errors += " and less than " + e.max;
						else if (e.max != null)
						   errors += " that is less than " + e.max;
						errors += "|";
						
				}
			}
			
			if (e.date)  //check for correct date
			{
				if (!isDate(e.value))
					errors += "" + e.errname + " must be a valid date";
			}
		}
	}	 
	
	if (!empty_fields && !errors)
	{
		//showMessage(strProductInfo,msgid);
		return true;
	}
	//msg = "Please correct these error(s) and re-submit.\n";
	//if (empty_fields)
	//{
	//	msg+="- The following required field(s) are empty:"
	//		  + empty_fields + "\n";
	//	if (errors) msg += "\n";
	//}
	//msg += errors;
	showerror(errors+empty_fields);
	return false;
}


function showerror(msg)
{
	var oTable=document.all.tblErrors;
	var arrError,oRow,oCell;
	document.all("divErrors").style.display="";
	arrError = msg.split("|");
	
	var i = 0;
	while (oTable.rows.length!=0)
		oTable.deleteRow(i);

	//for (var i=0; i < document.all.tblErrors.rows.length; i++) 
	//{
	//	document.all.tblErrors.deleteRow();
    //}

	//oTable.innerText='';	
	oRow = oTable.insertRow();
	oCell=oRow.insertCell();
	oCell.height=5;
	
	oRow = oTable.insertRow();
	oCell=oRow.insertCell();
	oCell.height=10;
	
	oRow = oTable.insertRow();
	oCell=oRow.insertCell();
	oCell.className="RequiredField";
	oCell.innerText='The information listed below is required to complete this submission.  Please enter the following information below to continue:'

	for (var i=0;i<arrError.length;i++)
	{
		oRow = oTable.insertRow();
		oCell=oRow.insertCell();	
		oCell.className="RequiredField";
		if (arrError[i] != '')
			oCell.innerHTML = '<li>&nbsp;&nbsp;&nbsp'+arrError[i]+'</li>';
	}

}


function fnFillShipTo(obj)
{
 if (obj.checked)
 {
	document.all("Company_ShipTo").value=document.all("Company_BillTo").value;
	document.all("ContactName_ShipTo").value=document.all("ContactName_BillTo").value;
	document.all("Address_ShipTo").value=document.all("Address_BillTo").value;
	document.all("Address1_ShipTo").value=document.all("Address1_BillTo").value;
	document.all("Phone_ShipTo").value=document.all("Phone_BillTo").value;
	document.all("Fax_ShipTo").value=document.all("Fax_BillTo").value;
 }
}