/* POSISION BACKGROUND */
// no longer used but you never know
function alertSize() {
	var myWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
    	//Non-IE
    	myWidth = window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
	}
	else if (document.body && document.body.clientWidth) {
    	//IE 4 compatible
    	myWidth = document.body.clientWidth;
	}
	if (myWidth < 1200) {
		var pad = parseInt((myWidth-1000)/2);
		if (pad < 0) {
			pad = 0; 
		}
		document.getElementById('content').style.marginLeft=pad + 'px';
		document.getElementById("container").style.background="url('images/container_bg.jpg') -" + (100 - parseInt(pad)) + "px 0px no-repeat";
	}
}

/* CUSTOM CHECKBOX */
// check/uncheck simple checkbox
function checkbox(el, input)
{
	var mainname = input.name.split("_");
	// if checked
	if (el.className == "checkboxon") {
		// uncheck and update value
		el.className = "checkbox";
		input.value = 0;
		// if All was checked, uncheck it and update value 
		if (document.getElementById(mainname[0]).className  == "checkboxon") {
			document.getElementById(mainname[0]).className  = "checkbox";
			document.getElementById(mainname[0]).value = 0;
		}
	} 
	// if not checked
	else {
		// check and update value
		el.className = "checkboxon";
		input.value = 1;
		var allcheck = true;
		// get all checkboxes in category
		for (var i = 1; i < 20; i++) {
			try {
				var checks = mainname[0] + "_" + i;
				var box = document.getElementById(checks);
				// if an unchecked checkbox still exists than allcheck not true 
				if (box.className  == "checkbox") {
					allcheck = false;
				}
			}
			catch(e) {}
		}
		// if all checkboxes are on, check All checkbox and update value
		if (allcheck) {
			document.getElementById(mainname[0]).className  = "checkboxon";
			document.getElementById(mainname[0]).value = 1;
		}
	}
}

// check/uncheck all in a category
function checkAll(el, input)
{
	// if checked, uncheck it and update value
	if (el.className == "checkboxon") {
		el.className = "checkbox";
		input.value = 0;
		// get all checkboxes in category
		for (var i = 1; i < 20; i++) {
			try {
				var checks = input + "_" + i;
				box = eval(checks);
				// uncheck all and update value
				box.value = 0;
				document.getElementById(box.name).className  = "checkbox";
			}
			catch(e) {}
		}
	} 
	// if not checked, check it and update value
	else {
		el.className = "checkboxon";
		input.value = 1;
		// get all checkboxes in category
		for (var i = 1; i < 20; i++) {
			try {
				var checks = input + "_" + i;
				box = eval(checks);
				// check all and update value
				box.value = 1;
				document.getElementById(box.name).className  = "checkboxon";
			}
			catch(e) {}
		}
	}
}

/* CUSTOM DROPDOWN */

// function show/hide dropdown
var listMe = function(id) 
{
	var itemsList = document.getElementById(id);
	if (itemsList.style.display == "none")
	{
		itemsList.style.display = "block";
		//alert(1 + '--->' + id);
	}	
	else {
		itemsList.style.display = "none";
		//alert(2);
	}
}

// function set input value
var setValue = function(val, list_id, target_id) {
	document.getElementById(target_id).value = val.innerHTML;
	listMe(list_id);
}

// event for show/hide dropdown on click outside
function hookEvent(element, eventName, callback)
{
	if(typeof(element) == "string")
	element = document.getElementById(element);
	if(element == null)
	return;
	
	try {
		if(typeof element.addEventListener == "function")
		{
			if(eventName == 'mousewheel')
			element.addEventListener('DOMMouseScroll', callback, false);
			element.addEventListener(eventName, callback, true);
		}
		else if(element.attachEvent)
			element.attachEvent("on" + eventName, callback);
	}
	catch(e) {};
}
// function show/hide dropdown on click outside using hookEvent
function whatever(event)
{
	var target = event.explicitOriginalTarget||event.srcElement||document.activeElement;
	var ddd = new Array();
	ddd[0] = document.getElementById("list_target");
	ddd[1] = document.getElementById("list_target1");
	ddd[2] = document.getElementById("list_target2");
	ddd[3] = document.getElementById("list_target3");
	var i = 0;
	if (target.tagName) {
		if ((target.id.indexOf('inp_target') == -1) && (target.id.indexOf('list_target') == -1) && (target.id.indexOf('arrow') == -1) 
				// hack for other browsers than IE & FF
				&& (target.id.indexOf('li') == -1))
		{
			for (i=0; i<4; i++) {
				if (ddd[i] != null) {
					ddd[i].style.display = "none";
				}
			}
		}
	} 
	else {
		if (target.parentNode.parentNode.id.indexOf('list_target') == -1) {
			for (i=0; i<4; i++) {
				if (ddd[i] != null) {
					ddd[i].style.display = "none";
				}
			}
		}
	}
}

// register event
hookEvent(document, 'click', whatever);


