var MaxInt= 13
var MaxString = 30;
var MaxAdress = 200;
var ContLength = 5 
var PassLength = 5
var MaxCP = 15
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";                     
var decimalPointDelimiter = "."                  

var mPrefix = "Nu ati introdus nimic in campul "
var mSuffix = ". Este un camp obligatoriu."

var sSubct = "Subcategorie"
var sPozp = "Pozitie produs"
var sExpl = "Eplicatie"
var sUmp = "Unitate de masura"
var sCuvch = "Cuvinte cheie"
var sPretm = "Pret maxim"
var sDenp ="Denumire produs"
var sCont = "Cont" 
var sFirstPass = "Parola"
var sSecPass = "Confirmare"
var sUSLastName = "Nume"
var sUSFirstName = "Prenume"
var sWorldLastName = "Nume de familie"
var sWorldFirstName = "Porecla"
var sTitle = "Ocupatie"
var sCompanyName = "Firma"
var sUSAddress = "Adresa"
var sWorldAddress = "Adresa"
var sCity = "Localitate"
var sStateCode = "Cod"
var sWorldState = "Judet"
var sCountry = "Tara"
var sZIPCode = "Cod"
var sWorldPostalCode = "Cod"
var sPhone = "Numar telefon"
var sFax = "Numar fax"
var sDateOfBirth = "Data nasterii"
var sExpirationDate = "Data expirarii"
var sEmail = "E-mail"
var sOtherInfo = "Alte informatii"

var iIntPozInterval = "Nu este un numar intre 0 si 2147483647"
var iInt = "Sirul de caractere nu este format doar din cifre "
var iMaxInt = "Un intreg de tip Integer nu poate depasi 13 caractere "
var iMaxString = "Nu se pot depasi 30 caractere "
var iMaxAdress = "Campurile pe mai multe linii nu trebuie sa depaseasca 200 caractere "
var iConfPass = "Parola nu a fost confirmata corect "
var iContLen = "Numele contului sa contina intre 5 si 15 litere "
var iPassLen = "parola sa contina intre 5 si 15 litere "
var iEmail = "Acest camp trebuie sa fie o adresa de e-mail valida "
var iDay = "Acest camp trebuie fie intre 1 sid 31 "
var iMonth = "Acest camp trebuie sa fie intre 1 and 12 "
var iYear = "Acest camp trebuie sa aibe 4 cifre "
var iDatePrefix = "Ziua, Luna, Anul pentru  "
var iDateSuffix = " Data nu este valida "

var pEntryPrompt = "Please enter a "
var pEmail = "addresa email valida."
var pDay = "ziua intre 1 si 31."
var pMonth = "luna intre 1 si 12."
var pYear = "4 cifre pentru an."

var defaultEmptyOK = false

function makeArray(n) {
  for (var i = 1; i <= n; i++) {
    this[i] = 0
  } 
  return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function isEmpty(s){
  return ((s == null) || (s.length == 0))
}

function isWhitespace (s){
  var i;
  if (isEmpty(s)) return true;
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }
  return true;
}

function stripCharsInBag (s, bag){
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function stripCharsNotInBag (s, bag){
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if (bag.indexOf(c) != -1) returnString += c;
  }
  return returnString;
}

function stripWhitespace (s){return stripCharsInBag (s, whitespace)}

function charInString (c, s){ 
  for (i = 0; i < s.length; i++){
    if (s.charAt(i) == c) return true;
  }
  return false
}

function stripInitialWhitespace (s){
  var i = 0;
  while ((i < s.length) && charInString (s.charAt(i), whitespace))   i++;
  return s.substring (i, s.length);
}

function isLetter (c){
  return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c){
  return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c){
  return (isLetter(c) || isDigit(c))
}

function isInteger (s){
  var i;
  if(isEmpty(s)) 
    if (isInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isInteger.arguments[1] == true);
	
  for(i = 0; i < s.length; i++){
    var c = s.charAt(i);
    if (!isDigit(c)) return false;
  }
  return true;
}

function isSignedInteger (s){
  if (isEmpty(s)) 
    if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isSignedInteger.arguments[1] == true);
  else{
    var startPos = 0;
    var secondArg = defaultEmptyOK;
    if (isSignedInteger.arguments.length > 1)
    secondArg = isSignedInteger.arguments[1];
    if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
      startPos = 1;    
    return (isInteger(s.substring(startPos, s.length), secondArg))
  }
}

