function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

	function CheckPhone(tel)
	{
		allowed="1234567890.-/";
		for (i = 0; i < tel.length; i++)
		{
			var c=tel.charAt(i);
			// test for spaces
			if(c != " ")
			{
				if(allowed.indexOf(c)==-1)
					return false;
			}		
		}
		return true;
	}

	function validare(param)
	{
	    var M_login,M_password,M_password_cf,M_firstname,M_lastname,M_email,M_city,M_country,M_zip_code,M_phone,M_address;
 		
 	    if(param!=1)
		{
		    M_login=document.contfrm.M_login;
			M_password=document.contfrm.M_password;
 		    M_password_cf=document.contfrm.M_password_cf;		
			M_email_cf = document.contfrm.M_email_cf;
		}	
	    M_firstname=document.contfrm.M_firstname;
	    M_lastname=document.contfrm.M_lastname;
	    M_email=document.contfrm.M_email;
	    M_city=document.contfrm.M_city;		
	    M_address=document.contfrm.M_address;		
	    M_country=document.contfrm.M_country;		
	    M_zip_code=document.contfrm.M_zip_code;		
	    M_phone=document.contfrm.M_phone;		
	    M_how_find=document.contfrm.M_how_find;
		
		if(param == 0)
			{
			if(document.contfrm.terms.checked == false)
				{
				alert("You must agree to the terms and conditions!");
				return false;
				}
			}
		if(param!=1)
		{
	  	    if (M_login.value.length==0)
	  	    {
	   	    	alert ("Please insert 'Desired login'!");
				M_login.focus();
	    		M_login.select();
	    		return false;
	   	    }
			else
			{
				//detect spaces
				splitlogin = M_login.value.split(" ");
				if(splitlogin.length > 1)
				{
		   	    	alert ("Please remove spaces from 'Desired login'!");
					M_login.focus();
		    		M_login.select();
	    			return false;
				}
				else
				{
				if(CheckPhone(M_login.value))
					{
		   	    	alert ("'Desired login' must contain characters too!");
					M_login.focus();
		    		M_login.select();
	    			return false;
					}
				}
			}			
		    if (M_password.value.length<6 || M_password.value!=M_password_cf.value)
	  	    {
		   		alert ('Please insert a correct password!');
		   		M_password.focus();
	    		M_password.select();
				return false;
		    }
		}	
  	    if (M_firstname.value.length<2)
  	    {
   	    	alert ("Please insert your 'First name'!");
			M_firstname.focus();
    		M_firstname.select();
    		return false;
   	    }
  	    if (M_lastname.value.length<2)
  	    {
   	    	alert ("Please insert your 'Last name'!");
			M_lastname.focus();
    		M_lastname.select();
    		return false;
   	    }		

		/* alta varianta
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (!filter.test(M_email.value))*/
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(M_email.value)))
	    {
	    	alert('Incorrect email address!!');
			M_email.focus();
			M_email.select();
			return false;
	    } 
		if(param != 1)
		{
		    if (M_email.value!=M_email_cf.value)
	  	    {
		   		alert ('Your email confirmation is incorrect!');
		   		M_email_cf.focus();
	    		M_email_cf.select();
				return false;
		    }
		}
  	    if (M_city.value.length==0)
  	    {
   	    	alert ("Please insert 'City'!");
			M_city.focus();
    		M_city.select();
    		return false;
   	    }				
  	    if (M_address.value.length==0)
  	    {
   	    	alert ("Please insert 'Address'!");
			M_address.focus();
    		M_address.select();
    		return false;
   	    }				
	    if(document.forms['contfrm'].elements['M_country'].value==0)
	    {
			alert('Please select your country!');
			return false;
	    }
		if(M_zip_code.value.length==0)
		{
			alert ("Please insert a valid 'zip code'");
			M_zip_code.focus();
			M_zip_code.select();
			return false;
		}	   
		if(M_phone.value.length==0 || !CheckPhone(M_phone.value))
		{
			alert ('Please insert a correct phone number!');
			M_phone.focus();
			M_phone.select();
			return false;
	    }		 
		if(param!=1)
		{
	  	    if (M_how_find.value.length < 4)
	  	    {
	   	    	alert ("Please type 'How did you find us'!");
				M_how_find.focus();
	    		M_how_find.select();
	    		return false;
	   	    }		
		}
   	}

	function validare_contact()
	{
	    var M_firstname,M_lastname,M_email,M_city,M_country,M_phone,M_comments;
		document.contactfrm.M_browser.value = trimString(document.contactfrm.M_browser.value);
		document.contactfrm.M_system.value = trimString(document.contactfrm.M_system.value);
	    M_firstname=document.contactfrm.M_firstname;
	    M_lastname=document.contactfrm.M_lastname;
	    M_email=document.contactfrm.M_email;
	    M_city=document.contactfrm.M_city;		
	    M_browser=document.contactfrm.M_browser;		
	    M_system=document.contactfrm.M_system;		
	    M_country=document.contactfrm.M_country;		
	    M_phone=document.contactfrm.M_phone;		
	    M_comments=document.contactfrm.M_comments;				
 		
  	    if (M_firstname.value.length==0)
  	    {
   	    	alert ("Please insert your 'First name'!");
			M_firstname.focus();
    		M_firstname.select();
    		return false;
   	    }
  	    if (M_lastname.value.length==0)
  	    {
   	    	alert ("Please insert your 'Last name'!");
			M_lastname.focus();
    		M_lastname.select();
    		return false;
   	    }		
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(M_email.value)))
	    {
	    	alert('Incorrect email address!');
			M_email.focus();
			M_email.select();
			return false;
	    }
  	    if(document.forms['contactfrm'].elements['M_country'].value==0)
	    {
			alert('Please select your country!');
			return false;
	    }
		if(M_phone.value.length!=0) 
		{
			if(!CheckPhone(M_phone.value))
			{			
				alert ('Please insert a correct phone number!');
				M_phone.focus();
				M_phone.select();
				return false;
			}	
	    }
  	    if (M_browser.value.length==0)
  	    {
   	    	alert ("Please insert 'Browser'!");
			M_browser.focus();
    		M_browser.select();
    		return false;
   	    }
  	    if (M_system.value.length==0)
  	    {
   	    	alert ("Please insert 'System'!");
			M_system.focus();
    		M_system.select();
    		return false;
   	    }
  	    if (M_comments.value.length==0)
  	    {
   	    	alert ("Please type your comment!");
			M_comments.focus();
    		M_comments.select();
    		return false;
   	    }		
   	}
	
	function CK_log(frm)
	{
	    var uname,pass;
	    uname=document.forms[frm].uname;
 	    pass=document.forms[frm].pass;
  	    if (uname.value.length==0)
  	    {
   	    	alert ("Please insert your login!");
			uname.focus();
    		uname.select();
    		return false;
   	    }
  	    if (pass.value.length==0)
  	    {
   	    	alert ("Please insert password!");
			pass.focus();
    		pass.select();
    		return false;
   	    }
	}

	function CK_image(frm_u)
	{
	    var up_image;
	    up_image = document.forms[frm_u].up_image;
  	    if (up_image.value.length==0)
  	    {
   	    	alert ("Please choose a image file!");
			up_image.focus();
    		up_image.select();
    		return false;
   	    }
		/*else
		{
			var file;
			file = up_image.value;
			extArray = new Array(".jpg", ".jpeg");
			allowSubmit = false;

			while (file.indexOf("\\") != -1)
				file = file.slice(file.indexOf("\\") + 1);
			ext = file.slice(file.indexOf(".")).toLowerCase();
			for (var i = 0; i < extArray.length; i++)
			{
				if (extArray[i] == ext) { allowSubmit = true; break; }
			}

			if(!allowSubmit)
			{
	   	    	alert ("Please choose a JPG image file!");
				up_image.focus();
    			up_image.select();
    			return false;
			}
		}*/
		else
		{
			document.all['td1'].style.display='none';
			document.all['td2'].style.display='block';
			return true;
		}	
	}

	function CK_image_ag(frm_u)
	{
	    var up_image;
	    var M_terms;
	    M_terms = document.forms[frm_u].M_terms;
	    up_image = document.forms[frm_u].up_image;
	    
	    if (M_terms.checked == false)
  	    {
   	    	alert ("Please read the terms and conditions and tick the checkbox if you agree.");
			return false
	    }
  	    if (up_image.value.length==0)
  	    {
   	    	alert ("Please choose a image file!");
			up_image.focus();
    		up_image.select();
    		return false;
   	    }
	    
	}

