function changeCurrency(name) {
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.event.returnValue = false;
		newloc = window.location.href;
	} else {
		newloc = location.href;
	}
	if(newloc.indexOf("?") > 0) {
		if(newloc.indexOf("currency") > 0) {
			start = newloc.indexOf("currency");
			end = newloc.indexOf("&", start);
			if(end > 0) {
				newurl = newloc.replace(newloc.substring(start, end), "currency=" + name);
			} else {
				newurl = newloc.replace(newloc.substring(start, newloc.length), "currency=" + name);
			}
		} else {
			newurl = newloc + "&" + "currency=" + name;
		}
	} else {
		newurl = newloc + "?" + "currency=" + name;
	}
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.location.href = newurl;
	} else {
		location.href = newurl;
	}
}

function changeCount(name) {
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.event.returnValue = false;
		newloc = window.location.href;
	} else {
		newloc = location.href;
	}
	if(newloc.indexOf("?") > 0) {
		if(newloc.indexOf("elements") > 0) {
			start = newloc.indexOf("elements");
			end = newloc.indexOf("&", start);
			if(end > 0) {
				newurl = newloc.replace(newloc.substring(start, end), "elements=" + name);
			} else {
				newurl = newloc.replace(newloc.substring(start, newloc.length), "elements=" + name);
			}
		} else {
			newurl = newloc + "&" + "elements=" + name;
		}
	} else {
		newurl = newloc + "?" + "elements=" + name;
	}
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.location.href = newurl;
	} else {
		location.href = newurl;
	}
}


function setFilter(filter, name) {
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.event.returnValue = false;
		newloc = window.location.href;
	} else {
		newloc = location.href;
	}
	if(newloc.indexOf("?") > 0) {
		if(newloc.indexOf(filter) > 0) {
			start = newloc.indexOf(filter);
			end = newloc.indexOf("&", start);
			if(end > 0) {
				newurl = newloc.replace(newloc.substring(start, end), filter + "=" + name);
			} else {
				newurl = newloc.replace(newloc.substring(start, newloc.length), filter + "=" + name);
			}
		} else {
			if(newloc.indexOf("#nav_start") > 0) {
				start = newloc.indexOf("#nav_start");
				newurl = newloc.replace(newloc.substring(start, newloc.length), "&" + filter + "=" + name + "#nav_start");
			} else {
				newurl = location + "&" + filter + "=" + name;
			}
		}
	} else {
		newurl = newloc + "?" + filter + "=" + name;
	}
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.location.href = newurl;
	} else {
		location.href = newurl;
	}
}

function setBasicFilter(filter) {
	location.href = "index.php?" + filter;
}

function setLeftFilter(link, filters) {
	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.event.returnValue = false;
		newloc = window.location.href;
	} else {
		newloc = location.href;
	}
	baseUrl = newloc.substring(0, newloc.lastIndexOf("/")+1);
	if(link.length > 0) {
		baseUrl = link;
	}

	params = newloc.substring(newloc.lastIndexOf("/")+1, newloc.length);
	if(filters.indexOf("&") >= 0) {
		filter1 = filters.substring(0, filters.indexOf("="));
		filter1data = filters.substring(filters.indexOf("=")+1, filters.indexOf("&"));
		filter2 = filters.substring(filters.indexOf("&")+1, filters.lastIndexOf("="));
		filter2data = filters.substring(filters.lastIndexOf("=")+1, filters.length);
		multi = 1;
	} else {
		filter1 = filters.substring(0, filters.indexOf("="));
		filter1data = filters.substring(filters.indexOf("=")+1, filters.length);
		multi = 0;
	}
	if(params != "") {
		if(params.indexOf("?") >= 0) {
			finalParams = params.substring(params.indexOf("?")+1, params.length);
//			alert("This is what happens when final params are there");
//			alert(finalParams);
			if(finalParams.indexOf(filter1) >= 0) {
//				alert("Filter was found");
				start = finalParams.indexOf(filter1);
				end = finalParams.indexOf("&", start);
				if(end >= 0) {
//					alert("filter has further &");
					finalParams = finalParams.replace(finalParams.substring(start, end), filter1 + "=" + filter1data);
				} else {
//					alert("filter does not have further &");
//					alert("filter1 data" + filter1data);
					finalParams = finalParams.replace(finalParams.substring(start, finalParams.length), filter1 + "=" + filter1data);
				}
				if(multi != 0) {
//					alert("Filter 2 was found");
					start = finalParams.indexOf(filter2);
					end = finalParams.indexOf("&", start);
					if(end >= 0) {
//						alert("filter 2 has further &");
						finalParams = finalParams.replace(finalParams.substring(start, end), filter2 + "=" + filter2data);
					} else {
//						alert("filter 2 does not have further &");
//						alert("filter 2 = " + filter2);
//						alert(finalParams);
						finalParams = finalParams.replace(finalParams.substring(start, finalParams.length), filter2 + "=" + filter2data);
					}					
				}
			} else {
//				alert("Filter wasn't found, adding to the end of the line");
				finalParams += "&" + filters;
			}
			finalLoc = baseUrl + "index.php" + "?" + finalParams;
		} else {
			finalLoc = baseUrl + "index.php" + "?" + filters;
//			alert("This is what happens when there no params and just .php");
//			alert(finalLoc);
		}
	} else {
		finalLoc = baseUrl + "index.php" + "?" + filters;
//		alert("This is what happens when there are no params");
//		alert(finalLoc);
	}
	if(finalLoc.indexOf("PAGEN_") > 0) {
		end = finalLoc.indexOf("&", finalLoc.indexOf("PAGEN_"));
		if(end > 0) {
			start = finalLoc.indexOf("=", finalLoc.indexOf("PAGEN_"));
			finalLoc = finalLoc.replace(finalLoc.substring(start+1, end), "1");
		} else {
			finalLoc = finalLoc.replace(finalLoc.substring(finalLoc.indexOf("=", finalLoc.indexOf("PAGEN_"))+1, finalLoc.length), "1");
		}
	}

	if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
		window.location.href = finalLoc;
	} else {
		location.href = finalLoc;
	}
}
                      
