function diagYesNo(str)
  {
  var r=confirm(str);
  if (r==true)
    {
    return true
    }
  else
    {
    return false
    }
  }   

function validateEmail(inputID,labelID,invalidMsgID)
{
	if (EmptyString(inputID))
	{alert (fill_in + " '" + $("#"+ labelID).val() + "'");
		return false;}
	else if (!emailCheck($("#" + inputID).val()))
	{	alert ($("#"+ invalidMsgID).val());
		return false;
	}
	else
	{   
		document.form1.submit();
		return true;
	}
}

 
function validateForm()
{	var fill_in = document.getElementById("fill_in").value;

	if (EmptyString("firstname"))
		{alert (fill_in + " '" + document.getElementById("name_firstname").value + "'");
		return false;}
	if (EmptyString("lastname"))
		{alert (fill_in + " '" + document.getElementById("name_lastname").value + "'");
		return false;}	
	if (EmptyString("email"))
		{alert (fill_in + " '" + document.getElementById("name_email").value + "'");
		return false;}
	if (!emailCheck(document.getElementById("email").value))
		{alert (document.getElementById("invalidEmail").value);
		return false;}	
	if (EmptyString("password"))
		{alert (fill_in + " '" + document.getElementById("name_password").value + "'");
		return false;}
	if (document.getElementById("password").value.length < 6)
		{alert (document.getElementById("password_length").value);
		return false;}
	if (document.getElementById("password").value != document.getElementById("repeat_password").value)
		{alert (document.getElementById("passwords_dont_match").value);
		return false;}
		
/*	if (EmptyString("repeat_password"))
		{alert (fill_in + " '" + document.getElementById("name_repeat_password").value + "'");
		return false;}
*/		
/*	if (EmptyString("captcha"))
		{alert (fill_in + " '" + document.getElementById("name_captcha").value + "'");
		return false;}	
*/	if (!document.getElementById("agree_to_terms").checked)
		{alert (document.getElementById("agree_to_terms").value);
		return false;}
		
		
	
			

	document.reg_form.submit();
	return true;	
}
function validateContact()
{
	var fill_in = document.getElementById("fill_in").value;
	
		if (EmptyString("c_name"))
		{alert (fill_in + " '" + document.getElementById("name_c_name").value + "'");
		 return false;}
		if (EmptyString("c_email"))
		{alert (fill_in + " '" + document.getElementById("name_c_email").value + "'");
		 return false;}
		 if (!emailCheck(document.getElementById("c_email").value))
		{alert (document.getElementById("invalidEmail").value);
		return false;}	
		if (EmptyString("c_message"))
		{alert (fill_in + " '" + document.getElementById("name_c_message").value + "'");
		 return false;}
		if (EmptyString("captcha"))
		{document.getElementById('wrong_captcha').innerHTML = document.getElementById('c_wrong_captcha').value;
		return false;}
		
	document.contact_form.submit();
	return true;	
}
//**************
function emailCheck(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	

	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}
	if (str.indexOf(" ")!=-1){return false;}
	return true;
}

function EmptyString(objectName){
	if(document.getElementById(objectName).value.replace(/\s/g,"") == "") 
		{return true;} 
	else 
		{return false;}
}
function displayError(err_message){

	document.getElementById('notification_area').innerHTML = err_message + "</br>";
}
function displayRegSuccessfull(){

	window.location='index.php?ref=mail_sent.php';
}
function displayWrongCaptcha(){
	document.getElementById('wrong_captcha').innerHTML = document.getElementById('c_wrong_captcha').value ;
	}

/*function validateShippingAddress(){
	//if (EmptyString("address"))
	if ((EmptyString("in_name")) || (EmptyString("in_lname")) || (EmptyString("in_city")) || (EmptyString("in_post_code")) || (EmptyString("in_street")) || (EmptyString("in_number")))
		{	alert(document.getElementById("insert_shipping_address").value)
			return false}

	document.shipping_address_form.submit();
	return true;	

}*/

function vaidateShippingID()
{
	if (EmptyString("shipping_cost_id"))
	{
		alert(document.getElementById("insert_invalid_id").value);
		return false;
	}
	
	DisableFields(false);
	//window.alert("Se e ok, idemo dalje!");
	document.shipping_address_form.submit();
	return true;	
}

