function change_manufacturer(manufacturer) {
    document.getElementById('products').options.length = 0;

    newoption = document.createElement('option');
    newoption.value = 0;
    newoption.appendChild(document.createTextNode('Alegeti un produs...'));
    document.getElementById('products').appendChild(newoption);

    for(var id in products) {
        if((manufacturer == 0) || (products[id][0] == manufacturer)) {
            newoption = document.createElement('option');
            newoption.value = id;

            if(manufacturer == 0) {
                newoption.appendChild(document.createTextNode(manufacturers[products[id][0]] + ' ' + products[id][1]));
            }
            else {
                newoption.appendChild(document.createTextNode(products[id][1]));
            }

            document.getElementById('products').appendChild(newoption);
        }
    }
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 8 && (charCode > 31 && (charCode < 48 || charCode > 57)) ) {
        return false;
    }
    return true;
}

function isAlphaKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (!((charCode >=65 && charCode <= 90) || (charCode >=97 && charCode <= 122) || (charCode == 32) || (charCode == 8)  || (charCode == 45) )) {
        return false;
    }
    return true;
}

function checkValid(id, msg, regula, valoare) {
    var prefix = "* ";
    var suffix = "\n";

    switch(regula) {
        case 'required':
                        if (document.getElementById(id).value=="") {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'select':
                        if (document.getElementById(id).options[document.getElementById(id).selectedIndex].value=="") {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'numeric':
                        var validch = "0123456789.-";
                        var isNumber=true;
                        var ch;
                        var val = document.getElementById(id).value;
                        for (i=0; i<val.length && isNumber == true; i++) {
                            ch = val.charAt(i);
                            if (validch.indexOf(ch) == -1) {
                                return prefix+msg+suffix;
                            }
                        }
                        break;
        case 'email':
                        var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
                        if (!filter.test(document.getElementById(id).value)) {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'integer':
                        var validch = "0123456789";
                        var isNumber=true;
                        var ch;
                        var val = document.getElementById(id).value;
                        for (i=0; i<val.length && isNumber == true; i++) {
                            ch = val.charAt(i);
                            if (validch.indexOf(ch) == -1) {
                                return prefix+msg+suffix;
                            }
                        }
                        break;
        case 'limitpercent':
                        if (document.getElementById(id).value<0 || document.getElementById(id).value>100) {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'notzero':
                        if (document.getElementById(id).value==0) {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'date':
                        msg = isDate(document.getElementById(id).value);
                        if (msg != "0") {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'lessthan':
                        if (document.getElementById(id).value > valoare) {
                            return prefix+msg+suffix;
                        }
                        break;
        case 'equals':
                if (document.getElementById(id).value != valoare) {
                    return prefix+msg+suffix;
                }
                break;
    }
    return "";
}

//validari
function validEmail(field) {
     var str = field; // email string
     var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
     var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
     if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
       return true;
    }
    return false;
}

function checkSearch(f)
{
	f.searchquery.value = f.searchquery.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	if (f.searchquery.value=="")
	{
		f.searchquery.focus();
		alert("Please insert searched term!");
		return false;
	}
	document.forms['cautare_frm'].searchquery.value=encodeURIComponent(document.getElementById('cautare_frm').searchquery.value);
	return true;
}

function Register_Newsletter(f)
{
	if (f.newsemail.value=="")
	{
		f.newsemail.focus();
		alert("Please insert e-mail address!");
		return false;
	}
	if (!validEmail(f.newsemail.value))
	{
		f.newsemail.focus();
		alert("Please insert a valid e-mail address!");
		return false;
	}
	return true;
}

function checkInteract(f)
{
	if (f.q.value=="")
	{
		f.q.focus();
		alert("Please insert your question!");
		return false;
	}
	return true;
}

function checkIAnswer(f)
{
	if (f.a.value=="")
	{
		f.a.focus();
		alert("Please insert your answer!");
		return false;
	}
	return true;
}

function checkYourVoice(f)
{
	if (f.comm.value=="")
	{
		f.comm.focus();
		alert("Please insert your voice!");
		return false;
	}
	return true;
}

function checkReview(f)
{
	msg="";
	if (f.customer_comment.value=="")
	{
		f.customer_comment.focus();
		msg+="Please insert your review!\n";
	}
	if (f.customer_name.value=="")
	{
		if (msg=="") f.customer_comment.focus();
		msg+="Please insert your name!\n";
	}
	if (msg!="")
	{
		alert("You have the following errors:\n"+msg);
		return false;
	}
	return true;
}

function checkLogin(f)
{
	msg="";
	if (f.email.value=="")
	{
		f.email.focus();
		msg+="Please insert your email address!\n";
	}
	else if (!validEmail(f.email.value))
	{
		f.email.focus();
		alert("Please insert a valid e-mail address!");
		return false;
	}
	if (f.password.value=="")
	{
		if (msg=="") f.password.focus();
		msg+="Please insert your password!\n";
	}
	if (msg!="")
	{
		alert("You have the following errors:\n"+msg);
		return false;
	}
	return true;
}

function checkAccount(f)
{
	msg="";
	if (f.firstname.value=="")
	{
		if (msg=="") f.firstname.focus();
		msg+="Please insert firstname!\n";
	}
	if (f.lastname.value=="")
	{
		if (msg=="") f.lastname.focus();
		msg+="Please insert lastname!\n";
	}
	if (f.email.value=="")
	{
		if (msg=="") f.email.focus();
		msg+="Please insert email address!\n";
	}
	else if (!validEmail(f.email.value))
	{
		if (msg=="") f.email.focus();
		alert("Please insert a valid e-mail address!");
		return false;
	}
	if (msg!="")
	{
		alert("You have the following errors:\n"+msg);
		return false;
	}
	return true;
}

function checkReg(f)
{
	if (f.terms.checked==false)
	{
		alert("You must agree with terms and conditions!");
		return false;
	}
	
	msg="";
	if (f.firstname.value=="")
	{
		if (msg=="") f.firstname.focus();
		msg+="Please insert firstname!\n";
	}
	if (f.lastname.value=="")
	{
		if (msg=="") f.lastname.focus();
		msg+="Please insert lastname!\n";
	}
	if (f.nemail.value=="")
	{
		if (msg=="") f.nemail.focus();
		msg+="Please insert email address!\n";
	}
	else if (!validEmail(f.nemail.value))
	{
		if (msg=="") f.nemail.focus();
		alert("Please insert a valid e-mail address!");
		return false;
	}
	
	if (f.regpassword.value=="")
	{
		if (msg=="") f.regpassword.focus();
		msg+="Please insert your password!\n";
	}
	else if (f.regpassword.value.length<6)
	{
		if (msg=="") f.regpassword.focus();
		msg+="You password must have at least 6 characters\n";
	}
	
	if (f.reregpassword.value=="")
	{
		if (msg=="") f.regpassword.focus();
		msg+="Please confirm your password!\n";
	}
	else if (f.regpassword.value!=f.reregpassword.value)
	{
		if (msg=="") f.regpassword.focus();
		msg+="The re-typed password must be the same with the initial password!\n";
	}
	
	
	if (msg!="")
	{
		alert("You have the following errors:\n"+msg);
		return false;
	}
	return true;
}

function checkPassword(f)
{
	msg="";
	if (f.password.value=="")
	{
		if (msg=="") f.password.focus();
		msg+="Please insert your password!\n";
	}
		
	if (f.newpassword.value=="")
	{
		if (msg=="") f.newpassword.focus();
		msg+="Please confirm your password!\n";
	}
	else if (f.newpassword2.value!=f.newpassword.value)
	{
		if (msg=="") f.newpassword2.focus();
		msg+="The re-typed password must be the same with the initial password!\n";
	}
	
	if (msg!="")
	{
		alert("You have the following errors:\n"+msg);
		return false;
	}
	return true;
}

function checkForgot(f)
{
	if (f.email.value=="")
	{
		f.email.focus();
		alert("Please insert e-mail address!");
		return false;
	}
	if (!validEmail(f.email.value))
	{
		f.email.focus();
		alert("Please insert a valid e-mail address!");
		return false;
	}
	return true;
}

/* popup windows */
function popup(url, winname, w, h) {
    compensatew = 0;
    compensateh = 0;

    if ($.browser.msie && parseInt($.browser.version) < 7) {
        compensatew = 10;
        compensateh = 30;
    }

    w = w - compensatew;
    h = h - compensateh;

    l = (screen.availWidth - w - compensatew) / 2;
    t = (screen.availHeight - h - compensateh) / 2;

    if (l < 0) {
        l = 0;
    }
    if (t < 0) {
        t = 0;
    }
    if (w > screen.availWidth) {
        w = screen.availWidth;
    }
    if (h > screen.availHeight) {
        h = screen.availHeight;
    }
    window.open(url, winname, 'top='+t+',left='+l+',width='+w+',height='+h+',scrollbars=yes,menubar=no,toolbar=no,location=no,resizable=yes,status=no');
}

function compare(id) {
    w = screen.availWidth;
    h = screen.availHeight;
    popup('/compara/'+id, 'compareWin', w, h);
}

function comment(id) {
    w = 770;
    h = 470;
    popup('/comenteaza/'+id, 'commentWin', w, h);
}

function recommend(id, theme, color, color2) {
    w = 770;
    h = 380;
    popup('/recomanda/'+id+'&theme='+theme+'&color='+color+'&color2='+color2+'/', 'recommendWin', w, h);
}

function changethumb(id, file) {
    $('#mainimg').attr('src', '/content/products/images/'+parseInt(id)+'/normal/'+file);
    $('#mainimga').attr('href', '/content/products/images/'+parseInt(id)+'/full/'+file);
}

function st(tab) {
    $(".tabmenu ul li a").removeClass("tsel");
    $("#sta"+tab).addClass("tsel");

    $(".stc").hide();
    $("#stc"+tab).show();
}


/* AJAX Stuff */
function createRequestObject()
{
    var request;
    var browser=navigator.appName;
    if(browser=="Microsoft Internet Explorer")
        request=new ActiveXObject("Microsoft.XMLHTTP");
    else
        request=new XMLHttpRequest();
    return request;
}

var http=createRequestObject();

function afterRecEmail_1220622(){
    if(http.readyState==4)  {
        alert(http.responseText);
    }
}

//filtre
var filters;
var man;
function toggle(targetId) {
    if (document.getElementById) {
        target = document.getElementById(targetId);
        if (target.style.display == "block") {
            target.style.display = "none";
        }
        else {
            target.style.display = "block";
        }
    }
}

function cancelFilters(catID, section){

    if(section == undefined || section == null){
        $(".filtercheck").each(function(){
            this.checked = false;
        });
        $("#cancelTab").hide();
        buildFiltersList(catID,pagenoi,1,"");
		$("#filterAll"+section).attr({checked:true});
    } else {
        $("#filterBox" + section + " input.filtercheck").attr({checked:false});
        buildFiltersList(catID,pagenoi,1,"","");
		$("#filterAll2").attr({checked:true});
	}

	
}

function buildFiltersList(catID,pagei,pagep,manURL,tag){

	$("#loadingTab").show();
	var filterString = '';
	var manString    = '';var manID=0;
    var hashData = new Array();
	$(".filtercheck:checked").each(function(){
		if($(this).attr('name') != 'man'){
			filterString +=  $(this).val();
		} else if($(this).attr('name') == 'man'){
			manString +=  $(this).val() + "-";
			manID = $(this).val();
		} 
        hashData.push($(this).parent().attr('id').substr(10));
	});
    	
    if (pagei) hashData.push("pagei-"+pagei);	
	if (pagep) hashData.push("pagep-"+pagep);	

	setHashStuff(hashData);
	if(manURL != '' && manURL != 'undefined' && manURL != undefined  && manURL != 0) manString = manURL;

	if ($('#articlesArea')) paginateArticles(catID, pagei, tag, manID);
	if (tag=="resources") tag="";
	if ($('#productsArea')) filterProducts(filterString, catID, pagep, manString, tag);   
}

function filterProducts(filterString,catID,page,manString, tag){
	
	if (tag.indexOf("search|")==-1)
		$.post('/index.php?sa=viewcategory_ajax&catid='+catID+'&filterString='+filterString+'&page='+page+'&manid='+manString+'&tag='+tag,
			function(data){
				if ($('#productsArea')) $('#productsArea').html(data);
				$("#loadingTab").hide();
	    });
	else
		$.post('/index.php?sa=search_ajax&catid='+catID+'&page='+page+'&tag='+tag+'&show=products',
			function(data){
				if ($('#productsArea')) $('#productsArea').html(data);
				$("#loadingTab").hide();
	    }); 
    
}

function paginateArticles(catID,page,tag,manID){
	if (tag.indexOf("search|")==-1)
		$.post('/index.php?sa=viewarticles_ajax&catid='+catID+'&page='+page+'&tag='+tag+'&manid='+manID,
			function(data){
				$('#articlesArea').html(data);
				$("#loadingTab").hide();
	    });
	else //alert('/index.php?sa=search_ajax&catid='+catID+'&page='+page+'&tag='+tag+'&show=articles');
		$.post('/index.php?sa=search_ajax&catid='+catID+'&page='+page+'&tag='+tag+'&show=articles',
			function(data){
				$('#articlesArea').html(data);
				$("#loadingTab").hide();
	    });	      
}

function goToPage(page,catID,tag){
	pagenop=page;
	buildFiltersList(catID,pagenoi,page,'',tag);
    window.scroll(0,0);
}
function goToPageA(page,catID,tag){
	pagenoi=page;
	buildFiltersList(catID,page,pagenop,'',tag);
	//paginateArticles(catID, page,tag);
    window.scroll(0,0);
}
function goToPageI(page,catID){
	paginateInteract(catID,page)
    window.scroll(0,0);
}

function setHashStuff(hashArr){
    
    var str = '';

    $.each(hashArr, function(hindex,hvalue){
        str +=  hvalue + "|";
    });
    if(str.length > 0){
    	document.location.href = "#" + str;
    }
   
}

function selectURLMan(manid,cat,p){
	$(".filtercheck[value="+manid+"]").attr({checked:true});
	$("#cancelTab").show();
}

function checkHistory(cat,pi,pp,tag){
    var s = document.location.hash.substr(1);
    if(s){
        var boxes = s.split("|");
        $.each(boxes, function(hindex,hvalue){
            if(hvalue){
            	ff=hvalue.split("-");
                $('#filterAll'+ff[0]).attr("checked",false);
                $('#filterLine' + hvalue+ ' .filtercheck').attr("checked",true);
            }
        });
    }
    buildFiltersList(cat,pi,pp,0,tag);  
}

function paginateInteract(cat,page)
{
	$("#loadingTab").show();
    var hashData = new Array();
    	
    hashData.push("page-"+page);	

	setHashStuff(hashData);
	  
	if ($('#articlesArea')) 
	{
		$.post('/index.php?sa=interact_ajax&catid='+cat+'&page='+page,
			function(data){
				$('#articlesArea').html(data);
				$("#loadingTab").hide();
	    });
	}

}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

/**
* Click pe tot div-ul la Vendors
*/
function __click_on_Vendor(VendorDiv) {
	href = $("a", VendorDiv).attr("href");
	window.location = href;
}
