// JavaScript Document
function recalculate_price(id,color_price_eur){
	//alert(id);
	var price = $( "#price_"+id ).val();
	var qty = $( "#qty_"+id ).val();	
	var result = $("#total_price_"+id);
	if($("#color") && color_price_eur!='')
	{
		// add color price too
		var color_name = $('input#color:checked').val();//$("#color").val();
		var color_price = ajax_get_color_price(color_name,id);
		
		var color_price_eur = (price*color_price)/100;
		
		///alert("cName:"+ color_name + " // cPrice: "+ color_price + " // cPriceEuro: "+ color_price_eur);
	}
	//alert(price+"/"+qty);
	if(qty!=0)
	{
		result.html(roundNumber(Number(parseFloat(price+color_price_eur))*Number(qty), 3));
	}
}

function recalculate_price_w_color(id,color_price){
	//alert(id);
	var price = $( "#price_"+id ).val();
	var qty = $( "#qty_"+id ).val();	
	var result = $("#total_price_"+id);
	var color_price_eur = (price*color_price)/100;
	
	//alert("cPrice:"+color_price+" / cPrice Eur:" + color_price_eur + " // Price: "+ price + " // Total:"+ (Number(price)+Number(color_price_eur)));
	
	if(qty!=0)
	{
		result.html(roundNumber(Number(parseFloat(Number(price)+Number(color_price_eur)))*Number(qty), 2));
	}
}


function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
	//result = result.replace(".",",");
	return addCommas(result);
	//var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	//return result;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	if(x[1])
	{
	x2 = x[1].length > 1 ? x[1] : x[1]+'0';
	}
	else
	{
	x2 = '00';	
	}
	//x2 = x[1];
	/*
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	*/
	return x1 + "," + x2;
}

function ajax_get_color_price(color,id){
	//var file_url	= "http://xwd.garduri.ro/ajax.php";
	var file_url	= "http://www.zaun-line.de/ajax.php";
	var randomnumber= Math.floor(Math.random()*11);
	
		$.ajax(
		{
			url: file_url,
			data: "action=get_color_price&color="+color+"&id="+id+"&random="+randomnumber,
			type: "GET",
			dataType: "json",
			
			error: function(){
				//ShowStatus( "AJAX - error() "+file_url );
				
				// Load the content in to the page.
				//jContent.html( "<p>Page Not Found!!</p>" );
				//alert("<p>Page Not Found!!</p>"+file_url);
			},
			
			beforeSend: function(){
				//ShowStatus( "AJAX - beforeSend()" );
				//ShowtAnimation(".processed_prices");
				//alert("AJAX - beforeSend()");
			},
			
			complete: function(){
				//ShowStatus( "AJAX - complete()" );
				//alert("AJAX - complete()");
			},
			
			success: function( strData ){
				//ShowStatus( "AJAX - success()" );
				// Load the content in to the page.
				var error 			= strData.error;
				var error_message	= strData.error_message; 
				var total 			= strData.total; 
				//alert("Error:"+error+" | Error Message:"+error_message+" | Total:"+total);
				recalculate_price_w_color(id,total);
				return total;
				
				// UPDATING shipping fee div content
				//ResultFee.html("<strong>"+roundNumber(Number(parseFloat(shipping)), 2)+default_curency+"</strong>");
				//total_price.html("<strong>"+roundNumber(Number(parseFloat(total)), 2)+default_curency+"</strong>");
				// Closing the popup window
				//return hs.close();
				
			}
		}							
		);
	// Prevent default click.
	return( false );
}

function isZip(s) 
{
 
     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
 
     if (!reZip.test(s)) {
          //alert("Zip Code Is Not Valid");
          return false;
     }
 
return true;
}

