function xmlhttpPost(strURL, idx, p)
{
	var xmlHttpReq = false;
    var self = this;
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, idx, p);
        }
    }
    self.xmlHttpReq.send(getquerystring(idx, p));
}
function getquerystring(idx, p)
{
	var qstr;
	
	if(idx=="voting")
	{
		document.getElementById('div_vote_main').style.display='none';
		document.getElementById('ajax_div').style.display='block';

		var id=document.getElementById('pid_id').value;
		qstr = 'rate='+escape(p)+'&id='+id;
	}
	if(idx=="favorites")
	{
		var ajax_div = document.getElementById('div_fav');
		ajax_div.innerHTML='<img src="/images/loading.gif" height="24" alt="" align="center">';
		
		var id=document.getElementById('pid_id').value;
		var lang=document.getElementById('lang_id').value;
		qstr = 'action='+escape(p)+'&id='+id+'&lang='+lang;
	}
	if(idx=="addresses")
	{
		/*var ajax_div = document.getElementById('div_addresses');
		ajax_div.innerHTML='<img src="/images/loading.gif" height="24" alt="" align="center">';*/
		
		var lang=document.getElementById('lang_id').value;
		qstr = 'id='+escape(p)+'&lang='+lang;
	}
	if(idx=="prom_code")
	{
		var lang=document.getElementById('lang_id').value;
		var code=document.getElementById('id_promcode').value;
		
		var ajax_div = document.getElementById('td_prom_code');
		ajax_div.innerHTML='<img src="/images/loading.gif" height="16" alt="" align="center">';
		
		qstr = 'code='+escape(code)+'&lang='+lang;
	}

	return qstr;
}
function updatepage(str, idx, p)
{
	if(idx=="voting")
	{
		document.getElementById('div_voting').innerHTML=str;
		document.getElementById('div_vote_main').style.display='block';
		document.getElementById('ajax_div').style.display='none';
	}
	if(idx=="favorites")
	{
		document.getElementById('div_fav').innerHTML=str;
	}
	if(idx=="addresses")
	{
		document.getElementById('id_li_'+p).style.display='none';
		//document.getElementById('div_addresses').innerHTML=str;
	}
	if(idx=="prom_code")
	{
		if(str == '')
		{
			document.getElementById('td_prom_title').style.color='#CC0000';
			document.getElementById('td_prom_code').innerHTML='<input type="text" name="prom_code" value="" class="side2-textfield" id="id_promcode" /> <input type="button" name="ok" value="ok" class="button" style="height:18px" onclick=\'JavaScript:xmlhttpPost("/includes/ajax.prom_code.php", "prom_code", "")\' />';
		}
		else
		{
			var strArr = str.split('***');
			//document.getElementById('td_prom_title').innerHTML=str;
			document.getElementById('td_prom_code').innerHTML=strArr[0];
			document.getElementById('td_total').innerHTML=strArr[1];
		}
	}
}

function changeListStyle(id, num)
{
	var namediv = document.getElementById('list_name_'+id);
	var pricediv = document.getElementById('list_price_'+id);
	
	if(namediv)
		namediv.className='cont-box'+num+'-top-text';
		
	if(pricediv)
		pricediv.className='cont-box'+num+'-price';
}

function check_all(){
	var lot_form = document.getElementById('lot_form');
	var lg = lot_form.elements['lots[]'].length;
	if(lg){
		for(i = 0; i < lg; i++){
			lot_form.elements['lots[]'][i].checked = true;
			lot = document.getElementById('lot_'+lot_form.elements['lots[]'][i].id);
			lot.setAttribute('bgColor','#CCCCCC');
		}
	}
	else{
		lot_form.elements['lots[]'].checked = true;
		lot = document.getElementById('lot_'+lot_form.elements['lots[]'].id);
		lot.setAttribute('bgColor','#CCCCCC');
	}
}

function onPayChange(val){
	if(val == 3){
		document.getElementById('deliv').checked = true;
		document.getElementById('deliv').disabled = true;
	}
	else{
		document.getElementById('deliv').disabled = false;
	}
}

function changeStar(id)
{
	var starName = 'star_full.png';
	
	for(var i=1; i<=5; i++)
	{
		document.getElementById('star_empty_'+i).src='/images/star_empty.png';
	}
		
	for(var i=1; i<=id; i++)
	{
		document.getElementById('star_empty_'+i).src='/images/'+starName;
	}
	return;
}

function slide(){
	jQuery('#simpleNext').click();
}

//Launch the scroll every 4 seconds
var intervalId = window.setInterval(slide, 4000);

//On user click deactivate auto-scrolling
jQuery('#previous, #simpleNext').click(
	function(){
		window.clearInterval(intervalId);
	}
);

