var browserType;
browserTypeArr = [ 'Firefox/1',  'Opera',     'MSIE 5.5',   'MSIE 5',  'MSIE 6',  'MSIE 4',
     'MSIE 3',     'MSIE',     'Konqueror', 'Netscape6',  'Netscape/7', 'Mozilla/4',
      'Mozilla/3', 'Mozilla/2', 'Mozilla' ];

function detectBrowser()
{
   for (i = 0; i < browserTypeArr.length; i++)
   {
        if (navigator.userAgent.indexOf(browserTypeArr[i]) >= 0)
        {
            browserType = browserTypeArr[i];
            return ;
        }
    }
}

function isValidRadio(sn)
{
	var MyArr = new Array();
	MyArr = sn;
	var value = "";
	var i=0;
	while(i<MyArr.length && !MyArr[i].checked)
	{
		i++;
	}
	if(i == MyArr.length)
	{
		i=0;
		while(i<MyArr.length)
		{
			MyArr[i].style.borderStyle = "solid";
			MyArr[i].style.borderWidth = "1";
			MyArr[i].style.borderColor = "ff0000";
			i++;
		}
  	return false;
	}
	else
	{
		i=0;
		while(i<MyArr.length)
		{
			MyArr[i].style.borderStyle = "";
			MyArr[i].style.borderWidth = "0";
			MyArr[i].style.borderColor = "";
			i++;
		}
		return true;
	}
}

function isValidName(sn)
{
	s = trim(sn.value)+'';
	if (s&&(/^[a-zA-Z\.\- ]+$/.test(s)))
  {
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function isValidPassword(sn)
{
    s = trim(sn.value)+'';
    if (browserType=='Opera')
    {
        if (s.indexOf(" ") > -1) {
	        HighlightErrorField(sn);
  	        return false;
        } else if(s.length < 5 )
        {
            HighlightErrorField(sn);
  	        return false;
        } else if(s.indexOf("'") > -1)
        {
            HighlightErrorField(sn);
  	        return false;
        } else
        {
          	RestoreFieldStyle(sn);
  	        return true;
        }
    }



	if (s&&(/([0-9a-zA-z\.\,\-\=\_\+]+){5}/.test(s)))
  {
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function isValid(sn)
{
  s = trim(sn.value)+'';
  if (s)
  {
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function isValidEmail(sn)
{
	s = trim(sn.value)+'';
	if(s && (/^\w+([\.\-]?\w+)*@\w+([\.\-]?\w+)*(\.\w{2,3})+$/.test(s)))

	{
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function isValidUserName(sn)
{
	s = trim(sn.value)+'';

	if(s && (/^[\w]{1,}[\w\d_]{1,}$/.test(s)))
	{
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function isValidUserGroup(sn)
{
	s = trim(sn.value)+'';
	if(s && (/^[\d]+$/.test(s)))

	{
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function isValidNumber(sn)
{
	s = trim(sn.value)+'';
	if(s && (/^[\d]+$/.test(s)))
	{
		RestoreFieldStyle(sn);
		return true;
	}
	else
	{
		HighlightErrorField(sn);
		return false;
	}

}

function isValidCNP(sn)
{
	s = trim(sn.value)+'';
	if(s && (/^[\d]+$/.test(s)) && 13==s.length)
	{
		RestoreFieldStyle(sn);
		return true;
	}
	else
	{
		HighlightErrorField(sn);
		return false;
	}
}

function isValidDate(sn)
{
	s = trim(sn.value)+'';
	if( s && (/^[\d]+$/.test(s)) && (Number(s) > 0) )
	{
		RestoreFieldStyle(sn);
		return true;
	}
	else
	{
		HighlightErrorField(sn);
		return false;
	}

}

function isValidYear(sn)
{
	s = trim(sn.value)+'';
	if( s && (/^[\d]+$/.test(s)) && (Number(s) >= 2000)  && (Number(s) <= 2010) )
	{
		RestoreFieldStyle(sn);
		return true;
	}
	else
	{
		HighlightErrorField(sn);
		return false;
	}

}

function isValidMYear(sn)
{
	s = trim(sn.value)+'';
	if( s && (/^[\d]+$/.test(s)) && (Number(s) >= 1800)  && (Number(s) <= 2010) )
	{
		RestoreFieldStyle(sn);
		return true;
	}
	else
	{
		HighlightErrorField(sn);
		return false;
	}

}

function isValidPhone(sn)
{

	s = trim(sn.value)+'';
	if(s && (/^[\d]{1,}([ ]{0,1}[\.,\(\)\-\/]{0,1}[ ]{0,1}[\d]{1,}[ ]{0,1}){0,}$/.test(s)))

	{
  	RestoreFieldStyle(sn);
  	return true;
  }
  else
  {
  	HighlightErrorField(sn);
  	return false;
  }
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
//   var inputString = inputField.value+'';
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function HighlightErrorField(field)
{
  field.style.borderColor="#ff0000";
  field.style.background="#ffffff";
}

function RestoreFieldStyle(field)
{
  field.style.borderColor="#21395a";
  field.style.background="#ffffff";

}