function validSum(sText)
	{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   if (!IsNumber)
	   	{
		
		return false;
		}
   return IsNumber;   
   }
   
 
	
	function CK_upload(T)
	{
	    var name,description,keywords,terms,sr_price;
	    name=document.uploadfrm1.M_title;
	    description=document.uploadfrm1.M_description;		
	    keywords=document.uploadfrm1.M_keywords;		
	    terms=document.uploadfrm1.M_terms;		
  	    sr_price=document.uploadfrm1.sr_price;
		
		if (sr_price.value!=0 && sr_price.value!='' && (sr_price.value<350 || sr_price.value>25000))
  	    {
   	    	alert ("Minimum value is 350, maximum value is 25000!");
			sr_price.focus();
    		sr_price.select();
    		return false;
   	    }
		
		if (name.value.length==0)
  	    {
   	    	alert ("Please type an image title!");
			name.focus();
    		name.select();
    		return false;
   	    }
  	    if (description.value.length==0)
  	    {
   	    	alert ("Please type an image description!");
			description.focus();
    		description.select();
    		return false;
   	    }
		if(name.value == description.value)
		{
			alert("Title and description have identical data. Please provide a more accurate description of the image.");
			description.focus();
    		description.select();
    		return false;
		}
	    if(document.forms['uploadfrm1'].elements['M_category_1'].value==0 && document.forms['uploadfrm1'].elements['M_category_2'].value==0 && document.forms['uploadfrm1'].elements['M_category_3'].value==0)
	    {
			alert('Please select one or more categories for image!');
			return false;
	    }
  	    if (keywords.value.length==0)
  	    {
   	    	alert ("Please type few keywords for image!");
			keywords.focus();
    		keywords.select();
    		return false;
   	    }		
		if(T == 0)
		{
			if (terms.checked == false)
  		    {
   				alert ("Please read the terms and conditions and tick the checkbox if you agree!");
    			return false;
	   	    }		
		}
	
		if (validSum(sr_price.value))
			{
			if (sr_price.value!=0 && sr_price.value!='' && (sr_price.value<350 || sr_price.value>25000) )
		 	    {
		  	    alert ("Minimum value is 350, maximum value 25000!");
				sr_price.focus();
		   		sr_price.select();
		   		return false;
		  	    }
			}
		else 
			{
			alert ("Numeric value required!");
			sr_price.focus();
	   		sr_price.select();
	   		return false;
			}
	
	   if (document.uploadfrm1.checkbox_sr.checked && !(document.uploadfrm1.checkbox_usereco.checked || document.uploadfrm1.sr_price.value))
		   	{
			alert ("For activating Sell the Rights license for this image you need to set a price or use recommended price!");
			sr_price.focus();
	   		sr_price.select();
		   	return false;
			}
		return;
	}
	
	function validare_board()
	{
	    var M_subject,M_message;
	    M_subject=document.boardfrm.M_subject;
	    M_message=document.boardfrm.M_message;		
		
  	    if (M_subject.value.length==0)
  	    {
   	    	alert ("Please type the subject!");
			M_subject.focus();
    		M_subject.select();
    		return false;
   	    }		
  	    if (M_message.value.length==0)
  	    {
   	    	alert ("Please type the message!");
			M_message.focus();
    		M_message.select();
    		return false;
   	    }				
	}
	
	function ConvertCF()
	{
	    var convertvalue;
	    convertvalue=document.convertfrm.convertvalue;
		
  	    if (convertvalue.value.length==0 || convertvalue.value==0 || isNaN(convertvalue.value))
  	    {
   	    	alert ("Incorrect credits value!");
			convertvalue.focus();
    		convertvalue.select();
    		return false;
   	    }		
	}	
	
	function PayCK()
	{
	    var payvalue;
	    payvalue=document.payfrm.payvalue;
  	    if (payvalue.value.length==0 || payvalue.value==0 || isNaN(payvalue.value))
  	    {
   	    	alert ("Incorrect payment value!");
			payvalue.focus();
    		payvalue.select();
    		return false;
   	    }		
	}		
	
	function ImageDownCK(frmname)
	{
	    var M_agree;		
		M_agree=document.forms[frmname].M_agree;
  	    if (M_agree.checked == false)
  	    {
   	    	alert ("Please read the terms and conditions and tick the checkbox if you agree.");
			return false
   	    }
		else
			return true;
	}

	function TermsCK(frmname)
	{
	    var M_agree;		
		M_agree=document.forms[frmname].M_agree;
  	    if (M_agree.checked == false)
  	    {
   	    	alert ("Please read the terms and conditions and tick the checkbox if you agree.");
			return false
   	    }
		else
			return true;
	}


	function PassCK()
	{
	    var M_email_request;
	    M_email_request=document.passfrm.M_email_request;

		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(M_email_request.value)))
	    {
	    	alert('Incorrect email address!');
			M_email_request.focus();
			M_email_request.select();
			return false;
	    }
	}
	
	function ConfirmDelete(frm)
	{
		if (window.confirm("Are you sure you want to delete this file?"))
			document.forms[frm].submit();
	} 
	
	function DisableImage(frm)
	{
		var rd_confirm = document.forms[frm].rd_confirm;
		var M_reason = document.forms[frm].M_reason.value;
		if(!rd_confirm[0].checked)
		{
			alert("Please select YES to disable image");
			return false;
		}
		if(M_reason.length < 3)		
		{
			alert("Please write a reason to disable image");
			return false;
		}
		return true;
	}
	
	function BNewsAdd(field)
	{
		var news = document.forms['frm_forum'].message;		
		if(news.value.length == 0)
		{
			alert("Please type your text!");
			news.focus();
			news.select();
			return false;
		}
		else
		{
			var MaxCharacters = 1500;
			if (news.value.length > MaxCharacters)
			{
				alert('Please type maximum '+MaxCharacters+' characters in this field!');
				news.value = news.value.substring(0, MaxCharacters);
				return false;
			}
		}
	} 
	
	function CollectionsCheck(forma,imageid)
	{
		var collectionid = forma.collectionid.value;
		if(collectionid == 0)
		{
			alert('Please select a collection!');
			return false;
		}
		if(collectionid == -1)
		{
			forma.do_action.value="";
			forma.collectionid.value="";
			forma.action = "collections.php?image_identifier="+imageid+"#point";
		}	
	}
	
	function CollectionsAdd(forma,mode)
	{
		if(mode == 1)
			var image_identifier = forma.image_identifier;		
		var ctitle = forma.ctitle;				
		var cdescription = forma.cdescription;				
		var ckeywords = forma.ckeywords;
						
		if(mode == 1)
		{
			if(image_identifier.value.length == 0)
			{
				alert("Please paste the ID or URL of the first image!");
				image_identifier.focus();
				image_identifier.select();
				return false;
			}
		}	
		if(ctitle.value.length == 0)
		{
			alert("Please type title of collection!");
			ctitle.focus();
			ctitle.select();
			return false;
		}
		if(cdescription.value.length == 0)
		{
			alert("Please type description!");
			cdescription.focus();
			cdescription.select();
			return false;
		}				
		if(ckeywords.value.length == 0)
		{
			alert("Please type keywords!");
			ckeywords.focus();
			ckeywords.select();
			return false;
		}		
	}
	
	function CollectionsSearch(forma)
	{
		var srh_coll = forma.srh_coll;				
		if(srh_coll.value.length == 0 || srh_coll.value=="your keyword here")
		{
			alert("Please type keywords!");
			srh_coll.focus();
			srh_coll.select();
			return false;
		}		
	}
	
	function PaymentRequest(forma)
	{
		var paytype = forma.paytype;				
		var paypalid = forma.paypalid;

		if(paytype[0].checked)
		{
			if(paypalid.value == "")
			{
				alert("Please complete your paypal ID!");
				paypalid.focus();
				paypalid.select();
				return false;
			}
			return true;
		}
		return true;
	}

	function OMS_review(forma)
	{
		var message = forma.message;				
		var do_action = forma.do_action;				
		var reason = forma.reason;				
		var coll = forma.elements;
		if(do_action.value == "escalate")
		{
			if(message.value.length == 0)
			{
				alert("Please type message to site admins!");
				message.focus();
				message.select();
				return false;
			}
		}
		if(do_action.value == "refuse")
		{
			check = false;
			nr = coll.length;
			for (i=0;i<=nr;i++)
			{
				if(null != coll[i] &&  null != coll[i].name)
				{
					if(coll[i].name.substr(0,3)=="rs_")
					{
						if(coll[i].checked == true)
						{		
							check = true;
						}
					}
				}	
			}
			if(reason.value.length == 0 && !check)
			{
				alert("Please type or choose reason to refuse image!");
				reason.focus();
				reason.select();
				return false;
			}
		}
	}

	function LightboxAdd(forma)
	{
		var lname = forma.lname;				
						
		if(lname.value.length == 0)
		{
			alert("Please type title of the lightbox!");
			lname.focus();
			lname.select();
			return false;
		}
	}
	
