
function openPage(url)
{
	window.open(url,"", "left=0,top=0,height=490,width=790,status=no,titlebar=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

function hideByTagByRange(idFrom, idTo)
{
	for(var i=0; i<document.all.length;i++)
	{
		var id = document.all[i].id;
		if(id>=idFrom && id<=idTo)
		{
			document.all[i].style.visibility = 'hidden';
		}
	}
}

function hideByTag(attachId)
{
	attachIdLength = attachId.length;
	//before print, set only selected field visible
	for(var i=0; i<document.all.length;i++)
	{
		var id = document.all[i].id;
		if(id<-999 && document.all[i].style.visibility!='hidden')
		{
			//alert(document.all[i].id);
			document.all[i].id = attachId + id;
			document.all[i].style.visibility = 'hidden';
			//alert(document.all[i].id);
		}
	}
}

function showByTag(attachId)
{
	//after print, reset visibility
	for (var i=0;i<document.all.length;i++)
	{
		var id = document.all[i].id;
		if(id.tagName!="INPUT")
		{
			if(id.indexOf(attachId)>-1)
			{
				//alert(document.all[i].id);
				document.all[i].style.visibility='';
				document.all[i].id = id.substring(attachIdLength, id.length);
				//alert(document.all[i].id);
			}
		}
	}
}

function doPrint()
{
	var attachId = '__print__';
	hideByTag(attachId);
	window.print();
	showByTag(attachId);
}

function doHelp()
{
	location="help.do";
}

function addItem()
{
	var frm = document.itemForm
	var selectAny = false;
	var ids = "-1";
	for (var i=0;i<frm.all.length;i++)
	{
		var select = frm.all[i]
		if(select.name=="partSelected")
		{
			if(select.checked)
			{
				selectAny = true;
				ids += "," + select.value 
			}
		}
	}

	if(selectAny)
	{
		var frm2 = document.componentSearchForm;
            document.itemForm.ids.value = ids;
		frm2.ids.value = ids;
		frm2.action = "itemUpdate.do?type=confirm"
		frm2.submit();
	}
	else
		alert("No item selected! Please selected a part or multiple parts to add.")
}

function removeItem()
{
	var frm = document.itemForm
	var selectAny = false;
	var ids = "-1";
	for (var i=0;i<frm.all.length;i++)
	{
		var select = frm.all[i]
		if(select.name=="partSelected")
		{
			if(select.checked)
			{
				selectAny = true;
				ids += "," + select.value 
			}
		}
	}

	if(selectAny)
	{
		var frm2 = document.componentSearchForm
		frm2.ids.value = ids;
		frm2.action = "itemUpdate.do?type=remove"
		frm2.submit();
	}
	else
		alert("No item selected! Please selected a part or multiple parts to remove.")
}


function doSearch()
{
	return getSearchDate();
}

function doPage(i)
{
	var searchForm = document.itemForm;
	var pageNum = searchForm.currentPage.value;
	if(!isNaN(pageNum) && !isNaN(i))
		searchForm.page.value = parseInt(pageNum) + parseInt(i);
	else
		searchForm.page.value = 1;
	getSearchDate();
	searchForm.submit();
}

function doClearSearch()
{
	var frm = document.componentSearchForm
	frm.number1.value="";
	frm.number2.value="";
	frm.number3.value="";
	frm.number4.value="";
	frm.number5.value="";
	frm.number6.value="";
	frm.number7.value="";
	frm.number8.value="";
	frm.ac.value="";
	frm.ata.value="";

}

function doNewSearch()
{
	var frm = document.componentSearchForm
	frm.page.value="0";
	frm.ids.value="";
	frm.submit();
}

function doClearSearchRFQ()
{
	var frm = document.RFQSearchForm
	frm.number.value="";
	frm.ac.value="";
	frm.ata.value="";
}

function doSearchByPage(page)
{
	var frm = document.componentSearchForm
	frm.page.value=page;
	frm.submit();
}
function doSearchByPageAndAdd( page )
{   
   var frm1 = document.itemForm;
   var selectAny = false;
   var ids = "-1";
	
   for (var i=0;i<frm1.all.length;i++)
   {
   		var select = frm1.all[i];
		if(select.name=="partSelected")
		{
			if(select.checked)
			{
				selectAny = true;
				ids += "," + select.value 
			}
		}
	}    
	if(selectAny)
	{	    
		var frm2 = document.componentSearchForm
		frm2.ids.value = ids;
		frm2.action = "itemUpdate.do?type=add"		
	}
	
   
   var frm = document.componentSearchForm;
   frm.page.value=page;	
   frm.submit();	   
   
}

function doSearchRFQByPage(page)
{
	var frm = document.RFQSearchForm
	frm.page.value=page;
	getSearchDate();
	frm.submit();
}

/********************************************************************************
 Auto_Tab functions for date field
 ********************************************************************************/
var filter = [0,8,9,16,17,18,37,38,39,40,46];
function checkNumber(e) {
	var key = e.keyCode;
	if ((key>47&&key<58)||(key>95&&key<106)||containsElement(filter,key))
		return true;
	else
		return false;
}

function autoTab(input,len, e) {
	var keyCode = e.keyCode; 
	if(keyCode==13)
	{
		if(doSearch()!=false)
		{
			input.form.submit();
			return true;
		}
	}
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		var index = getIndex(input)+1;
		while(input.form[index].type=="hidden")
			index++;
		input.form[index % input.form.length].focus();
	}
	return true;
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
	return index;
}


/********************************************************************************
 Date format functions
 ********************************************************************************/
function getDate() {
	var date = new Date();
	var day = date.getDate();
	if(day<10)
		day = "0" + day;
	var month = date.getMonth();
	month++;
	if(month<10)
		month = "0" + month;
	var year = date.getYear();

	return year + "-" + month + "-" + day;
}

function getSearchDate()
{
	var searchForm = document.RFQSearchForm;
	var date = "";
	var tmp = searchForm.submit_from_year.value;

	if(tmp.length != 0)
	{
		if(tmp.length != 2 && tmp.length !=4)
		{
			alert("Invalid input date format in the field of 'Date - From'");
			return false;
		}
		if(tmp.length == 2)
			date = "20" + tmp;
		else
			date = tmp;

		tmp = searchForm.submit_from_month.value;
		if(tmp.length != 0)
		{
			if(tmp.length != 2)
			{
				alert("Invalid input date format in the field of 'Date - From'");
				return false;
			}
			date += "-" + tmp;

			tmp = searchForm.submit_from_day.value;
			if(tmp.length != 0)
			{
				if(tmp.length != 2)
				{
					alert("Invalid input date format in the field of 'Date - From'");
					return false;
				}
				date += "-" + tmp;
			}
			else
				date = "";
		}
		else
			date = "";
	}
	searchForm.submit_dateFrom.value = date;
	
	date = "";
	tmp = searchForm.submit_to_year.value;
	if(tmp.length != 0)
	{
		if(tmp.length != 2 && tmp.length !=4)
		{
			alert("Invalid input date format in the field of 'Date - To'");
			return false;
		}
		if(tmp.length == 2)
			date = "20" + tmp;
		else
			date = tmp;

		tmp = searchForm.submit_to_month.value;
		if(tmp.length != 0)
		{
			if(tmp.length != 2)
			{
				alert("Invalid input date format in the field of 'Date - To'");
				return false;
			}
			date += "-" + tmp;

			tmp = searchForm.submit_to_day.value;
			if(tmp.length != 0)
			{
				if(tmp.length != 2)
				{
					alert("Invalid input date format in the field of 'Date - To'");
					return false;
				}
				date += "-" + tmp;
			}
			else
				date = "";
		}
		else
			date = "";
	}
	searchForm.submit_dateTo.value = date;

	date = "";
	tmp = searchForm.complete_from_year.value;
	if(tmp.length != 0)
	{
		if(tmp.length != 2 && tmp.length !=4)
		{
			alert("Invalid input date format in the field of 'Date - From'");
			return false;
		}
		if(tmp.length == 2)
			date = "20" + tmp;
		else
			date = tmp;

		tmp = searchForm.complete_from_month.value;
		if(tmp.length != 0)
		{
			if(tmp.length != 2)
			{
				alert("Invalid input date format in the field of 'Date - From'");
				return false;
			}
			date += "-" + tmp;

			tmp = searchForm.complete_from_day.value;
			if(tmp.length != 0)
			{
				if(tmp.length != 2)
				{
					alert("Invalid input date format in the field of 'Date - From'");
					return false;
				}
				date += "-" + tmp;
			}
			else
				date = "";
		}
		else
			date = "";
	}
	searchForm.complete_dateFrom.value = date;

	date = "";
	tmp = searchForm.complete_to_year.value;
	if(tmp.length != 0)
	{
		if(tmp.length != 2 && tmp.length !=4)
		{
			alert("Invalid input date format in the field of 'Date - To'");
			return false;
		}
		if(tmp.length == 2)
			date = "20" + tmp;
		else
			date = tmp;

		tmp = searchForm.complete_to_month.value;
		if(tmp.length != 0)
		{
			if(tmp.length != 2)
			{
				alert("Invalid input date format in the field of 'Date - To'");
				return false;
			}
			date += "-" + tmp;

			tmp = searchForm.complete_to_day.value;
			if(tmp.length != 0)
			{
				if(tmp.length != 2)
				{
					alert("Invalid input date format in the field of 'Date - To'");
					return false;
				}
				date += "-" + tmp;
			}
			else
				date = "";
		}
		else
			date = "";
	}
	searchForm.complete_dateTo.value = date;

}


function doClean()
{
	var frm = document.RFQSearchForm;
	frm.submit_from_month.value = "";
	frm.submit_from_day.value = "";
	frm.submit_from_year.value = "";
	frm.submit_to_month.value = "";
	frm.submit_to_day.value = "";
	frm.submit_to_year.value = "";
	frm.complete_from_month.value = "";
	frm.complete_from_day.value = "";
	frm.complete_from_year.value = "";
	frm.complete_to_month.value = "";
	frm.complete_to_day.value = "";
	frm.complete_to_year.value = "";
	frm.rfq_num.value = "";
	frm.rfq_company.value = "";
	frm.searchType.value = "";
}


function checkFields()
{
	var frm = document.creditForm;

	var stmp = frm.customer_name.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Customer Name");
		return false;
	}

	stmp = frm.customer_address1.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Customer Address 1");
		return false;
	}

	stmp = frm.customer_city.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Customer City/State/Zip");
		return false;
	}

	stmp = frm.customer_phone.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Customer Phone #");
		return false;
	}

	stmp = frm.customer_fax.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Customer fax #");
		return false;
	}

	stmp = frm.customer_fedid.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Customer Fed Id #");
		return false;
	}

	stmp = frm.bank_name.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Bank Name");
		return false;
	}

	stmp = frm.bank_address.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Bank Address");
		return false;
	}

	stmp = frm.bank_contact.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Bank Contact");
		return false;
	}

	stmp = frm.bank_phone.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Bank Phone #");
		return false;
	}

	stmp = frm.supplier1_name.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 1 Name");
		return false;
	}

	stmp = frm.supplier1_address.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 1 address");
		return false;
	}

	stmp = frm.supplier1_phone.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 1 Phone #");
		return false;
	}

	stmp = frm.supplier1_contact.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 1 Contact");
		return false;
	}

	stmp = frm.supplier2_name.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 2 Name");
		return false;
	}

	stmp = frm.supplier2_address.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 2 address");
		return false;
	}

	stmp = frm.supplier2_phone.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 2 Phone #");
		return false;
	}

	stmp = frm.supplier2_contact.value;
	if(stmp.length==0)
	{
		fieldRequiredMsg("Supplier 2 Contact");
		return false;
	}

	return true;

}

function fieldRequiredMsg(fieldName)
{
	var msg = "Please provide the information for all required fields. \r\nThe input field of '" + fieldName + "' is empty.";
	alert(msg);
}

var filter = [0,8,9,16,17,18,37,38,39,40,46];
function checkNumber(e) {
	var key = e.keyCode;
	if ((key>47&&key<58)||(key>95&&key<106)||containsElement(filter,key))
		return true;
	else
		return false;
}

function autoTab(input,len, e) {
	var keyCode = e.keyCode; 
	if(keyCode==13)
	{
		if(doSearch()!=false)
		{
			input.form.submit();
			return true;
		}
	}
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		var index = getIndex(input)+1;
		while(input.form[index].type=="hidden")
			index++;
		input.form[index % input.form.length].focus();
	}
	return true;
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
	return index;
}