function setLeftFilter2(filter) {
	if(location.toString().indexOf("detail.php") > 0) {
		astart = location.toString().indexOf("detail.php");
		aend = location.toString().indexOf("&", astart);
		if(aend > 0) {
			newloc = location.toString().replace(location.toString().substring(astart, aend), "index.php");
		} else {
			newloc = location.toString().replace(location.toString().substring(astart, location.toString().length), "index.php");	
		}
	} else {
		newloc = "";
	}
	if(location.toString().indexOf("index.php") > 0) {
		astart = location.toString().indexOf("index.php");
		aend = location.toString().indexOf("&", astart);
		if(aend > 0) {
			newloc = location.toString().replace(location.toString().substring(astart, aend), "index.php");
		} else {
			newloc = location.toString().replace(location.toString().substring(astart, location.toString().length), "index.php");	
		}
	} else {
		newloc = "";
	}
	if(newloc != "") {
		if(location.pathname.toString().indexOf("index.php") < 0) {
			loc = "index.php";
		} else {
			loc = "";
		}
		if(newloc.toString().indexOf("?") > 0) {
			if(filter.toString().indexOf("&") > 0) {
				filterstart = newloc.toString().indexOf(filter.toString().substring(0, filter.toString().indexOf("=")));
				if(filterstart >= 0) {
					filtermiddle = newloc.toString().indexOf("&", filterstart);
					end = newloc.toString().indexOf("&", filtermiddle+1);
					if(end > 0) {			
						newurl = newloc.toString().replace(newloc.toString().substring(filterstart, end), filter);
					} else {
						newurl = newloc.toString().replace(newloc.toString().substring(filterstart, newloc.toString().length), filter);
					}
					location.href = newurl + loc + "&PAGEN_1=1";
				} else {
					location.href = location + loc + "&" + filter + "&PAGEN_1=1";
				}
			} else {
				filterstart = newloc.toString().indexOf(filter.toString().substring(0, filter.toString().indexOf("=")));
				if(filterstart >= 0) {
					end = newloc.toString().indexOf("&", filterstart);
					if(end > 0) {
						newurl = newloc.toString().replace(newloc.toString().substring(filterstart, end), filter);
					} else {
						newurl = newloc.toString().replace(newloc.toString().substring(filterstart, newloc.toString().length), filter);
					}
				location.href = newurl + loc + "&PAGEN_1=1";
				} else {
					location.href = location + loc + "&" + filter + "&PAGEN_1=1";
				}
			}
		} else {
			location.href = newloc + loc + "?" + filter + "&PAGEN_1=1";
		}
	} else {
		if(location.pathname.toString().indexOf("index.php") < 0) {
			loc = "index.php";
		} else {
			loc = "";
		}
		if(location.toString().indexOf("?") > 0) {
			if(filter.toString().indexOf("&") > 0) {
				filterstart = location.toString().indexOf(filter.toString().substring(0, filter.toString().indexOf("=")));
				if(filterstart >= 0) {
					filtermiddle = location.toString().indexOf("&", filterstart);
					end = location.toString().indexOf("&", filtermiddle+1);
					if(end > 0) {			
						newurl = location.toString().replace(location.toString().substring(filterstart, end), filter);
					} else {
						newurl = location.toString().replace(location.toString().substring(filterstart, location.toString().length), filter);
					}
					location.href = newurl + loc + "&PAGEN_1=1";
				} else {
					location.href = location + loc + "&" + filter + "&PAGEN_1=1";
				}
			} else {
				filterstart = location.toString().indexOf(filter.toString().substring(0, filter.toString().indexOf("=")));
				if(filterstart >= 0) {
					end = location.toString().indexOf("&", filterstart);
					if(end > 0) {
						newurl = location.toString().replace(location.toString().substring(filterstart, end), filter);
					} else {
						newurl = location.toString().replace(location.toString().substring(filterstart, location.toString().length), filter);
					}
				location.href = newurl + loc + "&PAGEN_1=1";
				} else {
					location.href = location + loc + "&" + filter + "&PAGEN_1=1";
				}
			}
		} else {
			location.href =  loc + "?" + filter + "&PAGEN_1=1";
		}
	}
}


