// JavaScript Document
function submit_validation(form)
{
	if (form.txt_username.value.length < 4) {
    alert( "Please enter your Name" );
    form.txt_username.focus();
    return false ;
  }
   else if (email_validate(form.txt_email.value)==false) {
    alert( "Please enter a valid email address" );
    form.txt_email.focus();
    return false ;
  }
   else if (check_bug_summary(form.txt_bug_summary.value)==false) {
    alert( "Please enter a valid bug summary" );
    form.txt_bug_summary.focus();
    return false ;
  }
  else if (check_bug_description(form.txt_description.value)==false) {
    alert( "Please enter a valid bug description" );
    form.txt_description.focus();
    return false ;
  }
    else if (isValidOS(form.txt_operating_system.value)==false) {
    alert( "Please enter a Operating System information" );
    form.txt_operating_system.focus();
    return false ;
  }
    else if (isValidProductVersion(form.txt_product_version.value) == false) {
    alert( "Please enter your Product Version." );
    form.txt_product_version.focus();
    return false ;
  }
  else if (form.str_captcha.value == "") {
    alert( "Please enter captcha and then continue." );
    form.str_captcha.focus();
    return false ;
  }
		else
		{
			return true ;
			}
		
	}
	
	

	function check_bug_summary(text_area_value){
		
		var textArea = text_area_value;
			var char = "";
			char = trim(textArea, " ");
			
			var textAreaLength = char.length;
					
			var i = 0;
			var spaces = 0;
			while( i < textAreaLength)	{
				var character = char.substring(i,i+1);
//				alert("ASCII code is " + char.charCodeAt(i));
				
				if(char.charCodeAt(i) == 32)	{
					spaces++;
				}									
				
				i++;

			}
	
			if(spaces < 2 || char.length < 10)	{
			//	alert("You entered an invalid text");
//				alert(char);
//				alert("Length is " + char.length);
//				alert("Space(s) " + spaces);
				return false;
			} else	{
					/*	alert("You entered an valid text");
				alert(char);
				alert("Length is " + char.length);
				alert("Space(s) " + spaces);*/
				return true;
			}			
	}
		
		
		function check_bug_description(text_area_value){
		
			var textArea = text_area_value;
			var char = "";
			char = trim(textArea, " ");
			
			var textAreaLength = char.length;
					
			var i = 0;
			var spaces = 0;
			while( i < textAreaLength)	{
				var character = char.substring(i,i+1);
//				alert("ASCII code is " + char.charCodeAt(i));
				
				if(char.charCodeAt(i) == 32)	{
					spaces++;
				}									
				
				i++;

			}
	
			if((spaces < 5) || (char.length < 20) )	{
					/*alert("You entered an invalid text");
				alert(char);
				alert("Length is " + char.length);
				alert("Space(s) " + spaces);*/
				return false;
			} else	{
					/*alert("You entered an valid text");
				alert(char);
				alert("Length is " + char.length);
				alert("Space(s) " + spaces);*/
				return true;
			}			
	}
		
function email_validate(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
	}


function trim(str, chars) {
				return ltrim(rtrim(str, chars), chars);
			}
			 
			function ltrim(str, chars) {
				chars = chars || "\\s";
				return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
			}
			 
			function rtrim(str, chars) {
				chars = chars || "\\s";
				return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
			}	
			
			
			// Product Version
			function isValidProductVersion(txt_product_version) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(txt_product_version)) {
      var parts = txt_product_version.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++) {
		  //alert(parseInt(parseFloat(parts[i])));
         if (parseInt(parseFloat(parts[i])) > 255)
		 { 
		// return false;
		 }
      }
      return true;
   } else {
      return false;
   }
}
// Operating system validation
function isValidOS(inpString) {
  return /^[a-zA-Z]+\s?([0-9])|(VISTA)|(vista)|(Vista)|(XP)|(xp)$/.test(inpString);
}