/* SHOW/HIDE DIVS CONTENT */
// used for estimator.php
var currentStep=0;
function step(div) {
	var pops = new Array();
	pops[0] = "pas1";
	pops[1] = "pas2";
	pops[2] = "pas3";
	var divs = [];
	
	for(var i = 0; i < div; i++){
		divs.push(pops[i]);
	}

	if(!validate(divs,'erori_'+pops[currentStep])){
		return false;
	}
	
	
	var thediv=document.getElementById(pops[div]);
	
	if(thediv.style.display == "none")
	{
		for (var i=0; i < pops.length; i++)
		{
			document.getElementById(pops[i]).style.display="none";
		}
		thediv.style.display="block";
		currentStep=div;
	} 
	return false;
}
// used for parteneri.php
function ag(div) {
	var ag  = new Array();
	ag[0] = "agentie_web";
	ag[1] = "agentie_publicitate";
	ag[2] = "publisher";
	
	var thediv=document.getElementById(div);
	
	if(thediv.style.display == "none")
	{
		for (i=0; i < ag.length; i++)
		{
			document.getElementById(ag[i]).style.display="none";
		}
		document.getElementById(div).style.display="block";
	} 
	return false;
}
// used for optimizare.php
function web(div) {
	var web  = new Array();
	web[0] = "iphone";
	web[1] = "android";
	
	var thediv=document.getElementById(div);
	
	if(thediv.style.display == "none")
	{
		for (i=0; i < web.length; i++)
		{
			document.getElementById(web[i]).style.display="none";
		}
		document.getElementById(div).style.display="block";
	} 
	return false;
}


/* POSISION CUSTOM BROWSE BUTTON FOR OPERA */
// detect browsers
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	},
	// detect version
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	// detect browser
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	]

};

// initiate browser detect
BrowserDetect.init();

// let Opera have her very own special class
function styleMe() {
	if (BrowserDetect.browser != 'Opera') {
		var allInputs = document.getElementsByTagName('input');
		for (i=0; i<allInputs.length; i++) {
			if (allInputs[i].className=='hide') {
				allInputs[i].className = 'hide2';
			}
		}
	}
}


/* MAKE TARGET NOT SELECTABLE */
// we use it for checkboxes
function disableSelection(target) {
	if (typeof target.onselectstart!="undefined") //IE route
    	target.onselectstart = function() {
			return false
		}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	    target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
	    target.onmousedown=function() {
			return false
		}
    target.style.cursor = "default"
}



function validate(tipuri,eroriDiv){
	var erori = [];
	var campuri = {
		'pas1':{
			'aplicatie':'Numele aplicatiei trebuie completat',
			'piata':'Selectati categoria',
			'proiect':'Selectati proiectul',
			'descriere':'Scurta descriere trebuie completata'
		},
		'pas3':{
			'nume':'Numele trebuie completat',
			'telefon':'Telefonul trebuie completat',
			'email':'Introduceti o adresa de e-mail valida',
			'comentariu':'Comentariul trebuie completat'
		},
		'contact':{
			'nume':'Numele trebuie completat',
			'telefon':'Telefonul trebuie completat',
			'email':'Introduceti o adresa de e-mail valida',
			'comentariu':'Comentariul trebuie completat'
		},
		'android':{
			'android_nume':'Numele trebuie completat',
			'android_telefon':'Telefonul trebuie completat',
			'android_email':'Introduceti o adresa de e-mail valida',
			'android_comentariu':'Comentariul trebuie completat'
		},
		'agentie_web':{
			'publicitate_nume':'Numele trebuie completat',
			'publicitate_telefon':'Telefonul trebuie completat',
			'publicitate_email':'Introduceti o adresa de e-mail valida',
			'publicitate_comentariu':'Comentariul trebuie completat'
		},
		'agentie_publicitate':{
			'publicitate_nume':'Numele trebuie completat',
			'publicitate_telefon':'Telefonul trebuie completat',
			'publicitate_email':'Introduceti o adresa de e-mail valida',
			'publicitate_comentariu':'Comentariul trebuie completat'
		},
		'publisher':{
			'publisher_nume':'Numele trebuie completat',
			'publisher_telefon':'Telefonul trebuie completat',
			'publisher_email':'Introduceti o adresa de e-mail valida',
			'publisher_comentariu':'Comentariul trebuie completat'
		}

	}
	for(var i = 0; i < tipuri.length; i++){
		for(camp in campuri[tipuri[i]]){
			if(!document.getElementById(camp).value){
				erori.push(campuri[tipuri[i]][camp]);
			}
			else if(camp=='email'){
				var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;
				if(reg.test(document.getElementById(camp).value) == false) {
					erori.push(campuri[tipuri[i]][camp]);
				}
			}
		}
	}
	var div = document.getElementById(eroriDiv);
	div.innerHTML='';
	if(erori.length){
		for(var i = 0; i < erori.length; i++){
			div.innerHTML=div.innerHTML+erori[i]+'<br>'
		}
		return false;
	}
	else{
		return true;
	}
}
