// JavaScript Document
// Registration validation : for registation.html i.e validates user inputs  //
// Developed by :: Kishore Kumar.T, Project Leader, Option Matrix Pvt. Ltd.  //
// -------------------------  10-07-2006  ---------------------------------- //


//----------- function to trim inputs --------------------//
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
//----------- function to trim inputs --------------------//

//----------- function to check validity of email --------------------//
function echeck(str) 
{
		var at		= "@"
		var dot		= "."
		var lat		= str.indexOf(at)
		var lstr	= str.length
		var ldot	=str.indexOf(dot)
		
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   alert("Please enter valid email id")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    alert("Please enter valid email id")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    alert("Please enter valid email id")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    alert("Please enter valid email id")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    alert("Please enter valid email id")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    alert("Please enter valid email id")
		    return false
		 }

 		 return true					
	}

//----------- function to check validity of email --------------------//

function checkRegistration()
{
		var doc = document.frmRegistration;
		
		var strUserName	= doc.txtUserName.value;
		strUserName		= trim(strUserName)
		if(strUserName == "") 
		{
				alert("Please enter user name");
				doc.txtUserName.focus();
				return false;
		}
		if(strUserName != "") 
		{
				var strUserNameLength = strUserName.length
				if(strUserNameLength < 6)
				{
					alert("Please enter atleast 6 characters for user name");
					doc.txtUserName.focus();
					return false;
				}
		}
		
		var strPassword	= doc.txtPassword.value;
		strPassword		= trim(strPassword)
		if(strPassword == "") 
		{
				alert("Please enter Password");
				doc.txtPassword.focus();
				return false;
		}
		if(strPassword != "") 
		{
				var strPasswordLength = strPassword.length
				if(strPasswordLength < 6)
				{
					alert("Please enter atleast 6 characters for password");
					doc.txtPassword.focus();
					return false;
				}
		}
		
		var strCofirmPwd	= doc.txtCofirmPwd.value;
		strCofirmPwd		= trim(strCofirmPwd)
		if(strCofirmPwd == "") 
		{
				alert("Please confirm your password");
				doc.txtCofirmPwd.focus();
				return false;
		}
		if(strCofirmPwd != "") 
		{
				if(strPassword != strCofirmPwd)
				{
					alert("Passwords doesnt match. Please enter again.");
					doc.txtCofirmPwd.focus();
					return false;
				}
		}

		var strEmail = doc.txtEmail.value
		if (echeck(strEmail)==false)
		{
			doc.txtEmail.focus()
			return false
		}
		
		var strFirstName = doc.txtFirstName.value;
		strFirstName = trim(strFirstName);
		if(strFirstName == "") 
		{
				alert("Please enter first name");
				doc.txtFirstName.focus();
				return false;
		}

		var strLastName = doc.txtLastName.value;
		strLastName = trim(strLastName);
		if(strLastName == "") 
		{
				alert("Please enter last name");
				doc.txtLastName.focus();
				return false;
		}

		
		var strOrganization	= doc.txtOrganization.value;
		strOrganization		= trim(strOrganization)
		if(strOrganization == "") 
		{
				alert("Please enter your organization's name");
				doc.txtOrganization.focus();
				return false;
		}
		
		
		var strAddress1	= doc.txtAddress1.value;
		strAddress1		= trim(strAddress1)
		if(strAddress1 == "") 
		{
				alert("Please enter address of the organization");
				doc.txtAddress1.focus();
				return false;
		}
		if(strAddress1 != "") 
		{
				var strAddress1Length = strAddress1.length
				if(strAddress1Length < 10)
				{
					alert("Please enter atleast 10 characters for address");
					doc.txtAddress1.focus();
					return false;
				}
		}
		
		var strCity	= doc.txtCity.value;
		strCity		= trim(strCity)
		if(strCity == "") 
		{
				alert("Please enter city");
				doc.txtCity.focus();
				return false;
		}

		var strState	= doc.txtState.value;
		strState		= trim(strState)
		if(strState == "") 
		{
				alert("Please enter state");
				doc.txtState.focus();
				return false;
		}

		var strCountry	= doc.txtCountry.options[doc.txtCountry.selectedIndex].value;
		if(strCountry == "0") 
		{
				alert("Please select your country");
				doc.txtCountry.focus();
				return false;
		}

		var strPostalCode	= doc.txtPostalCode.value;
		strPostalCode		= trim(strPostalCode)
		strPostalCodeLength	= strPostalCode.length
		if(strPostalCode == "") 
		{
				alert("Please enter postal code");
				doc.txtPostalCode.focus();
				return false;
		}
		if(isNaN(strPostalCode) || (strPostalCodeLength <=3))
		{
				alert("Please enter valid postal code");
				doc.txtPostalCode.focus();
				return false;
		}
		
		var strOrgTelephone	= doc.txtOrgTelephone.value;
		strOrgTelephone		= trim(strOrgTelephone)
		strOrgTelephoneLength	= strOrgTelephone.length
		if(strOrgTelephone == "") 
		{
				alert("Please enter telephone number of your organisation.");
				doc.txtOrgTelephone.focus();
				return false;
		}
		if(isNaN(strOrgTelephone) || (strOrgTelephoneLength <=4))
		{
				alert("Please enter valid telephone number.");
				doc.txtOrgTelephone.focus();
				return false;
		}
		
		var strCompanyEmail = doc.txtCompanyEmail.value
		if (echeck(strCompanyEmail)==false)
		{
			doc.txtCompanyEmail.focus()
			return false
		}
		
		
		var strIndustry	= doc.txtCountry.options[doc.txtIndustry.selectedIndex].value;
		if(strIndustry == "0") 
		{
				alert("Please select your industry");
				doc.txtIndustry.focus();
				return false;
		}
		
		var strDescription	= doc.txtDescription.value;
		strDescription		= trim(strDescription)
		if(strDescription == "") 
		{
				alert("Please enter description on your organization");
				doc.txtDescription.focus();
				return false;
		}
		if(strDescription != "") 
		{
				var strDescriptionLength = strDescription.length
				if(strDescriptionLength < 50)
				{
					alert("Please enter atleast 50 characters for description");
					doc.txtDescription.focus();
					return false;
				}
		}
}