jQuery(document).ready(function () {
	if(jQuery('#viewport').length > 0){
		jQuery('#viewport').show();
		jQuery('#viewport').carousel('#simplePrevious', '#simpleNext');
	
		jQuery('#viewport ul').mouseover(
			function(){
				window.clearInterval(intervalId);
			}
		);
		jQuery('#viewport ul').mouseout(
			function(){
				intervalId = window.setInterval(slide, 4000);
			}	
		);
	}
	
	/*  Password */
	jQuery('#password-clear').show();
	jQuery('#password-password').hide();

	jQuery('#password-clear').focus(function() {
		jQuery('#password-clear').hide();
		jQuery('#password-password').show();
		jQuery('#password-password').focus();
	});
	jQuery('#password-password').blur(function() {
		if(jQuery('#password-password').val() == '') {
			jQuery('#password-clear').show();
			jQuery('#password-password').hide();
		}
	});

	jQuery('.default-value').each(function() {
		var default_value = this.value;
		jQuery(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		jQuery(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	if(jQuery('#app_img').length > 0){
		Zapatec.Calendar.setup({
	        inputField 		: "app_date",   // id of the input field
	        button      	: "app_img",  // What will trigger the popup of the calendar
	        showsTime      	: true,          //no time
	        dateStatusFunc  : disallowDateBefore, //the function to call
	        onUpdate       	: null,
	        ifFormat:"%d/%m/%Y %H:%M"
		});
	}
	
	//jQuery('iframe').contents().find('body #NailStudio').append('<param name="wmode" value="transparent" />');
	//jQuery('iframe').contents().find('body embed').attr("wmode", "transparent");
	
	/*jQuery('#simple-list-1').click(function() {
		jQuery(this).next().toggle('slow');
		return false;
	}).next().hide();*/
	
	var icons = {
		header: "ui-icon-circle-arrow-e",
		headerSelected: "ui-icon-circle-arrow-s"
	};
	
	if(jQuery('.simple-list-1').length){
		jQuery.extend(jQuery.ui.accordion.animations, {
		slowslide: function(options) {
		    jQuery.ui.accordion.animations.slide(options, { duration: 1200 }); }
		 });
		
		if(jQuery('input:hidden[name=accAct]').length){
			var accAct = jQuery('input:hidden[name=accAct]').val();
			jQuery('.simple-list-1').accordion({
				icons: icons,
				event: 'click',
				navigation: true,
				navigationFilter: function() {
					return this.href == location.href.replace(/-\d*/g, '');
				},
				clearStyle: true,
				autoHeight: false,
				create: function(event, ui){
					jQuery('.simple-list-1 > h3 > span').remove();
				},
				//animated: 'slowslide',
				active: "'#"+accAct+"'"
			});
		} else {
			jQuery('.simple-list-1').accordion({
				icons: icons,
				event: 'click',
				navigation: true,
				navigationFilter: function() {
					return this.href == location.href.replace(/-\d*/g, '');
				},
				clearStyle: true,
				autoHeight: false,
				create: function(event, ui){
					jQuery('.simple-list-1 > h3 > span').remove();
				}
				//animated: 'slowslide'
			});
		}
		jQuery('.simple-list-1 h3 a').click(function(){
			jQuery(".act-arr-down").addClass('act-arr-right').removeClass('act-arr-down');
			if(jQuery.trim(jQuery(this).parent().next().html()) != ''){
				jQuery(this).removeClass('act-arr-right').addClass('act-arr-down');
			}
			if(jQuery.trim(jQuery(this).parent().next().html()) == ''){
				document.location.href = jQuery(this).attr('href');
			}
		})
		jQuery('.simple-list-1 div').each(function(index) {
		    if(jQuery.trim(jQuery(this).html()) != ''){
		    	jQuery(this).prev().children().addClass('act-arr-right');
			}
		  });
		jQuery(".ui-state-active a").removeClass('act-arr-right');
		if(jQuery.trim(jQuery(".ui-state-active").next().html()) != ''){
			jQuery(".ui-state-active a").addClass('act-arr-down');
		}
	}
});

function removeUiClasses(){
	jQuery('#simple-list-2').removeClass('ui-accordion');
}

function changePrice(el){
	//var priceId = '#price_'+el.value;
	//jQuery('#price-block').children().hide();
	//jQuery(priceId).show();
	var mtId = el.value;
	var langCode = jQuery('input:hidden[name=currentLangCode]').val();
	var langId = jQuery('input:hidden[name=currentLangId]').val();
	
	
	var response = jQuery.ajax({
		method: "GET",
		url: "/includes/ajax.php",
		data: "func=change_price&mt_id="+mtId+"&langCode="+langCode+"&langId="+langId,
		success: function(response){
			//document.getElementById("price-block").innerHTML = response;
			jQuery('#price-block').html(response);
			var prc = parseInt(jQuery("#price-block .prices .fll .sale").html());
			if(prc > 0){
				/*jQuery('.prodtetc .addtocart').css('cursor', 'pointer');
				jQuery('.prodtetc .addtocart .addcart_readmore').css('color', '#FFFFFF');
				jQuery('div.prodtetc > div.addtocart').click(document.addtocart.submit);*/
				jQuery('.prodtetc .addtocart').eq(0).show();
				jQuery('.prodtetc .addtocart').eq(1).hide();
			} else {
				jQuery('.prodtetc .addtocart').eq(0).hide();
				jQuery('.prodtetc .addtocart').eq(1).show();
				/*jQuery('div.prodtetc > div.addtocart').unbind('click', document.addtocart.submit);
				jQuery('.prodtetc .addtocart').css('cursor', 'default');
				jQuery('.prodtetc .addtocart .addcart_readmore').css('color', '#CCCCCC');*/
			}
		}
	});
}

function clearField(thefield){
	if (thefield.defaultValue==thefield.value)
		thefield.value = "";
}

jQuery(document).ready(function (){
	//alert(jQuery('.button').css("width"));
	//var a=jQuery('.button').width()+22;
	//jQuery('.btn-div').width(a);
	//var a = jQuery('.btn-div').width() + 1;
	//jQuery('.btn-div').width(a);
	fix_flash();
	//setInterval( fix_flash, 10000);
	});

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}

