/**
 * @author srky
 */
 
 	/* IE (strije verz) ne podrzava funkciju indexOf pa je moramo napisati  */
	if(!Array.indexOf){
    	Array.prototype.indexOf = function(obj, start){
    	    for(var i=(start||0); i<this.length; i++){
    	        if(this[i]==obj){
    	            return i;
    	        }
    	    }
    	}
    }
 
	
	function writeNRows(n, str)  {
		document.writeln("<br/>\n");
		for (var i=0; i<n; i++) {
			document.writeln("<br/>"+str+"...\n");
		}
	} 

	function isValidEmail(str) {
		// These comments use the following terms from RFC2822:
		// local-part, domain, domain-literal and dot-atom.
		// Does the address contain a local-part followed an @ followed by a domain?
		// Note the use of lastIndexOf to find the last @ in the address
		// since a valid email address may have a quoted @ in the local-part.
		// Does the domain name have at least two parts, i.e. at least one dot,
		// after the @? If not, is it a domain-literal?
		// This will accept some invalid email addresses
		// BUT it doesn't reject valid ones. 
		var atSym = str.lastIndexOf("@");
		if (atSym < 1) { return false; } // no local-part
		if (atSym == str.length - 1) { return false; } // no domain
		if (atSym > 64) { return false; } // there may only be 64 octets in the local-part
		if (str.length - atSym > 255) { return false; } // there may only be 255 octets in the domain
		
		// Is the domain plausible?
		var lastDot = str.lastIndexOf(".");
		// Check if it is a dot-atom such as example.com
		if (lastDot > atSym + 1 && lastDot < str.length - 1) { return true; }
		//  Check if could be a domain-literal.
		if (str.charAt(atSym + 1) == '[' &&  str.charAt(str.length - 1) == ']') { return true; }
		return false;
	}

 	function validateForm (formId, errorMessageId) {
 		var form = document.getElementById(formId);
 		var errMsg = document.getElementById(errorMessageId);
 		errMsg.innerHTML = '';
 		var name = form.name.value;
 		var regexpName = '[\!\"\#\%\&\'\(\)\;\<\=\>\?\[\\\]\*\+\,\-\.\/\:\^\_\{\|\}\~0123456789]';
 		var mailAdd = form.email.value;
 		if ((name.search(regexpName) != -1) || (name=="")) {
			errMsg.innerHTML = "<font color='red'> Please check if you entered your name correctly.</font>";
			form.name.focus();
			return false;
 		} else {
			if (!isValidEmail(mailAdd)){
				errMsg.innerHTML = "<font color='red'> Please check if you entered your e-mail address correctly.</font>";
				form.email.focus();
				return false;
			} else {
				return true;
			}
 		}
 	}
 	
 	function expires (days) {
	    var expirationDate = new Date();
	    expirationDate.setTime(expirationDate.getTime()+(days*24*60*60*1000));
	    return expirationDate;
 	}
 	
 	/* ako ne postoji cookie stvori ga i zapisi aktivan sadrzaj, vrati false 
 	 * ako postoji vrati true */
 	function checkCookie (activeContentId) {	
 		if (document.cookie == "" || document.cookie.indexOf("undefined") != -1) {
 			//alert(document.cookie);
 			updateCookie(activeContentId);
 			return false;
  		} else {
 			return true;
 		}
	 }
	 
	 function readCookie() {
	 	var cookieContent = document.cookie;
	 	var searchString = "activeContentId=";
	 	var start = cookieContent.indexOf(searchString, 0) + searchString.length;
	 	//alert('Citanje cookiea:unutra:' + cookieContent.substring(start));
	 	return cookieContent.substring(start);
	 }
	 
	 function updateCookie (activeContentId) {
 		
	      var expDate = expires(7);
	      var cookieContent;
	      cookieContent = "activeContentId=" + activeContentId + "; expires=" + expDate.toGMTString() + "; path=/";
	      document.cookie = cookieContent;
	      //alert('Update cookiea:' + document.cookie);
	 }