function showFilter(id) {
	container = document.getElementById(id);
	if(container.style.display == "block") {
		container.style.display = "none";
	} else {
		container.style.display = "block";
	}
}

function toggleDetails() {
	if(document.getElementById('details').style.display == "none") {
		document.getElementById('details').style.display = "block";
		document.getElementById('arrow').src = "/images/arrow_shown.jpg";
	} else {
		document.getElementById('details').style.display = "none";
		document.getElementById('arrow').src = "/images/arrow_hidden.jpg";
	}
}

function create_object() {
    var http_request = false;
    if(window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html; charset=windows-1251');
        }
    }
    else if(window.ActiveXObject) {
        try { 
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {}
        }
    }
    if(!http_request) {
        alert('Failed to create object of class XMLHTTP');
        return false;
    }
    else { return http_request; }
}
 
function make_request(url, id) {
    http_request = create_object();
    start = url.indexOf("-n-");
    end = url.length;
    if(start > 0) {
	formatid = url.substring(start+3, end);
	url = url.replace(url.substring(start, end), '');
    } else {
	formatid = false;
    }
    http_request.onreadystatechange = function() { paste_response(http_request, id, formatid); };
    http_request.open('GET', url, true);
    http_request.send(null);
}
 
function make_request_del_compare(url, id) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_del_compare(http_request, id); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function paste_response_del_compare(http_request, id) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
		document.getElementById(id).style.display = "none";
		document.getElementById('compare_add_'+id).style.display = "block";
        } else {
            alert('No response from server or another error accured.');
        }
    }
}

function make_basket_request(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_basket(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function paste_response_basket(http_request) {
    document.getElementById('loading').style.display = "block";
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
	    document.getElementById('loading').style.display = "none";
		if(navigator.userAgent.toString().indexOf("MSIE") > 0) {
//			window.event.returnValue = false;
			window.location.reload();
		} else {
			location.reload();
		}
        } else {
            alert('No response from server or another error accured.');
        }
    }
}

function make_request_select(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_select(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function paste_response_select(http_request) {
    document.getElementById('loading').style.display = "block";
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
		response = http_request.responseText.toString();
		id = response.substring(2, response.indexOf("---"));
		values = response.substring(response.indexOf("---")+3, response.length);
		x = document.getElementById(id);
		x.innerHTML = "";
		x.innerHTML = values;
		x.style.display = "block";		
        } else {
            alert('No response from server or another error accured.');
        }
    }
}


function minus_one(id) {
	var init_value = new Number(document.getElementById(id).value);
	new_value = init_value - 1;
	if(new_value <= 0) {
		document.getElementById(id).value = 0;
	} else {
		document.getElementById(id).value = new_value;
	}
}	

function plus_one(id) {
	var init_value = new Number(document.getElementById(id).value);
	new_value = init_value + 1;
	document.getElementById(id).value = new_value;
}

function AddToCompare(url, id, name) {
	var oldurl = location.toString();
	var del = false;
	url = url + '?id=' + id + '&del=' + del;
	make_request_compare(url, id, oldurl, del, name);
}

function DeleteFromCompare(url, id, name) {
	var oldurl = location.toString();
	var del = true;
	url = url + '?id=' + id + '&del=' + del;
	make_request_compare(url, id, oldurl, del, name);
}

function make_request_compare(url, id, oldurl, del, name) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_compare(http_request, oldurl, del, id, name); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function paste_response_compare(http_request, oldurl, del, id, name) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
	if(http_request.responseText == "ok") {
	    if(del == true) {
	       document.getElementById('compare_delete_'+id).style.display = "none";
	       document.getElementById('compare_add_'+id).style.display = "block";
	       document.getElementById(id).style.display="none";
	    } else {
	       document.getElementById('compare_delete_'+id).style.display = "block";
	       document.getElementById('compare_add_'+id).style.display = "none";
	       document.getElementById('compare_ul').innerHTML += "<li id=" + id + "><a href=javascript:void(0) onClick=make_request_del_compare('/content/del_compare.php?id=" + id + "', '" + id + "')>X</a>&nbsp;&nbsp;" + name;
	    }
	} else {
	    alert(http_request.responseText);
	}
        } else {
            alert('No response from server or another error accured.');
        }
    }
}

// 02.05.2011, functions for ajax buy

function base64_encode( data ) {    

    data = escape(data);  
 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';
 
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
 
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
} 

function base64_decode( data ) {   

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';
 
    do {  
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64)      enc += String.fromCharCode(o1);
        else if (h4 == 64) enc += String.fromCharCode(o1, o2);
        else               enc += String.fromCharCode(o1, o2, o3);
    } while (i < data.length);
 
    return unescape(enc);
}