function check_secure_form()
	{
	var x_card_num = document.frm_secure.x_card_num;
	var x_exp_date = document.frm_secure.x_exp_date;
	var x_card_code = document.frm_secure.x_card_code;
	var x_first_name = document.frm_secure.x_first_name;	
	var x_last_name = document.frm_secure.x_last_name;
	var x_address = document.frm_secure.x_address;
	var x_city = document.frm_secure.x_city;
	var x_zip = document.frm_secure.x_zip;
	var x_country = document.frm_secure.x_country;
	var x_phone = document.frm_secure.x_phone;
	var x_email = document.frm_secure.x_email;
	
	
	if(x_card_num.value == '')
		{
			alert("Please insert Card Number!");
			x_card_num.focus();
			x_card_num.select();
			return false;
		}	
	
	if(x_exp_date.value == '')
		{
			alert("Please insert Expiration Date!");
			x_exp_date.focus();
			x_exp_date.select();
			return false;
		}	
	
	if(x_card_code.value == '')
		{
			alert("Please insert Card Code!");
			x_card_code.focus();
			x_card_code.select();
			return false;
		}
			
    if(x_first_name.value == '')
		{
			alert("Please insert First Name!");
			x_first_name.focus();
			x_first_name.select();
			return false;
		}	
	if(x_last_name.value == '')
		{
			alert("Please insert Last Name!");
			x_last_name.focus();
			x_last_name.select();
			return false;
		}	
	
	if(x_address.value == '')
		{
			alert("Please insert Address!");
			x_address.focus();
			x_address.select();
			return false;
		}	
	
	if(x_city.value == '')
		{
			alert("Please insert City!");
			x_city.focus();
			x_city.select();
			return false;
		}

	if(x_zip == '')
		{
			alert("Please insert Zip/Postal Code!");
			x_zip.focus();
			x_zip.select();
			return false;
		}	
	
	if(x_country.value == '')
		{
			alert("Please insert Country!");
			x_country.focus();
			x_country.select();
			return false;
		}	
	
	if(x_phone.value == '')
		{
			alert("Please insert Phone!");
			x_phone.focus();
			x_phone.select();
			return false;
		}
			
    if(x_email.value == '')
		{
			alert("Please insert Email!");
			x_email.focus();
			x_email.select();
			return false;
		}	
	return true;
	}

	