function DisableFields(flag)
{
	if (flag==true)
		f_color='#767676';
	else
		f_color='#002B4E';
		
	document.shipping_address_form.in_name.disabled=flag;
	document.shipping_address_form.in_lname.disabled=flag;
	document.shipping_address_form.in_city.disabled=flag;
	document.shipping_address_form.in_post_code.disabled=flag;
	document.shipping_address_form.in_street.disabled=flag;
	document.shipping_address_form.in_number.disabled=flag;	
	document.shipping_address_form.in_note.disabled=flag;
	document.shipping_address_form.in_phone.disabled=flag;
	
	document.shipping_address_form.in_name.style.color=f_color;
	document.shipping_address_form.in_lname.style.color=f_color;
	document.shipping_address_form.in_city.style.color=f_color;
	document.shipping_address_form.in_post_code.style.color=f_color;
	document.shipping_address_form.in_street.style.color=f_color;
	document.shipping_address_form.in_number.style.color=f_color;
	document.shipping_address_form.in_note.style.color=f_color;
	document.shipping_address_form.in_phone.style.color=f_color;
}

function backAtShippingAddress()
{
	DisableFields(false);
	//document.getElementById("outgoing_shipping_cost").style.display="none";	
	document.getElementById("ShippingCostTable").style.display="none";
	document.getElementById("opt_panel").style.display="";
	document.getElementById("opt_panel_final").style.display="none";
	if (document.getElementById("curr_addr_form").style.display=="block")
	{
		document.getElementById("use_current").disabled=false;
		document.getElementById("enter_new_addr").disabled=false;
	}
}

function validateShippingAddress(){
	if ((EmptyString("in_name")) || (EmptyString("in_lname")) || (EmptyString("in_city")) || (EmptyString("in_post_code")) || (EmptyString("in_street")) || (EmptyString("in_number")) || (EmptyString("in_phone")))
		{
			alert(document.getElementById("insert_shipping_address").value);
			return false;
		}

	var skopje=false;	
	var i=0;	
	
	while ((skopje==false) && (i<document.getElementById("n_sk").value))
	{
		if (document.getElementById("n_sk"+i).value==document.getElementById("in_city").value.toUpperCase()) { skopje=true; }
		i++;
	}

	DisableFields(true);
	//document.getElementById("get_shipping_cost_btn").style.visibility="hidden";
	document.getElementById("opt_panel").style.display="none";
	
	if (document.getElementById("order_total_cost").value<1500)
	{
		var price=0;
		var id=11300;
		if (skopje==true) 
		{		
			price=document.getElementById("shipping_price0").value;
		}
		else 
		{
			id=32219;
			price=document.getElementById("shipping_price1").value;
		}
	
		//document.getElementById("back_btn").style.visibility="visible";
		//document.getElementById("submit_btn").style.visibility="visible";
		document.getElementById("opt_panel_final").style.display="block";
		document.getElementById("shipping_cost_id").value=id;
		//document.getElementById("outgoing_shipping_cost").style.display="block";
		document.getElementById("ShippingCostTable").style.display="";
		document.getElementById("outgoing_shipping_cost").value=document.getElementById("shipping_cost_text").value + " " + price + " MKD";
	}
	else
	{
		DisableFields(false);
		//window.alert("ne se naplakja isporaka, idemo dalje!")
		document.shipping_address_form.submit();
	}
	
	return true;	

}

function backToCurrAddrForm(flag)
{
	if (flag==true)
	{
		document.getElementById("curr_addr_form").style.display="block";
		document.getElementById("new_addr_form").style.display="none";
	}
	else
	{
		document.getElementById("curr_addr_form").style.display="none";
		document.getElementById("new_addr_form").style.display="block";
	}
	document.getElementById("use_current").disabled=!flag;
	document.getElementById("enter_new_addr").disabled=!flag;
	
}

function useCurrentAddress(flag)
{
	//window.alert(flag);
	if (flag==true)
	{
		//window.alert("Se e ok, idemo dalje!");
		document.getElementById("in_name").value=document.getElementById("curr_name").value;
		document.getElementById("in_lname").value=document.getElementById("curr_lname").value;
		document.getElementById("in_street").value=document.getElementById("curr_street").value;
		document.getElementById("in_number").value=document.getElementById("curr_number").value;
		document.getElementById("in_city").value=document.getElementById("curr_city").value;
		document.getElementById("in_post_code").value=document.getElementById("curr_post_code").value;
		document.getElementById("in_phone").value=document.getElementById("curr_phone").value;		
		document.getElementById("use_current").disabled=true;
		document.getElementById("enter_new_addr").disabled=true;
		//document.getElementById("enter_new_addr").disabled=true;
		validateShippingAddress();
	}
	else
	{
		backToCurrAddrForm(false);
	}
		
	//return true;
}

