
//ask approval before deleting
function deleteApproval(str){
	return confirm("Sunteti sigur ca vreti sa stergeti "+str+"?");
}

//ask approval before suspend
function suspendApproval(str){
	return confirm("Sunteti sigur ca vreti sa "+str+"?");
}


//check req fields
function inputCheck(obj){
	var inputFields = obj.getElementsByTagName('input');
	
	var selectFields = obj.getElementsByTagName('select');

	var textareaFields = obj.getElementsByTagName('textarea');
	
	var inputTypesArray = new Array();

	var notRequired = new Array();
	
	var errorStr,password,repeatPassword;
	
	var checked,checkboxes = false;
		
	inputTypesArray["submit"] = 1;
	inputTypesArray["hidden"] = 1;	
	inputTypesArray["checkbox"] = 1;
	
	notRequired["pozaProdus"] = 1;
	notRequired["reducere"] = 1;
	notRequired["fax"] = 1;
	notRequired["siteWeb"] = 1;
	notRequired["sigla"] = 1;
	notRequired["cuvinteCheie"] = 1;
	notRequired["telefon"] = 1;
	notRequired["firma_reprezentata"] = 1; ////notRequired["firma"] = 1;
	notRequired["activ"] = 1;
	notRequired["deLaFirma"] = 1;	
	notRequired["deLaFax"] = 1;	
	notRequired["acceptStiri"] = 1;	
	notRequired["cuvinteCheie"] = 1;	
	notRequired["activa"] =1;
	notRequired["pret"] =1;
	notRequired["tva"] =1;
	notRequired["telMob"] =1;
	notRequired["data_incepere_campanie"] =1;
	notRequired["link"] =1;
	
	//validez input
	for(var i=0;i < inputFields.length ;i++){
		if(!inputTypesArray[inputFields[i].type] && !notRequired[inputFields[i].name]){
			if(!LTrim(inputFields[i].value) && !inputFields[i].disabled){

				if(inputFields[i].name.substring(0,6) != 'limita' && inputFields[i].parentNode.parentNode.childNodes[1].firstChild.tagName != 'IMG' && inputFields[i].name != 'data_incepere_campanie'){

					if(inputFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue){
						errorStr = inputFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue;
					}else{
						errorStr = inputFields[i].parentNode.parentNode.childNodes[0].firstChild.nodeValue;
					}
					
				}else{
				
					if(inputFields[i].parentNode.parentNode.childNodes[1].childNodes[1].nodeValue){
						errorStr = inputFields[i].parentNode.parentNode.childNodes[1].childNodes[1].nodeValue;
					}else{ 
						if(inputFields[i].parentNode.parentNode.childNodes[1].childNodes[2].nodeValue){
							errorStr = inputFields[i].parentNode.parentNode.childNodes[1].childNodes[2].nodeValue;
						}else{
							errorStr = inputFields[i].parentNode.parentNode.childNodes[1].childNodes[0].nodeValue;
						}
					}
					
				}				
				//alert('Completati campul "'+substring(0,errorStr.length-4)+'" !')
				alert('Completati campul "'+errorStr+'" !')
				inputFields[i].focus();
				return false;
			}
			
			if(inputFields[i].name == 'email' || inputFields[i].name == 'deLaEmail')	{
				if(!emailCheck(inputFields[i].value)){
					inputFields[i].focus();
					return false;
				}	
					
			}
			
			if(inputFields[i].name == 'password' || inputFields[i].name == 'oldPassword' || inputFields[i].name == 'newPassword')	{
				password = inputFields[i].value;
			}
			
			if(inputFields[i].name == 'passwordR' || inputFields[i].name == 'newPasswordR')	{
				repeatPassword = inputFields[i].value;
			}
						
			if(password && repeatPassword){
				if(password != repeatPassword){
					alert('Campurile \"Parola\" si \"Reintrodu parola\" trebuie sa fie identice!')
					inputFields[i].focus();
					return false;
				}
			}
			
			if(inputFields[i].name == 'telFix' || inputFields[i].name == 'telMob' || inputFields[i].name == 'fax'){
				var stripped = inputFields[i].value.replace(/[\(\)\.\-\ \/ \\]/g, '');

				//strip out acceptable non-numeric characters
				if (!stripped.match(/^\d+$/)) {

					if(inputFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue)
						errorStr = inputFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue;
					else  errorStr = inputFields[i].parentNode.parentNode.childNodes[0].firstChild.nodeValue;
					
					alert('Campul '+ errorStr + ' contine caractere nepermise!');
					inputFields[i].focus();
					return false;
				}			
			}
			
			
		}else if(inputFields[i].name == 'telFix' || inputFields[i].name == 'telMob' || inputFields[i].name == 'fax'){
			if(LTrim(inputFields[i].value) != ''){
				var stripped = inputFields[i].value.replace(/[\(\)\.\-\ \/ \\]/g, '');

				//strip out acceptable non-numeric characters
				if (!stripped.match(/^\d+$/)) {

					if(inputFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue)
						errorStr = inputFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue;
					else  errorStr = inputFields[i].parentNode.parentNode.childNodes[0].firstChild.nodeValue;
					
					alert('Campul '+ errorStr + ' contine caractere nepermise!');
					inputFields[i].focus();
					return false;
				}			
			}
		}
		
		
		if(inputFields[i].type == 'checkbox' &&  !notRequired[inputFields[i].name]){
			checkboxes = true;
			if(inputFields[i].checked){
				checked = true;
			}
		}
		
	}
	if(checkboxes)
		if(!checked){
			alert('Nu ati selectat un articol!');
			return false;
		}
	
	//validez textarea	
	for(var i=0;i < textareaFields.length; i++){

/*			//scot spatiile din fata daca exista; cele de la sfarsit nu ma intereseaza
			while(''+textareaFields[i].value.charAt(0)==' '){
				textareaFields[i].value = textareaFields[i].value.substring(1,textareaFields[i].value.length)
			}
*/
			//dupa ce am scos spatiile daca tot mai e vid, atunci ...
			if(LTrim(textareaFields[i].value) == "" && !notRequired[textareaFields[i].name]){

				if(textareaFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue)
					errorStr = textareaFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue;
				else  errorStr = textareaFields[i].parentNode.parentNode.childNodes[0].firstChild.nodeValue;
			
				alert('Completati campul "'+errorStr+'" !')
				textareaFields[i].focus();
				return false;
			}
	}

	//validez select
	for(var i=0;i < selectFields.length ;i++){
			if(selectFields[i].name == 'subdomenii' || selectFields[i].name == 'localitati' || selectFields[i].name == 'subdomeniiAlese'){

				if(!selectFields[i].selectedIndex){

					if(selectFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue)
						errorStr = selectFields[i].parentNode.parentNode.childNodes[1].firstChild.nodeValue;
					else  errorStr = selectFields[i].parentNode.parentNode.childNodes[0].firstChild.nodeValue;
				
					alert('Completati campul "'+errorStr+'" !')
					selectFields[i].focus();
					return false;
				}
			}
	}
		
	/*
	var limitaAfis = obj.getElementsByTagName('limitaAfisari').value;
	
		// alert(inputFields[i].name);
			//admin->adauga reclama
			if ( limitaAfis == "" ){
					alert("yupi");
				//|| inputFields[i].name == "limitaClickuri" || inputFields[i].name == "limitaTimp" ){
				
				//if(!inputFields[i].value){
				//	alert('Completati campul 123!')
				//	inputFields[i].focus();
					return false;
				//}
			}			
	*/		
	return true;
		
}

