function strTrim(tmpStr)
{
	var i;
	for (i=0; i < tmpStr.length; ++i)
		if (tmpStr.charAt(i) != " ") break;
	tmpStr = tmpStr.substring(i);
	for (i=tmpStr.length - 1; i >= 0; --i)
		if (tmpStr.charAt(i) != " ") break;
	tmpStr = tmpStr.substring(0, i + 1);
	return tmpStr;
}
//-----------------------------------------------------------------------------
function chkPhone(tmpStr)
{
    var i = 0, tmpChar = "";
    for (i = 0; i < tmpStr.length; ++i)
    {
    	tmpChar = tmpStr.charAt(i);
	if (!((tmpChar >= "0" && tmpChar <= "9")
		|| (tmpChar == "-")
		|| (tmpChar == " ")
		|| (tmpChar == "(")
		|| (tmpChar == ")")
		)) return true;
	}
    return false;
}
//-----------------------------------------------------------------------------
function chkExtension(tmpStr)
{
    var i = 0, tmpChar = "";
    for (i = 0; i < tmpStr.length; ++i)
    {
    	tmpChar = tmpStr.charAt(i);
		if (!(tmpChar >= "0" && tmpChar <= "9")) return true;
	}
    return false;
}
//------------------------------------------------------------------------------
function chkZIP(tmpStr)
{
    var i = 0,tmpChar = "";
    for (i = 0; i < tmpStr.length; ++i)
    {
    	tmpChar = tmpStr.charAt(i);
		if (!((tmpChar >= "0" && tmpChar <= "9")
			||(tmpChar >= "a" && tmpChar <= "z")
			|| (tmpChar>= "A" && tmpChar <= "Z")
			|| (tmpChar == "-")
			|| (tmpChar == "(")
			|| (tmpChar == ")")
			|| (tmpChar == " ")
			)) return true;
	}
    return false;
}
//--------------------------------------------------------------------------------
function chkWebSite(tmpStr)
{
    var i = 0,tmpChar = "";
    for (i = 0; i < tmpStr.length; ++i)
    	{
    	 tmpChar = tmpStr.charAt(i);
		if (!((tmpChar >= "0" && tmpChar <= "9")
			||(tmpChar >= "a" && tmpChar <= "z")
			|| (tmpChar>= "A" && tmpChar <= "Z")
			|| (tmpChar == "-")
			|| (tmpChar == "_")
			|| (tmpChar == "/")
			|| (tmpChar == ":")
			|| (tmpChar == ".")
			|| (tmpChar == "~")
			|| (tmpChar == "?")
			|| (tmpChar == "#")
			|| (tmpChar == "=")
			|| (tmpChar == "&")
			)) return true;
	}
    return false;
}
//--------------------------------------------------------------------------------
function chkOrgName(tmpStr)
{
    var i = 0,tmpChar = "";
    for (i = 0; i < tmpStr.length; ++i)
    	{
    	 tmpChar = tmpStr.charAt(i);
		if (!((tmpChar >= "0" && tmpChar <= "9")
			||(tmpChar >= "a" && tmpChar <= "z")
			|| (tmpChar>= "A" && tmpChar <= "Z")
			|| (tmpChar == "-")
			|| (tmpChar == "(")
			|| (tmpChar == ")")
			|| (tmpChar == "'")
			|| (tmpChar == " ")
			|| (tmpChar == ".")
			|| (tmpChar == ",")
			)) return true;
	}
    return false;
}
//--------------------------------------------------------------------------------
function chkEmail(tmpStr)
{
	var i;
	var posAt = 0;
	var posDot = 0
	var count = 0;
	for(i=0;i<tmpStr.length;++i)
	{
		if(tmpStr.charAt(i) == "@")
		{
			posAt = i;
			count++;
		}
		if(tmpStr.charAt(i) == ".")
		{
			posDot = i;
		}
		if (!((tmpStr.charAt(i) >= "0" && tmpStr.charAt(i) <= "9")
				||(tmpStr.charAt(i) >= "a" && tmpStr.charAt(i) <= "z")
				|| (tmpStr.charAt(i)>= "A" && tmpStr.charAt(i) <= "Z")
				|| (tmpStr.charAt(i) == "-")
				|| (tmpStr.charAt(i) == "_")
				|| (tmpStr.charAt(i) == "@")
				|| (tmpStr.charAt(i) == ".")
			)) return false;
	}
	if(count>1) return false;
	if(eval(posAt) > 1 && posAt != tmpStr.length-1 && posDot > posAt && posDot != tmpStr.length-1) return true;
	return false;
}
//--------------------------------------------------------------------------------
function addCategory()
{
	var opt;
	for(i=obj['OrgCategory[]'].options.length-1;i>0;i--)
	{
		obj['OrgCategory[]'].options[i] = null;
	}
	if(obj.OrgType.selectedIndex != 0 )
	{
		for(i=0;i<arrStrData.length;i++)
		{

			if(obj.OrgType[obj.OrgType.selectedIndex].value == arrStrData[i][0])
			{
				opt = new Option();
				opt.value = arrStrData[i][1] ;
				opt.text = arrStrData[i][2] ;
				obj['OrgCategory[]'].options[obj['OrgCategory[]'].length] = opt;
			}
		}
	}
}
//--------------------------------------------------------------------------------
function Add()
{
	if(obj.OrgType.selectedIndex == 0)
	{
		alert("Please select a Type.");
		obj.OrgType.focus();
		return;
	}
	if(obj.elements['OrgCategory[]'].selectedIndex == -1 || obj.elements['OrgCategory[]'].selectedIndex == 0)
	{
		alert("Please select one or more Category");
		obj['OrgCategory[]'].focus();
		return;
	}
	if(strTrim(obj.OrgName.value) == "")
	{
		alert("Please enter a Name.");
		obj.OrgName.focus();
		return;
	}
/*
	if(chkOrgName(obj.OrgName.value))
	{
		alert("Only alphabets, numbers, blank space, hyphen and \nparentheses are allowed in name.");
		obj.OrgName.focus();
		obj.OrgName.select();
		return;
	}
	tmp1stChar = obj.OrgName.value.charAt(0);
	if(tmp1stChar == " " || tmp1stChar == "-" || tmp1stChar == "(" || tmp1stChar == ")")
	{
		alert("First character of name must be an alphabet or a number.");
		obj.OrgName.focus();
		obj.OrgName.select();
		return;
	}
	*/
	if(strTrim(obj.OrgDescription.value) == "")
	{
		alert("Please enter the Description.");
		obj.OrgDescription.focus();
		return;
	}
	if(strTrim(obj.OrgDescription.value).length > 1000)
	{
		alert("Maximum length of Description is limited to 100 words.\n(1000 characters)" );
		obj.OrgDescription.focus();
		obj.OrgDescription.select();
		return;
	}
	if(strTrim(obj.OrgAddress1.value) == "")
	{
		alert("Please enter the Address.");
		obj.OrgAddress1.focus();
		return;
	}
	if(strTrim(obj.OrgCity.value) == "")
	{
		alert("Please enter the City.");
		obj.OrgCity.focus();
		return;
	}
	if(obj.OrgState.selectedIndex == 0)
	{
		alert("Please select the State.");
		obj.OrgState.focus();
		return;
	}
	if(obj.OrgZip.value != "")
	{
		if(chkZIP(obj.OrgZip.value) == true)
		{
			alert("Only alphabets, numbers, blank space, hyphen and \nparentheses are allowed in ZIP.");
			obj.OrgZip.focus();
			obj.OrgZip.select();
			return;
		}
		tmp1stChar = obj.OrgZip.value.charAt(0);
		if(tmp1stChar == "-" || tmp1stChar == "(" || tmp1stChar == ")")
		{
			alert("First character of Zip cannot be a hyphen or parenthesis.");
			obj.OrgZip.focus();
			obj.OrgZip.select();
			return;
		}
	}
	if(strTrim(obj.OrgPhone.value) == "" && strTrim(obj.OrgExtension.value) != "")
	{
		alert("Please enter Telephone before entering Extension.");
		obj.OrgPhone.focus();
		return;
	}
	if(obj.OrgPhone.value != "")
	{
		if(chkPhone(obj.OrgPhone.value))
		{
			alert("Only numbers, hyphens and parentheses are allowed in Telephone number.");
			obj.OrgPhone.focus();
			obj.OrgPhone.select();
			return;
		}
		tmp1stChar = obj.OrgPhone.value.charAt(0);
		if(tmp1stChar == "-")
		{
			alert("First character of Telephone number cannot be a hyphen.");
			obj.OrgPhone.focus();
			obj.OrgPhone.select();
			return;
		}
	}
	if(obj.OrgExtension.value != "")
	{
		if(chkExtension(strTrim(obj.OrgExtension.value)))
		{
			alert("Only numbers are allowed for Extension number.")
			obj.OrgExtension.focus();
			obj.OrgExtension.select();
			return;
		}
	}
	if(obj.OrgFax.value != "")
	{
		if(chkPhone(strTrim(obj.OrgFax.value)))
		{
			alert("Only numbers, hyphens and parentheses are allowed in Fax number.")
			obj.OrgFax.focus();
			obj.OrgFax.select();
			return;
		}
		tmp1stChar = obj.OrgFax.value.charAt(0);
		if(tmp1stChar == "-")
		{
			alert("First character of Fax number cannot be a hyphen.");
			obj.OrgFax.focus();
			obj.OrgFax.select();
			return;
		}
	}
	if(obj.OrgWebSite.value != "")
	{
		if(chkWebSite(obj.OrgWebSite.value))
		{
			alert("Please enter a Valid URL for the Web Site.");
			obj.OrgWebSite.focus();
			obj.OrgWebSite.select();
			return;
		}
		if(obj.OrgWebSite.value.substring(0,7) != "http://")
		{
			alert("Please include the 'http://' portion in the URL of the Website.");
			obj.OrgWebSite.focus();
			obj.OrgWebSite.select();
			return;
		}
	}
	if(strTrim(obj.ContactName.value) == "")
	{
		alert("Please enter the Contact Person's Name.");
		obj.ContactName.focus();
		return;
	}
	if(strTrim(obj.ContactPhone.value) == "" && strTrim(obj.ContactExtension.value) != "")
	{
		alert("Please enter the Contact Phone number before entering Extension.");
		obj.ContactPhone.focus();
		return;
	}
	if(obj.ContactPhone.value != "")
	{
		if(chkPhone(obj.ContactPhone.value))
		{
			alert("Only numbers, hyphens and parentheses are allowed in Telephone number.");
			obj.ContactPhone.focus();
			obj.ContactPhone.select();
			return;
		}
		tmp1stChar = obj.ContactPhone.value.charAt(0);
		if(tmp1stChar== "-")
		{
			alert("First character of Telephone number cannot be a hyphen.");
			obj.ContactPhone.focus();
			obj.ContactPhone.select();
			return;
		}
	}
	if(obj.ContactExtension.value != "")
	{
		if(chkExtension(strTrim(obj.ContactExtension.value)))
		{
			alert("Only numbers are allowed for Extension number.")
			obj.ContactExtension.focus();
			obj.ContactExtension.select();
			return;
		}
	}
	if(strTrim(obj.ContactEmail.value) == "")
	{
		alert("Please enter Contact Person's Email Address.");
		obj.ContactEmail.focus();
		return;
	}
	if(chkEmail(obj.ContactEmail.value)==false)
	{
		alert("Please enter a valid Email Address.");
		obj.ContactEmail.focus();
		obj.ContactEmail.select();
		return;
	}
	obj.action = "add_org_response.php?id="+id;
	obj.submit();
}
//-----------------------------------------------------------------------------
function ResetForm()
{
	obj.reset();
	for(i=obj['OrgCategory[]'].options.length-1;i>0;i--)
	{
		obj['OrgCategory[]'].options[i] = null;
	}
}
//-----------------------------------------------------------------------------
function Search()
{
	if(strTrim(obj.Keywords.value) == "")
	{
		alert("Please enter Keyword(s) to search for.");
		obj.Keywords.focus();
		return;
	}
	if(obj.OrgCategory.options[obj.OrgCategory.selectedIndex].value == "OrgType")
	{
		typeName = obj.OrgCategory.options[obj.OrgCategory.selectedIndex].text
		typeName = typeName.substring(3, typeName.length-3);
		alert("Please select a Category under "+typeName+".");
		obj.OrgCategory.focus();
		return;
	}
	obj.action = 'search_org.php';
	obj.submit();
}