function sendContactMail()
{
	if (!emailCheck(document.getElementById("c_email").value))
		{alert (document.getElementById("invalidEmail").value);
		return false;}	
	document.contact_form.submit();
}


function resetPassword(email)
{
	var ajaxRequest;
	var fill_in = document.getElementById("fill_in").value;
	
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
			window.location='index.php?ref=reset_mail_sent.php';
			}
		};
		
		if (EmptyString("email"))
		{alert (fill_in + " '" + document.getElementById("name_email").value + "'");
		return false;}
		if (!emailCheck(document.getElementById("email").value))
		{alert (document.getElementById("invalidEmail").value);
		return false;}	
		
		ajaxRequest.open("GET","reset_password.php?email=" + email, true);
		ajaxRequest.send(null); 
}

function updatePassword1(reg_code) {
	var ajaxRequest;
	var fill_in = document.getElementById("fill_in").value;
	
	try {ajaxRequest = new XMLHttpRequest();}
		catch (e)
			{try {ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");}
				catch (e)
					{try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");}
						catch (e)
							{alert("Вашиот пребарувач нe поддржува Ajax!!");
							return false;}
					}
			}

		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
			window.location='index.php?ref=reset_mail_sent.php';
			}
		};


		if (EmptyString("password"))
			{alert (fill_in + " '" + document.getElementById("name_password").value + "'");
			return false;}
		if (document.getElementById("password").value.length < 6)
			{alert (document.getElementById("password_length").value);
			return false;}
		if (document.getElementById("password").value != document.getElementById("repeat_password").value)
			{alert (document.getElementById("passwords_dont_match").value);
			return false;}
		
		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
			window.location='index.php?ref=home.php';
			}
		};

		ajaxRequest.open("GET","update_password.php?reg_code=" + reg_code + "password=" + document.getElementById("password").value, true);
		ajaxRequest.send(null); 

}
function updatePassword() {
	var fill_in = document.getElementById("fill_in").value;
	
		if (EmptyString("password"))
			{alert (fill_in + " '" + document.getElementById("name_password").value + "'");
			return false;}
		if (document.getElementById("password").value.length < 6)
			{alert (document.getElementById("password_length").value);
			return false;}
		if (document.getElementById("password").value != document.getElementById("repeat_password").value)
			{alert (document.getElementById("passwords_dont_match").value);
			return false;}
		
		document.change_pass.submit();
		return true;

}

function checkbox_click()
{
	//window.alert('vleze');
	//<a href='sendOrder.php' id='submit_link'>
		
		if (document.getElementById('payment_after_delivery').checked==true)
		{
			//window.alert('true');
			document.getElementById('submit_link').action='sendOrder.php?payment=0';
		}
		else
		{
			//window.alert('false');
			document.getElementById('submit_link').action='sendOrder.php?payment=1';
		}
		return true;


}
//index.php
//used in Advanced Search [dispalyCategoryProducts.php]
//toggle na kategoriite
 
function toggle(elem,showCategories,hideCategories)
{
	if($(elem).hasClass('toggle'))
	{
		$("#blueLine").toggle();
		$(elem).toggleClass("active");
	
		
		$(elem).html(showCategories);
		if($(elem).hasClass('active'))
			$(elem).html(hideCategories);
	}
	$(elem).parent().next("#hide").toggle();
	//$(elem).parent().next("#hide").toggle();
} 	
//onclick na kopceto "?????"
function search_submit()
{		
	var search_type = $('input[name=search_type]:checked').val();
	var search_str = document.getElementById("search_str");
	var search_getForm = document.search_get;
	search_getForm.method = "post" ;
	search_getForm.action = "index.php?advsearch=1&search_str=" + search_str.value + "&search_type="+ search_type;
	document.search_get.submit();
}
//onclick na parent kategorija (so level = 0)
function parentChecked(parent)
{
	var b = document.getElementById("cat_id");
	//var parent = event.target || event.srcElement;
	var a = document.getElementsByName(parent.id);
	for(var i=0; i < a.length ; i++)
	{
		var tmp = document.getElementsByName(parent.id)[i];
		tmp.checked=parent.checked;
		
		b.value = b.value.replace("|" + tmp.id + "|","");
	
		if (tmp.checked)
		{								
			b.value = b.value+ "|" + tmp.id + "|";
		}
		else
		{
			b.value = b.value.replace("|" + tmp.id + "|","");
		}
				
	}
	if($('#categories input:checkbox:checked').length == $('#categories input:checkbox').length)
	{
		
		$('#check_all').attr('checked',true);
	}
	else
	{
		$('#check_all').attr('checked',false);
	}
}
//onclick na child kategorija (so level = 1)
function childChecked(child)
{
	var b = document.getElementById("cat_id");
	//var child = event.target || event.srcElement;
		
	var tmp = document.getElementById(child.id);
		
	if (tmp.checked)
	{								
		b.value = b.value + "|" + tmp.id + "|";
	}
	else
	{
		b.value = b.value.replace("|" + tmp.id + "|","");
	}
	
	if($('#categories input:checkbox:checked').length == $('#categories input:checkbox').length)
	{
		
		$('#check_all').attr('checked',true);
	}
	else
	{
		$('#check_all').attr('checked',false);
	}
}

