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 checkSearch(){
	var searchText = document.getElementById('searchText').value;

	if(searchText.length>0){
		document.search.submit();
	}else{
        return false;
    }
}

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


function replaceSubstring(inputString, fromString, toString) {
      var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { 
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { 
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";

      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      }
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } 
   return temp; 
} 

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="+replaceSubstring(escape(username),'+','%2B')
	url=url+"&password="+replaceSubstring(escape(password),'+','%2B')
	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="+replaceSubstring(escape(login),'+','%2B')
    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 validateUserEmail(field){
	var email = document.getElementById(field).value;  
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var url="getemail.php"
	url=url+"?email="+email
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateUserEmail
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)		
}

function stateUserEmail() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{			
		var result=xmlHttp.responseText 
		if(result!='0' || result.length<=0){
			document.getElementById("inf_userEmail").style.color='#b02d32';
			document.getElementById("errorlogin").innerHTML=document.getElementById("errorlogin").innerHTML+'<br />'+xmlHttp.responseText
			document.getElementById("errorlogin").style.color='#b02d32';				
		}
		else{			             
			document.getElementById("inf_userEmail").style.color='#494949';
			document.getElementById("errorlogin").innerHTML="";		
		}
	}
}

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