//validare separata pentru promovare online
function validateCerere(){
	var serviciu = document.getElementById('serviciu_promovat');
	/*var data = document.getElementById('data_incepere_campanie');*/
	var nume = document.getElementById('nume');
	var email = document.getElementById('email');
	var telefon = document.getElementById('telefon');
	var companie = document.getElementById('companie');
	var site = document.getElementById('site');
	
	if(LTrim(serviciu.value) == ""){
		alert("Specificati ce serviciu / produs doriti sa promovati!");
		serviciu.focus();
		return false;
	}

	/*
	if(LTrim(data.value)== ""){
		alert("Specificati data la care incepe campania promotionala!");
		data.focus();
		return false;
	}*/

	if(LTrim(nume.value) == ""){
		alert("Completati campul 'Nume si prenume'!");
		nume.focus();
		return false;
	}

	if(LTrim(email.value) == ""){
		alert("Completati campul 'Adresa email'!");
		email.focus();
		return false;
	}else{
		if(!emailCheck(email.value)){
			email.focus();
			return false;
		}	
	}

	if(LTrim(telefon.value) == ""){
		alert("Completati campul 'Telefon'!");
		telefon.focus();
		return false;
	}

	if(LTrim(companie.value) == ""){
		alert("Completati campul 'Companie'!");
		companie.focus();
		return false;
	}

	if(LTrim(site.value) == ""){
		alert("Completati campul 'Site web'!");
		site.focus();
		return false;
	}
	return true;
}


//left trim for spaces
function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	if(v_length < 1){
		return "";
	}

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space || VALUE.charAt(iTemp) == '\n'){
		}else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
	iTemp = iTemp + 1;
	} //End While

	return strTemp;
} 


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. */
	alert("Adresa de email pare sa fie invalida (verificati numarul caracterelor \"@\" si \".\" )");
	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) {
			
		alert("Username-ul contine caractere invalide.");
		
		return false;
	   }
	}
	
	for (i=0; i<domain.length; i++) {
		
		if (domain.charCodeAt(i)>127) {
			
		alert("Domeniul adresei de email contine caractere nevalide.");
		
		return false;
	   }
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
	// user is not valid
	
	alert("Username-ul adresei de email pare sa fie invalid.");
	
	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) {
				
				alert("Adresa ip destinatie pare sa fie invalida!");
				
				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("Domeniul adresei de email pare sa nu fie 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("Adresa de email trebuie sa se termine cu un nume de domeniu valid sau o combinatie de doua litere din numele unei tari.");
		
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		
		alert("Adresa de email nu are host valid!");
		
		return false;
	}

// If we've gotten this far, everything's valid!
return true;
}
