var xmlHttp

function changeStock(){    

    var sel = document.getElementById("productSize");
    var d = sel.options[sel.selectedIndex].lang;
    
    var m=d.split("-")
    
    var prStock=document.getElementById('prStock');
    if(prStock){                
        prStock.innerHTML=m[0]
    }    
    document.getElementById('productID').value=m[1];
    document.getElementById('productPrice').innerHTML=m[2];
    
    if(m[3]>0) document.getElementById('productPriceOld').innerHTML=m[3]+''+m[4];
    
    if(m[0]<=0){
        document.getElementById('catr_butt_r').className="button_right_off";
        document.getElementById('cart_butt_cont').className="button_content_off";
        document.getElementById('catr_butt_l').className="button_left_off";
        //document.getElementById('productID').value=0;
    }else{
        document.getElementById('catr_butt_r').className="button_right_c";
        document.getElementById('cart_butt_cont').className="button_content";
        document.getElementById('catr_butt_l').className="button_left";
    }
}

function product_select_size(color,id){

    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url="getproductsize.php"
    url=url+"?color="+color
    url=url+"&id="+id
    url=url+"&sid="+Math.random()    
    xmlHttp.onreadystatechange=stateProductSize 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)        
    return false;
}

function stateProductSize()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
    document.getElementById("prodSize").innerHTML=xmlHttp.responseText;
    changeStock();
 }
}

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();
	}else{
        return false;
    }
}

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';
}


function showNormalPic(photoID,ind,widthPic,heightPic,pic,prodID){
	var text="<span onclick=showGallery("+photoID+","+ind+","+widthPic+","+heightPic+","+prodID+")><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<125) setTimeout("showDivLogin("+divHeight+")",10);
    
    return false;
}

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.frmlogin.submit();              
        location.href = "checkout.php"; 
	}
 	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 simbolonly(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;

else if ((key==34) || (key==39))
   return false;

else
   return true;
}

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 doubleonly2(myfield, e, dec)
{
    var key;
    var keychar;
    
    var x=myfield.value.indexOf(",");
    var y=myfield.value.indexOf(".");
    
    var simb="0123456789.,";
    if(x>-1 || y>-1) simb="0123456789";

    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 ((simb.indexOf(keychar) > -1))
       return true;

    // decimal point jump
    else if (dec && (keychar == "."))
    {
        myfield.form.elements[dec].focus();
        return false;
    }
    else
       return false;
}
