Array.prototype.inArray = function(val) {
	for (var i in this) { if (this[i] === val) return i; }
	return -1;
}

function addType(type, i){
	document.getElementById(type+ "_"+i).style.display="block";
}

function clearType(type,ind){
	document.getElementById(type + '_' + ind).style.display='none';
	
	var arrInput=document.getElementsByTagName('input');
	var arrSelect=document.getElementsByTagName('select');
	var arrTextarea=document.getElementsByTagName('textarea');
	
	for (i=0;i<arrInput.length;i++)
		if (arrInput[i].name.substr(arrInput[i].name.length-2)=='_'+ind){
			arrInput[i].value='';
		}
	
	for (i=0;i<arrSelect.length;i++)
		if (arrSelect[i].name.substr(arrSelect[i].name.length-2)=='_'+ind){
			arrSelect[i].selectedIndex=0;
		}
	
	for (i=0;i<arrTextarea.length;i++)
		if (arrTextarea[i].name.substr(arrTextarea[i].name.length-2)=='_'+ind){
			arrTextarea[i].value='';
		}
	
	
}


function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */


return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {

return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {

return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid


return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {

return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
//alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
//alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}


function sendForm(frm){
	var i;
	var trimite=1;
	var p1,p2; //vor tine parola si confirmarea parolei
	for (i=0;i<frm.elements.length;i++){
		var element=frm.elements[i];
		/**
		* multiple checks
		*/
		if(element.title.indexOf(';')>0){
			var tests = element.title.split(';');
			for(var t in tests){
				var elementtitle = tests[t];
				if (elementtitle=='Obligatoriu' && !element.value){
					trimite=0;
					alert('Va rugam sa completati campul ' + element.name);
				}
				if (elementtitle=='Numar'){
					var v = (element.value.length);
					if(v > 0){
						trimite = isNaN(parseInt(element.value)) ? 0:1;
						if(!trimite)
							alert('Va rugam sa completati campul ' + element.name + ' cu un numar.');
							element.focus();
					}
				}
				if (elementtitle=='Parola')
					p1=element.value;
				if (elementtitle=='Parola_confirm')
					p2=element.value;
				if (element.title.substring(0,2)=='lt' && (x=parseInt(element.title.substring(2)))){
					var val = parseInt(element.value);
					val = isNaN(val) ? 0 : val;
					x= isNaN(x)?0:x;
					if (val>=x){
						trimite=0;
						alert('Ati introdus un numar in afara limitelor admise');
					}
				}
				if (element.title.substring(0,3)=='lte' && (x=parseInt(element.title.substring(3)))){
					var val = parseInt(element.value);
					val = isNaN(val) ? 0 : val;
					x= isNaN(x)?0:x;
					if (val>x){
						trimite=0;
						alert('Ati introdus un numar in afara limitelor admise');
					}
				}
				if (element.title.substring(0,2)=='gt' && (x=parseInt(element.title.substring(2)))){
					var val = parseInt(element.value);
					val = isNaN(val) ? 0 : val;
					x= isNaN(x)?0:x;
					if (val<=x){
						trimite=0;
						alert('Ati introdus un numar in afara limitelor admise');
					}
				}
				if (element.title.substring(0,3)=='gte' && (x=parseInt(element.title.substring(3)))){
					var val = parseInt(element.value);
					val = isNaN(val) ? 0 : val;
					x= isNaN(x)?0:x;
					if (val < x){
						trimite=0;
						alert('Ati introdus un numar in afara limitelor admise');
					}
				}
				if (elementtitle.substring(0,7)=='Custom:' && (x=elementtitle.substring(7)) && !element.value){
					trimite=0;
					alert(x);
				}
				if (elementtitle.substring(0,10)=='Composite:'){
					trimite = 0;
					var elemente = elementtitle.split(':');
					var x = '';
					var def = elemente[1];
					var composite = def.split('|');
					var elems = new Array();
					if(composite.length>=1){
						for(var j in composite){
							var tmp = document.getElementById(composite[j]);
							if(tmp.value){
								trimite=tmp.value.length;
								elems[composite[j]] = tmp.value;
								break;
							}
						}
					}
					if(trimite==0){
						x = "Formularul nu este completat corect. O combinatie de campuri este invalida.";
						if(elemente.length==3){
							x=elemente[2];
						}
						alert(x);
					}
				}
			}
		}
// email
		if (element.title=='Email'){
			if (!emailCheck(element.value)){
				trimite=0;
				if (sendForm.arguments[1]=='en')
					alert('Please check the Email address');
				else
					alert('Va rugam verificati adresa de Email introdusa');
				element.focus();
				break;
			}
				
		}


//campul cu titlu 'Obligatoriu' este intors daca nu area o valoare definita
		if (element.title=='Obligatoriu' && !element.value){
			trimite=0;
			if (sendForm.arguments[1]=='en')
				alert('Please fill in the current field');
			else
				alert('Va rugam sa completati campul curent');
			element.focus();
			break;
		}
//campul cu titlu 'Numar' este intors daca nu are o valoare numerica definita
		if (element.title == 'Numar'){

			trimite = (isNaN(parseInt(element.value))) ? 0:1;
			if(trimite == 0){
				alert('Va rugam sa completati campul curent cu un numar.');
				element.focus();
				break;
			}
		}

//titlu este parola sau confirma parola - stochez variabile cu valoarea campului
		if (element.title=='Parola')
			p1=element.value;
		if (element.title=='Parola_confirm')
			p2=element.value;

//titlul este 'lt' urmat de un numar - intorc campul daca valoarea sa este mai mare sau egala decat numarul din titlu
		if (element.title.substring(0,2)=='lt' && (x=parseInt(element.title.substring(2)))){
			var val = parseInt(element.value);

			if (isNaN(val)) {
				trimite = 0;
				alert("Trebuie sa introduceti un numarul in campul curent");
				element.focus();
				break;
			}

			x= isNaN(x)? 0 : x;
			if (val>=x){
				trimite=0;
				alert('Ati introdus un numar in afara limitelor admise');
				element.focus();
				break;
			}
		}

//titlul este 'lte' urmat de un numar - intorc campul daca valoarea sa este mai mare strict decat numarul din titlu
		if (element.title.substring(0,3)=='lte' && (x=parseInt(element.title.substring(3)))){
			var val = parseInt(element.value);

			if (isNaN(val)) {
				trimite = 0;
				alert("Trebuie sa introduceti un numarul in campul curent");
				element.focus();
				break;
			}

			x= isNaN(x)?0:x;
			if (val>x){
				trimite=0;
				alert('Ati introdus un numar in afara limitelor admise');
				element.focus();
				break;
			}
		}

//titlul este 'gt' urmat de un numar - intorc campul daca valoarea sa este mai mica sau egala decat numarul din titlu
		if (element.title.substring(0,2)=='gt' && (x=parseInt(element.title.substring(2)))){
			var val = parseInt(element.value);

			if (isNaN(val)) {
				trimite = 0;
				alert("Trebuie sa introduceti un numarul in campul curent");
				element.focus();
				break;
			}

			x= isNaN(x)?0:x;
			if (val<=x){
				trimite=0;
				alert('Ati introdus un numar in afara limitelor admise');
				element.focus();
				break;
			}
		}

//titlul este 'gte' urmat de un numar - intorc campul daca valoarea sa este strict mai mica decat numarul din titlu
		if (element.title.substring(0,3)=='gte' && (x=parseInt(element.title.substring(3)))){
			var val = parseInt(element.value);

			if (isNaN(val)) {
				trimite = 0;
				alert("Trebuie sa introduceti un numarul in campul curent");
				element.focus();
				break;
			}

			x= isNaN(x)?0:x;
			if (val < x){
				trimite=0;
				alert('Ati introdus un numar in afara limitelor admise');
				element.focus();
				break;
			}
		}

//titlu este 'custom:' urmat de un text si valaorea campul este nula - intorc campul cu mesajul de eroare care vine dupa 'custom'
		if (element.title.substring(0,7)=='Custom:' && (x=element.title.substring(7)) && !element.value){
			trimite=0;
			alert(x);
			element.focus();
			break;
		}

/**
* titlu este 'composite:' - structura asteptata : "Composite: id1|id2|id3 : 'mesaj' SAU Composite: id1 : 'mesaj'
* Daca unul dintre id1, id2, id3 are valoare formularul este validat
* in cazul 2 - daca id1 are valoare - formularul este validat
*/
		if (element.title.substring(0,10)=='Composite:'){
			trimite = 0;
			var elemente = element.title.split(':');
			var x = '';
			var def = elemente[1];
			var composite = def.split('|');
			var elems = new Array();
			if(composite.length>=1){
				for(var j in composite){
					var tmp = document.getElementById(composite[j]);
					if(tmp && tmp.value){
						trimite=tmp.value.length;
						elems[composite[j]] = tmp.value;
						break;
					}else {
						if(composite[j].indexOf('RegExp')>=0 && composite[j].indexOf('=')>=0){
							var p = composite[j].split('=');
							if(p.length >= 2){
								//alert(p[1]);
								var re = new RegExp(p[1]);
								if(!element.value.match(re)){
									trimite = 0;
									if(p.length==3){
										x = p[2];
									}
									break;
								}
							}
						}
					}
				}
			}

			if(trimite==0){
				if(!x.length){
					x = "Formularul nu este completat corect. O combinatie de campuri este invalida.";
					if(elemente.length==3){
						x=elemente[2];
					}
				}
				alert(x);
				element.focus();
				return;
			}
		}
	}

//daca am campuri de parola si ele nu se potrivesc - intorc campurile cu mesaj de eroare
	if ((p1 || p2) && p1!=p2){
		trimite=0;
		alert('Va rugam, confirmati corect parola');

	}

	


//trimite = 0;
	if(trimite!=0){
		frm.submit();
		return;
	}
}


function getfileextension(fileinput) 
{ 
 
 if(!fileinput ) return ""; 
 var filename = fileinput.value; 
 if( filename.length == 0 ) return ""; 
 var dot = filename.lastIndexOf("."); 
 if( dot == -1 ) return ""; 
 var extension = filename.substr(dot+1,filename.length); 
 return extension; 
} 

function checkFileType(inputId) 
{ 
 var arrExtensii = new Array ("doc", "docx", "txt", "pdf", 'jpg', 'jpeg', 'gif', 'png', 'tif', 'tiff', 'bmp');  
 var ext = getfileextension(inputId).toLowerCase(); 
 if( arrExtensii.inArray(ext) == -1){
  if (checkFileType.arguments[1]=='en')
  		alert("Please check the allowed file types for upload"); 
  else
 	alert("Va rugam verificati tipurile de fisiere permise pentru upload"); 
  inputId.value="";	
 }
} 