var xmlHttp

function bookmark_us(title,url){

	if (window.sidebar) // firefox
	    window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
	}
	else if(document.all)// ie
	    window.external.AddFavorite(url, title);
}

function search_input_click(el,text){
	if(el.value==text){
		el.value='';
	}
}

function search_input_blur(el,text){
	if(el.value.length==0){
		el.value=text;
	}
}


function init() { 
	if (!document.getElementById) return false; 
	var f = document.getElementById('searchText'); 
		if(f){
			f.setAttribute("autocomplete", "off"); 
		}
} 

function checkSearch(){
	var searchText = document.getElementById('searchText').value;

	if(searchText.length>0){
		document.search.submit();
	}
}

function showDiv(id){
	var iddiv="div"+id;
	var div=document.getElementById(iddiv)
	div.style.display='block';

	var idcat="cat"+id;
	var div=document.getElementById(idcat)
	div.className='leftmenuover';
}

function hideDiv(id){
	var iddiv="div"+id;
	var div=document.getElementById(iddiv)
	div.style.display='none';

	var idcat="cat"+id;
	var div=document.getElementById(idcat)
	div.className='leftmenu';
}

// --------------------------------------------
//
// functions scrol div
// 
// --------------------------------------------

var divwidth=210;
var divLeft=0;

function moveleft(){

	var divID = document.getElementById("picDiv");
	var divWidth=divID.clientWidth;

	divLeft=divLeft-2;
	divID.style.left=divLeft+'px';		
		
	if((divWidth+divLeft)>=200) leftMove=setTimeout("moveleft()",1);
}

function moveright(){

	var divID = document.getElementById("picDiv");
	
	divLeft=divLeft+2;
	divID.style.left=divLeft+'px';		
	
	if(divLeft<0) rightMove=setTimeout("moveright()",1);
	
}
function showNormalPic(photoID,ind,widthPic,heightPic,table,ID,pic){
	var text="<span onclick=showGallery("+photoID+","+ind+","+widthPic+","+heightPic+",'"+table+"',"+ID+",0)><img src='"+pic+"'></span>";
	var divID = document.getElementById("productpic").innerHTML=text;	
}

// --------------------------------------------
//
// functions for login form
// 
// --------------------------------------------

function showDivLogin(divHeight){
		
	var divShow = document.getElementById("loginform");
	
	divHeight=divHeight+5;
	divShow.style.display='block';
	divShow.style.height=divHeight+'px';		
	
	if(divHeight<100) setTimeout("showDivLogin("+divHeight+")",10);
}

function hideDivLogin(divHeight){
		
	var divHide = document.getElementById("loginform");
	
	divHeight=divHeight-5;
	divHide.style.height=divHeight+'px';		
	
	if(divHeight>0) {
		setTimeout("hideDivLogin("+divHeight+")",10);
	}
	else{
		divHide.style.display='none';
		document.getElementById("user").value="";
		document.getElementById("pass").value="";
		document.getElementById("error").innerHTML="";
		
	}return false ;
}

function checkLoginDiv(){
	var username = document.getElementById('user').value; 
	var password = document.getElementById('pass').value; 


	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	var url="getusers.php"
	url=url+"?username="+username
	url=url+"&password="+password
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateLoginDiv 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)		
	return false;
}

function stateLoginDiv()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 	result=xmlHttp.responseText
 	if(result == "0"){
		document.logindiv.submit();
	}
 	else{ 		
		document.getElementById("error").innerHTML=result; 	
		
 	} 	 	 	 	 	 	 	 	
 }
}

function checkLoginFrm(){
	var username = document.getElementById('userfrm').value; 
	var password = document.getElementById('passfrm').value; 


	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	var url="getusers.php"
	url=url+"?username="+username
	url=url+"&password="+password
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateLoginFrm
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)		
	
	return false;
}

function stateLoginFrm()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 	result=xmlHttp.responseText
 	if(result == "0"){
		document.loginfrm.submit();
	}
 	else{ 		
		document.getElementById("errfrm").innerHTML=result; 	
 	} 	 	 	 	 	 	 	 	
 }
}
//---------------------------------------------
//
//validate shopform
//---------------------------------------------

var invoice = 0;

function toggleInvoice(inv,id)
{
	if (document.getElementById)
	{
		var e = document.getElementById(id);
		if (e)
		{
			if (inv==1)
			{
				e.style.display = "block";
				invoice=1;
			}
			else
			{
				e.style.display = "none";
				invoice =0;
			}
		}
	}
}



// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail  (valfield,msgfield)
{
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  
	if (!email.test(tfld)) {  
		document.getElementById(msgfield).style.color='#b02d32';
		return false;
	}else{
		document.getElementById(msgfield).style.color='#494949';
		return true;
	}  
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}



function validateField(field){

	var inf="inf_"+field;

	if(document.getElementById(field).value.length<=0) {
		document.getElementById(inf).style.color='#b02d32';
		return false;
	}
	else{
		document.getElementById(inf).style.color='#494949';
		return true;
	}
	
}

//--------------------------------------------------------
//	functions validate User login
//--------------------------------------------------------

function validateUserLogin(field){
	var login = document.getElementById(field).value;  
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var url="getlogin.php"
	url=url+"?login="+login
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateUserLogin 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)		
}

function stateUserLogin() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{			
		var result=xmlHttp.responseText 
		if(result!='0' || result.length<=0){
			document.getElementById("inf_userLogin").style.color='#b02d32';
			document.getElementById("errorlogin").innerHTML=xmlHttp.responseText
			document.getElementById("errorlogin").style.color='#b02d32';				
		}
		else{			
			document.getElementById("inf_userLogin").style.color='#494949';
			document.getElementById("errorlogin").innerHTML="";		
			onlyLatin("userLogin");
		}
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

//--------------------------------------------------------
//end function validate login
//--------------------------------------------------------

function validateName(){

        var check = 0;

	//get file name
        var field = document.getElementById("fileupload").value;          
        var lastPathDelimiter = field.lastIndexOf("\\");         
        var fileNameOnly = field.substring(lastPathDelimiter+1);
          
	//chek file name
	for (var i=0; i<fileNameOnly.length; i++){
		if(fileNameOnly.charCodeAt(i)>=32 && fileNameOnly.charCodeAt(i)<=126)
		{
			check=check;	
		}	
		else{
			check=check+1;
		}
	}
	if(check!=0) {
		alert("Your file name contain cyrillic symbol. Please rename file and try again.")
		return false;
	}else{
		return true;
	}
}

function onlyLatin(field){

        var check = 0;

	//get file name
        var name = document.getElementById(field).value;          
          
	//chek file name
	for (var i=0; i<name.length; i++){
		if((name.charCodeAt(i)>=97 && name.charCodeAt(i)<=122) || (name.charCodeAt(i)>=48 && name.charCodeAt(i)<=57))
		{
			check=check;	
		}	
		else{
			check=check+1;
		}
	}
	if(check!=0) {		
		document.getElementById("errorlogin").innerHTML="Your login name contain incorrect symbol.";
		document.getElementById("errorlogin").style.color='#b02d32';
	}
}


function numbersonly(myfield, e, dec)
{
var key;
var keychar;
if (window.event)
 key = window.event.keyCode;
else if (e)
 key = e.which;
else
 return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) || 
 (key==9) || (key==13) || (key==27) )
 return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
 return true;
// decimal point jump
else if (dec && (keychar == "."))
 {
 myfield.form.elements[dec].focus();
 return false;
 }
else
 return false;
}


function doubleonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function sameShipp(ch){
    if(ch.checked==true) {
     document.getElementById('billName').value=document.getElementById('userName').value;
     document.shopform.billName.readOnly = true;   
     document.getElementById('billAddress').value=document.getElementById('addressAddress').value;
     document.shopform.billAddress.readOnly = true;
     document.getElementById('billCity').value=document.getElementById('addressCity').value;    
     document.shopform.billCity.readOnly = true;
     document.getElementById('billZip').value=document.getElementById('addressZip').value;
     document.shopform.billZip.readOnly = true;
     document.getElementById('billRegion').value=document.getElementById('addressRegion').value;
     document.shopform.billRegion.readOnly = true;
     document.getElementById('billCountry').value=document.getElementById('addressCountry').value;
     document.shopform.billCountry.readOnly = true;
    }else{
     document.getElementById('billName').value="";
     document.shopform.billName.readOnly = false;   
     document.getElementById('billAddress').value="";
     document.shopform.billAddress.readOnly = false;
     document.getElementById('billCity').value="";
     document.shopform.billCity.readOnly = false;    
     document.getElementById('billZip').value="";
     document.shopform.billZip.readOnly = false;
     document.getElementById('billRegion').value="";
     document.shopform.billRegion.readOnly = false;
     document.getElementById('billCountry').value="";
     document.shopform.billCountry.readOnly = false;
    }
    
}