// JavaScript Document

// Registration page validation
function validregister(frm)
{ 
with(frm)
	{
	
		
	if (validate_required(fname,"First Name must be filled out!")==false)
		{fname.focus();return false;}	
		
		if (validate_required(lname,"Last Name must be filled out!")==false)
		{lname.focus();return false;}	
		
		if (validate_required(address1,"Address must be filled out!")==false)
		{address1.focus();return false;}	
		
		if (validate_required(city,"City must be filled out!")==false)
		{city.focus();return false;}	
		if (validate_required(state,"State must be filled out!")==false)
		{state.focus();return false;}	
		if (validate_required(zip,"Zip Code must be filled out!")==false)
		{zip.focus();return false;}	
		
		if(IsNumeric(zip.value)==false)
		{
			
			alert("Only Numbers are allowed");	
			zip.value="";
			zip.focus();return false;
		}
		if(zip.value.length>5)
		{
			
		alert("Zip only have 5 digits");
		zip.value="";
		zip.focus();return false;
		}
		
		if (validate_required(country,"country must be filled out!")==false)
		{country.focus();return false;}	
		
		
		if(validate_email(email)==false)
		{
			email.value="";
			email.focus();return false;
		}
		
		if(phone.value!="")
		{
			if(IsNumeric(phone.value)==false)
			{
				alert("Phone No should be in numbers only");
				phone.value="";
				phone.focus();return false;
			}
			
			if(phone.value.length>12)
			{
			alert("Phone No only have 12 digits");
			phone.value="";
			phone.focus();return false;
			}
		}
		
		if (valButton(gender,2)==false)
		{
			alert("Gender must be filled out");
			return false;
			
		}	
		
		if (validate_required(age,"Age must be filled out!")==false)		
		{age.focus();return false;}	
		
		if(IsNumeric(age.value)==false)
		{
			alert("Age should be in  numbers only");
			age.value="";
			age.focus();return false;
		}
			
		if (valButton(wherebuy,3)==false)
		{
			alert("Where By must be filled out");
			return false;
			
		}	
		
		if (validate_required(wherehear,"Where Here must be filled out!")==false)		
		{wherehear.focus();return false;}	
		
		if (valButton(alreadyhave,2)==false)
		{
			alert("Already Have must be filled out");
			return false;
			
		}	
		
		/*if (valButton(modelno,2)==false)
		{
			alert("Choose Model First");
			return false;
			
		}	*/
		if (validate_required(serialno,"Serial no. must be filled out!")==false)	
		{

			serialno.focus();return false;
			
			}	
		
		if(serialno.value=="S/N:")
		{
			alert("Please enter Serial no");
			
			serialno.focus();return false;
		}
		
		if (valButton(likecontact,2)==false)
		{
			alert("Choose Contact by Email");
			return false;
			
		}	
		
		
		
  }
	return true;
}


function IsNumeric(strString)
   //  check for valid numeric strings	
   {

   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
//Email validation

function validate_email(field)
{

	if (field.value==null||field.value=="")
	{
		alert("Email must be filled out");
		return false;
	}
	else
	{
			apos=field.value.indexOf("@");
			dotpos=field.value.lastIndexOf(".");
			if (apos<1||dotpos-apos<2) 
			 {
			  alert("Invalid Email");
			  return false;
			  }
			else 
			{
				return true;
			}
	}
	return true;
}

function valButton(btn,n)
{
	
    var cnt = 0;
	var n=n;
    
    for (var i=0; i<n; i++)
	{
        if (btn[i].checked) 
		{
			
			cnt = 1;
		}
    }
	
    if (cnt > 0) 
	{
	return true;
	}
    else 
	{	
		return false;
	}
	
}
function validate_required(field,alerttxt)
{
	if(field.value==null || field.value=="")
	  {
			alert(alerttxt);
			return false;
	  }
	else
	 {
		return true;
	 }
}

function chkimage(songimage)
 { 
 var img;
 img=songimage.value;
var filelength = parseInt(img.length) - 4;
var fileext = img.substring(filelength,filelength + 4);

// Check file extenstion
if (fileext.toLowerCase() != ".jpeg" && fileext.toLowerCase() != ".jpg" && fileext.toLowerCase() != ".gif" && fileext.toLowerCase() != ".bmp")
{   alert("Please Upload the valid extension of image");    
	return false;

}
else
  {
   return true;
  }
}