//onclick na check all 
function checkAll()
{	
	$('#cat_id').val(null);
	$('#check_all').checked = true;
	var b = "";
	$('#categories input:checkbox').each(function(){
				this.checked = true;
				b = b + "|" + this.id + "|";
		}); 
	
	$('#cat_id').val(b);

}
function uncheckAll()
{
	$('#cat_id').val(null);
	$('#check_all').checked = false;
	$('#categories input:checkbox').each(function(){
		this.checked = false;
		$('#cat_id').value = null;
	} );
}

function check_uncheck()
{
	 if($('#check_all').is(':checked'))
			checkAll();
	 else
			uncheckAll();
}

function validateJob()
	{    
	    var fill_in = $("#fill_in").val();//document.getElementById("fill_in").value;
		var phoneFilter = /^[\+0-9](\d+(-|\s)?\d+)*$/;
		//var emailFilter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		
		if (EmptyString("name"))
		{alert (fill_in + " '" + $("label[for='name']").text() + "'");
		 return false;}
		else if ($("#name").val().length < 4)
		{alert ($("#nameValidMsg").val());
		return false;}
		if (EmptyString("address"))
		{alert (fill_in + " '" + $("label[for='address']").text() + "'");
		 return false;}
		else if ($("#address").val().length < 6)
		{alert ($("#addressValidMsg").val());
		return false;}
		if (EmptyString("phone"))
		{alert (fill_in + " '" + $("label[for='phone']").text() + "'");
		 return false;}
		else if (!phoneFilter.test($("#phone").val()))
		{alert ($("#phoneValidMsg").val());
		 return false;}
		
		if (EmptyString("email2"))
		{alert (fill_in + " '" + $("label[for='email2']").text() + "'");
		 return false;}
		else if (!emailCheck($("#email2").val()))
		{alert ($("#emailValidMsg").val());
		return false;}	
		if (EmptyString("captcha"))
		{$('#wrong_captcha').text($('#wrong_captchaHidden').val());
		return false;}
		 
	document.job_form.submit();
	return true;	
}

/* sidePolicy.php */
function policy(elem){
	var elemid = elem.id;
	var arr = [ "terms", "shipping", "privacy" ];
	
	$.each(arr, function() {
		if(this != elemid)
		{	$("tr#" + this).hide();
			$("li#" + this).removeClass("sidePolicesHover");}
		else
		{  	$("tr#" + this).show();
			
			$("li#" + this).addClass("sidePolicesHover");}
	
	});
	
	//$(elem).unbind('onmouseover').unbind('onmouseout');
}

/* scroll-to-Top function */
function scrollToTop() { // run this code on page load (AKA DOM load)
		 
			/* set variables locally for increased performance */
			var scroll_timer;
			var displayed = false;
			var $message = $('#message a');
			var $window = $(window);
			var top = $(document.body).children(0).position().top;
		 	
			$message.hover(function() {
				$(containerHoverIDHash, this).stop().animate({
					'opacity': 1
				}, 600, 'linear');
			}, function() { 
				$(containerHoverIDHash, this).stop().animate({
					'opacity': 0
				}, 700, 'linear');
			});
			/* react to scroll event on window */
			$window.scroll(function () {
				window.clearTimeout(scroll_timer);
				scroll_timer = window.setTimeout(function () { // use a timer for performance
					if($window.scrollTop() <= top) // hide if at the top of the page
					{
						displayed = false;
						$message.fadeOut(500);
					}
					else if(displayed == false) // show if scrolling down
					{
						displayed = true;
						$message.stop(true, true).show().click(function () { $message.fadeOut(500); });
					}
				}, 100);
			});
			
}