function isPositiveInteger (s){
  var secondArg = defaultEmptyOK;
  if (isPositiveInteger.arguments.length > 1)
    secondArg = isPositiveInteger.arguments[1];
  return (isSignedInteger(s, secondArg)
	&& ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

function isNonnegativeInteger (s){
  var secondArg = defaultEmptyOK;
  if (isNonnegativeInteger.arguments.length > 1)
    secondArg = isNonnegativeInteger.arguments[1];
  return (isSignedInteger(s, secondArg)
    && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isNegativeInteger (s){
  var secondArg = defaultEmptyOK;
  if (isNegativeInteger.arguments.length > 1)
    secondArg = isNegativeInteger.arguments[1];
  return (isSignedInteger(s, secondArg)
    && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}

function isNonpositiveInteger (s){
  var secondArg = defaultEmptyOK;
  if (isNonpositiveInteger.arguments.length > 1)
    secondArg = isNonpositiveInteger.arguments[1];
  return (isSignedInteger(s, secondArg)
    && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}

function isFloat (s){
  var i;
  var seenDecimalPoint = false;
  if (isEmpty(s)) 
    if (isFloat.arguments.length == 1) return defaultEmptyOK;
    else return (isFloat.arguments[1] == true);
  if (s == decimalPointDelimiter) return false;
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if ((c == decimalPointDelimiter) && !seenDecimalPoint) 
	  seenDecimalPoint = true;
    else if (!isDigit(c)) return false;
  }
  return true;
}

function isSignedFloat (s){
  if (isEmpty(s)) 
    if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
    else return (isSignedFloat.arguments[1] == true);
  else {
    var startPos = 0;
    var secondArg = defaultEmptyOK;
    if (isSignedFloat.arguments.length > 1)
       secondArg = isSignedFloat.arguments[1];
    if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
      startPos = 1;    
    return (isFloat(s.substring(startPos, s.length), secondArg))
  }
}

function isAlphabetic (s){
  var i;
  if (isEmpty(s)) 
    if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
    else return (isAlphabetic.arguments[1] == true);
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if (!isLetter(c))
      return false;
  }
  return true;
}

function isAlphanumeric (s){
  var i;
  if (isEmpty(s)) 
    if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
    else return (isAlphanumeric.arguments[1] == true);
  for (i = 0; i < s.length; i++){   
    var c = s.charAt(i);
    if (! (isLetter(c) || isDigit(c) ) )
      return false;
  }
  return true;
}

function isEmail (s){
  if (isEmpty(s)) 
    if (isEmail.arguments.length == 1) return defaultEmptyOK;
    else return (isEmail.arguments[1] == true);
  if (isWhitespace(s)) return false;
  var i = 1;
  var sLength = s.length;
  while ((i < sLength) && (s.charAt(i) != "@")){ i++}
  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;
  while ((i < sLength) && (s.charAt(i) != ".")){ i++}
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
  else return true;
}

function isYear (s){
  if (isEmpty(s)) 
    if (isYear.arguments.length == 1) return defaultEmptyOK;
    else return (isYear.arguments[1] == true);
  if (!isNonnegativeInteger(s)) return false;
  return (s.length == 4);
}

function isIntegerInRange (s, a, b){
  if (isEmpty(s)) 
    if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
    else return (isIntegerInRange.arguments[1] == true);
  if (!isInteger(s, false)) return false;
  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}

function isMonth (s){
  if (isEmpty(s)) 
    if (isMonth.arguments.length == 1) return defaultEmptyOK;
    else return (isMonth.arguments[1] == true);
  return isIntegerInRange (s, 1, 12);
}

function isDay (s){
  if (isEmpty(s)) 
    if (isDay.arguments.length == 1) return defaultEmptyOK;
    else return (isDay.arguments[1] == true);   
  return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year){
// Februarie are 29 zile in orice an div. la 4; exceptie cand an div 100 dar nu cu 400
  return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day){
  if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;
  var intYear = parseInt(year);
  var intMonth = parseInt(month);
  var intDay = parseInt(day);
  if (intDay > daysInMonth[intMonth]) return false; 
  if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
  return true;
}

function prompt (s){   window.status = s   }
function promptEntry (s){   window.status = pEntryPrompt + s  }

function warnEmpty (theField, s){
  theField.focus()
  alert(mPrefix + s + mSuffix)
  return false
}

function warnInvalid (theField, s){
  theField.focus()
  theField.select()
  alert(s)
  return false
}


function checkString (theField, s, emptyOK){
  if(checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
  if(((emptyOK == true) && (isEmpty(theField.value))) || (isWhitespace(theField.value))) return true;
  else{
    if(isWhitespace(theField.value)) 
      return warnEmpty (theField, s);
    if(theField.value.length>MaxString)	
      return warnInvalid(theField,iMaxString);
    else return true;
  }
  return false;
}

function checkStringLen(theField,s,no, emptyOK){
  if(checkStringLen.arguments.length == 3) emptyOK = defaultEmptyOK;
  if(((emptyOK == true) && (isEmpty(theField.value))) || (isWhitespace(theField.value))) return true;
  else{
    if(isWhitespace(theField.value)) 
      return warnEmpty (theField, s);
	var max = parseInt (no);  
	if(theField.value.length>max){
	  var text = "Maxim " + no + " caracatere"
	  return warnInvalid(theField,text);
	}  
    else return true;
  }
  return false;
}

function checkInt(theField, s, emptyOK){
  if (checkInt.arguments.length == 2) emptyOK = defaultEmptyOK;
  if (((emptyOK == true) && (isEmpty(theField.value)))||(isWhitespace(theField.value))) return true;
  else{
    if(isWhitespace(theField.value)) 
	  return warnEmpty (theField, s);
	if(theField.value.length>MaxInt)	
	  return warnInvalid(theField,iMaxInt);
    var norma = stripCharsInBag(theField.value, whitespace);//phoneNumberDelimiters)
	if(isInteger(norma,true)){  
	  var num = parseInt (norma);
      if((num > 2147483646) || (num < 0))
	    return warnInvalid(theField,iIntPozInterval);
    }	
	else
	  return warnInvalid(theField,iInt);
	return true;
  }
  return false;
}

function checkTextare(theField, s, emptyOK){
  if(checkTextare.arguments.length == 2) 
    emptyOK = defaultEmptyOK;
  if((emptyOK == true) && (isEmpty(theField.value))) return true;
  
  if(isWhitespace(theField.value)) 
    return warnEmpty (theField, s);
  if(theField.value.length>MaxAdress)	
    return warnInvalid(theField,iMaxAdress);
  else return true;
}

function checkPass(f0,f1,f2){
  if((f0.value.length < ContLength) || (f0.value.length > MaxCP))
    return warnInvalid(f0,iContLen);
  if((f1.value.length < PassLength) || (f1.value.length > MaxCP))
    return warnInvalid(f1,iPassLen);
  if(f1.value!=f2.value)
    return warnInvalid(f1,iConfPass);
  return true;	 
}


function checkEmail (theField, emptyOK){
  if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
  if ((emptyOK == true) && (isEmpty(theField.value))) return true;
  else if (!isEmail(theField.value, false)) 
    return warnInvalid (theField, iEmail);
  else return true;
}

function checkYear (theField, emptyOK){
  if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
  if ((emptyOK == true) && (isEmpty(theField.value))) return true;
  if (!isYear(theField.value, false)) 
    return warnInvalid (theField, iYear);
  else return true;
}
 
function checkMonth (theField, emptyOK){
  if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
  if ((emptyOK == true) && (isEmpty(theField.value))) return true;
  if (!isMonth(theField.value, false)) 
    return warnInvalid (theField, iMonth);
  else return true;
}

function checkDay (theField, emptyOK){
  if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
  if ((emptyOK == true) && (isEmpty(theField.value))) return true;
  if (!isDay(theField.value, false)) 
    return warnInvalid (theField, iDay);
  else return true;
}

function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay){
  if (checkDate.arguments.length == 4) OKtoOmitDay = false;
  if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
  if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
  if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
  else
    if (!isDay(dayField.value)) 
      return warnInvalid (dayField, iDay);
  if (isDate (yearField.value, monthField.value, dayField.value))
    return true;
  alert (iDatePrefix + labelString + iDateSuffix)
  return false
}

function getRadioButtonValue (radio){
  for (var i = 0; i < radio.length; i++){   if (radio[i].checked) { break }  }
  return radio[i].value
}

function do_reply() {
  document.view.submit();
}

function putvoid(obiect) {
  obiect.cuvch.value="";
  obiect.pretm.value="";
}