function ajax_get_shipping_fee(){
	var jContent 	= $("#total_shipping_price" );
	var country 	= $("#country_").val();
	var zip_code	= $("#cod_postal_").val();	
	var zip_code_ok	= $("#sh_cp");	
	var zip_code_ok_	= $("#sh_cp_");		
	var ResultFee	= $("#shipping_fee");
	var total_price	= $("#total_price");
	
	if (zip_code=='' || !isZip(zip_code)){alert("Bitte geben Sie Ihre PLZ ein!"); return false;}
	//var file_url	= "http://garduri.xwd.ro/ajax.php";
	//var file_url	= "http://xwd.garduri.ro/ajax.php";
	var file_url	= "http://www.zaun-line.de/ajax.php";
	var randomnumber= Math.floor(Math.random()*11);
	
		$.ajax(
		{
			url: file_url,
			data: "action=total_shipping_fee&country="+country+"&zip="+zip_code+"&random="+randomnumber,
			type: "POST",
			dataType: "json",
			
			error: function(){
				ShowStatus( "AJAX - error() "+file_url );
				
				// Load the content in to the page.
				jContent.html( "<p>Page Not Found!!</p>" );
			},
			
			beforeSend: function(){
				ShowStatus( "AJAX - beforeSend()" );
				//ShowtAnimation(".processed_prices");
			},
			
			complete: function(){
				ShowStatus( "AJAX - complete()" );
			},
			
			success: function( strData ){
				ShowStatus( "AJAX - success()" );
				// Load the content in to the page.
				var error 			= strData.error;
				var error_message	= strData.error_message; 
				var shipping 		= strData.shipping;
				var total 			= strData.total; 
				var default_curency = strData.default_curency;
				var change_form		= strData.change_form;
				jContent.html( strData );
				//alert("Error:"+error+" | Error Message:"+error_message+" | Shipping:"+shipping+" | Total:"+total);
				
				// UPDATING shipping fee div content
				ResultFee.html("<strong>"+roundNumber(Number(parseFloat(shipping)), 2)+default_curency+"</strong>");
				total_price.html("<strong>"+roundNumber(Number(parseFloat(total)), 2)+default_curency+"</strong>");
				// Closing the popup window
				//return hs.close();
				
				if(error==1)
				{
					alert(error_message);
					
					if(change_form==1)
					{
						document.cosul.action.value = 'trimite_lista';	
						document.getElementById('payment_buttons').className = 'ascunde';
						document.getElementById('send_order_and_pay').value = document.getElementById('lang_rq').value;
						asc('buttons'); 
						asc('fullform');
					}
				}
				
				zip_code_ok.val(zip_code);
				zip_code_ok_.val(zip_code);
				
				asc('shipping_details');
				asc('get_shipping_price_small');
			}
		}							
		);
	// Prevent default click.
	return( false );
}

function ShowStatus( strStatus ){
	var jStatusList = $( "#ajax_status" );
	//alert("show_status: "+strStatus);
	// Prepend the paragraph.
	jStatusList.prepend( "<p>" + strStatus + "</p>" );
}

function ShowtAnimation(element){
	$(element).removeClass('processed_prices');
    $(element).addClass('processed_prices_loader');	
}

function asc(obj) {
	if(document.getElementById(obj)){
		var element = document.getElementById(obj);
	}
	else if(document.all){ // this is the way old msie versions work
		//alert("In document.all");
		var element = document.all[obj];
	}
	else if (document.layers){ // this is the way nn4 works
		var element = document.layers[obj];
	}
	 
	if(element.className == 'show') 
	{
		document.getElementById(obj).className = 'ascunde';
		//element.className = 'hide';
	}
	else if(element.className == 'ascunde' || element.className == '')
	{
		document.getElementById(obj).className = 'show';
		//element.className = 'hide';
	}
	//alert(obj);
	
}

function confirmLink(theLink, theSqlQuery)
{
    // Confirmation is not required in the configuration file
    if (confirmMsg == '') {
        return true;
    }

    var is_confirmed = confirm(confirmMsg + ' \n' + theSqlQuery);
    if (is_confirmed) {
        theLink.href += '&is_js_confirmed=1';
    }

    return is_confirmed;
}

// Title: Skype Killer Code v1.1
// Author: Oliver Green - http://www.codeblog.co.uk - green2go@gmail.com
// Licesnse: Freely distributable, you MUST maintain this credits & usage line at all times please. :)
// Description: Removes the Skype toolbar inline links within IE, Firefox & Chrome. 
// (Only tested in IE8 with 4.2.0.4997 of the Skype add-on loaded, FireFox 3.6.3 with 4.2.0.5016 of the Toolbar
// & Chrome 4.1.249.1045 with 1.0.0.5048 of the Toolbar loaded)
// Usage: Call the function killSkype() on document load.

var ksEles;
var ksRuns = 0;
var ksComplete = false;
var browserName=navigator.appName; 

function killSkype() {
if(ksComplete == false){
ksEles = document.body.getElementsByTagName("*");
var ksFound = false;
for(i=0;i<ksEles.length;i++){
if(ksEles[i].id=="__skype_highlight_id") ksEles[i].innerHTML = "";
if(ksEles[i].id=="__skype_highlight_originaltext_id") ksEles[i].setAttribute("class", "");
if(ksEles[i].className=="skype_pnh_print_container") ksEles[i].setAttribute("class", "");
if(ksEles[i].className=="skype_pnh_container"){
ksEles[i].innerHTML = "";
setTimeout("killSkype()",10);
ksFound = true;
if(browserName=="Microsoft Internet Explorer") break;
}	
}
if(ksRuns < 200 && ksFound == false){ setTimeout("killSkype()",10); ksRuns++; } else { if(browserName!=="Microsoft Internet Explorer") ksComplete = true; }
} }

$(document).ready(function () {
  killSkype();
});

function checkEmail() {
 var email = document.getElementById('emailaddress');
 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (!filter.test(email.value)) {
 alert('Please provide a valid email address');
 email.focus;
 return false;
 }
 }
