function goTo(url)
{
   document.location.href = url;
}

function check_field(form, field, errorMsg)
{
   if(!document.getElementById(field).value)
   {
      alert(errorMsg);
      document.getElementById(field).focus();
   }
   else
      document.getElementById(form).submit();
}

function check(id)
{
   if(document.getElementById(id).checked)
      document.getElementById(id).checked = false;
   else
      document.getElementById(id).checked = true;
}

function ajax_request(file, params, msg){
   //Build URL of link details script
   var url = '/dev/projects.php?' + params;

   //Build request options
    var opt = {
    // Use POST
    method: 'get',
    // Handle successful response
    onSuccess: alert(msg),
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
   }
   //Make request
   new Ajax.Request(url, opt);

   return false;
}
function register()
{
   // Check Username Field

	//alert(checkUsername(document.getElementById('r_user').value, 1));

   if (checkUsername(document.getElementById('r_user').value, 1, 0) == '')
         return false;
   else
      document.getElementById('usernameMSG').innerHTML = '';
   // Checking First name Field
   if (document.getElementById('fname').value == '') {
      document.getElementById('fnameMSG').innerHTML = '<font color=red>First name can not be empty</font>';
      new Effect.Highlight('fnameMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('fnameMSG').innerHTML = '';

   // Checking Last name Field
   if (document.getElementById('lname').value == '') {
      document.getElementById('lnameMSG').innerHTML = '<font color=red>Last name can not be empty</font>';
      new Effect.Highlight('lnameMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('lnameMSG').innerHTML = '';



   // Check Password Field
   if (document.getElementById('r_pass').value == '') {
      document.getElementById('passwordMSG').innerHTML = '<font color=red>Password can not be empty</font>';
      new Effect.Highlight('passwordMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('passwordMSG').innerHTML = '';

   // Check Confirm Password Field
   if (document.getElementById('r_cpass').value == '') {
      document.getElementById('cpasswordMSG').innerHTML = '<font color=red>Password can not be empty</font>';
      new Effect.Highlight('cpasswordMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('cpasswordMSG').innerHTML = '';

   // Check for different passwords
   if (document.getElementById('r_pass').value != document.getElementById('r_cpass').value) {

      document.getElementById('passwordMSG').innerHTML = '<font color=red>The two passwords are different</font>';
      new Effect.Highlight('passwordMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});

      document.getElementById('cpasswordMSG').innerHTML = '<font color=red>The two passwords are different</font>';
      new Effect.Highlight('cpasswordMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});

      return false;
   }
   else
   {
      document.getElementById('passwordMSG').innerHTML = '';
      document.getElementById('cpasswordMSG').innerHTML = '';
   }

   // Check Email Field
   if (document.getElementById('r_email').value == '') {
      document.getElementById('emailMSG').innerHTML = '<font color=red>Email can not be empty</font>';
      new Effect.Highlight('emailMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('emailMSG').innerHTML = '';

   // Check Street Address
   if (document.getElementById('r_street').value == '') {
      document.getElementById('streetMSG').innerHTML = '<font color=red>Street address can not be empty</font>';
      new Effect.Highlight('streetMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('streetMSG').innerHTML = '';

   // Check Postal Code
//   if (document.getElementById('r_postal').value == '') {
//      document.getElementById('postalMSG').innerHTML = '<font color=red>Postal code can not be empty</font>';
//      new Effect.Highlight('postalMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
//      return false;
//   }
//   else
//      document.getElementById('postalMSG').innerHTML = '';

   // Check City
   if (document.getElementById('r_city').value == '') {
      document.getElementById('cityMSG').innerHTML = '<font color=red>City can not be empty</font>';
      new Effect.Highlight('cityMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('cityMSG').innerHTML = '';

   // Check Country
   if (document.getElementById('r_country').value == '') {
      document.getElementById('countryMSG').innerHTML = '<font color=red>Please select your country from the list</font>';
      new Effect.Highlight('countryMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('countryMSG').innerHTML = '';


   // Check Phone Field
   if (document.getElementById('r_phone').value == '') {
      document.getElementById('phoneMSG').innerHTML = '<font color=red>Phone can not be empty</font>';
      new Effect.Highlight('phoneMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('phoneMSG').innerHTML = '';

   // Check for accepted agreement
   if(!document.getElementById('agreement').checked)
   {
      alert('You need read and accept our customer agreements before register');
      return false;
   }

   return true;

}


function profile()
{
   // Checking First name Field
   if (document.getElementById('fname').value == '') {
      document.getElementById('fnameMSG').innerHTML = '<font color=red>First name can not be empty</font>';
      new Effect.Highlight('fnameMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('fnameMSG').innerHTML = '';

   // Checking Last name Field
   if (document.getElementById('lname').value == '') {
      document.getElementById('lnameMSG').innerHTML = '<font color=red>Last name can not be empty</font>';
      new Effect.Highlight('lnameMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('lnameMSG').innerHTML = '';



   // Check for different passwords
   if (document.getElementById('r_pass').value != document.getElementById('r_cpass').value) {

      document.getElementById('passwordMSG').innerHTML = '<font color=red>The two passwords are different</font>';
      new Effect.Highlight('passwordMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});

      document.getElementById('cpasswordMSG').innerHTML = '<font color=red>The two passwords are different</font>';
      new Effect.Highlight('cpasswordMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});

      return false;
   }
   else
   {
      document.getElementById('passwordMSG').innerHTML = '';
      document.getElementById('cpasswordMSG').innerHTML = '';
   }

   // Check Email Field
   if (document.getElementById('r_email').value == '') {
      document.getElementById('emailMSG').innerHTML = '<font color=red>Email can not be empty</font>';
      new Effect.Highlight('emailMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('emailMSG').innerHTML = '';

   // Check Street Address
   if (document.getElementById('r_street').value == '') {
      document.getElementById('streetMSG').innerHTML = '<font color=red>Street address can not be empty</font>';
      new Effect.Highlight('streetMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('streetMSG').innerHTML = '';

   // Check Postal Code
   if (document.getElementById('r_postal').value == '') {
      document.getElementById('postalMSG').innerHTML = '<font color=red>Postal code can not be empty</font>';
      new Effect.Highlight('postalMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('postalMSG').innerHTML = '';

   // Check City
   if (document.getElementById('r_city').value == '') {
      document.getElementById('cityMSG').innerHTML = '<font color=red>City can not be empty</font>';
      new Effect.Highlight('cityMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('cityMSG').innerHTML = '';

   // Check Country
   if (document.getElementById('r_country').value == '') {
      document.getElementById('countryMSG').innerHTML = '<font color=red>Please select your country from the list</font>';
      new Effect.Highlight('countryMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('countryMSG').innerHTML = '';


   // Check Phone Field
   if (document.getElementById('r_phone').value == '') {
      document.getElementById('phoneMSG').innerHTML = '<font color=red>Phone can not be empty</font>';
      new Effect.Highlight('phoneMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
      return false;
   }
   else
      document.getElementById('phoneMSG').innerHTML = '';


   return true;
}

function checkUsername(username, type, display_msg)
{   //Build request options
   var status;
   var opt = {
   method: 'get',

    onSuccess: function(t) {
       if(t.responseText.match('success'))
       {
          if(type == 0)
          {
             document.getElementById('usernameMSG').innerHTML = '<font color=green>Username is available</font>';
             new Effect.Highlight('usernameMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
          }
          status = true;
       }
       else if (display_msg)
       {
          document.getElementById('usernameMSG').innerHTML = '<font color=red>Username is not available</font>';
          new Effect.Highlight('usernameMSG', {startcolor:'#DFE4EA', endcolor:'#A1BBE4'});
          status = false;
       }
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
   }
   //Make request
   new Ajax.Request('register.php?action=CU:' + username, opt);
   return status;
}


function addToCart(id, price, type, doc_root, count)
{
   // Update Session
   var optNum  = document.getElementsByTagName('select').length;
   var action  = 'forward';
   var options = '';

   for (var i = 0; i < optNum; i++)
   {
      // If not selected
      if(document.getElementById('opt'+i).value == '' && action == 'forward')
      {
         alert('You need to select all options for the item before adding to cart');
         document.getElementById('opt' + i).focus();
         action = 'return';
      }
      // If selected, assign value
      if(document.getElementById('opt'+i).value != '' && action == 'forward')
         options = options + '|' + document.getElementById('opt'+i).value + '|';
   }
   if(action == 'forward')
   {
      //Build request options
       var opt = {
       // Use POST
       method: 'get',
       // Handle successful response
       onSuccess: updateVisibleArea(id, price, type),
       // Handle 404
       on404: function(t) {
           alert('Error 404: location "' + t.statusText + '" was not found.');
       },
       // Handle other errors
       onFailure: function(t) {
           alert('Error ' + t.status + ' -- ' + t.statusText);
       }
      }
      //Make request
      new Ajax.Request(doc_root+'/shopping_cart.php?action=add_item&id=' + id + '&price=' + price + '&options=' + options + '&count=' + count, opt);
      // Notice
      notice('notice', '<center>Produkt wurde im Einkaufswagen abgelegt.<br><br></center>');
   }   return false;
}


function notice(id, message)
{
   new Effect.Highlight(id, {startcolor:'#EAEFF5', endcolor:'#FFFFFF'});
   document.getElementById(id).innerHTML = '<br>' + message;
}

function updateVisibleArea(id, price, type)
{
   if(type != 1)
   {
      // Update items
      var items = document.getElementById('shopping_cart_items').innerHTML;
      document.getElementById('shopping_cart_items').innerHTML = 1 * items + 1;

      // Update price
      var total = document.getElementById('shopping_cart_total').innerHTML.replace(',', '.');
      document.getElementById('shopping_cart_total').innerHTML = 1 * total + 1 * price;

      // Highlight
      new Effect.Highlight('shopping_cart_row', {startcolor:'#DFE4EA', endcolor:'#EAEFF5'});
   }
}
function viewCart(doc_root)
{   document.location.href = doc_root+'/shopping_cart.php?action=view_cart';
}

function checkout(doc_root)
{
   document.location.href = doc_root+'/shopping_cart.php?action=checkout1';
}

function getVal(id)
{
   if(parent.document.getElementById(id))
   {
      if(!parent.document.getElementById(id).type)
         return parent.document.getElementById(id).innerHTML;
      else
         return parent.document.getElementById(id).value;
   }
   else
      return false;
}


function setVal(id, value)
{
   if(parent.document.getElementById(id))
   {
      if(!parent.document.getElementById(id).type)
      {
         document.getElementById(id).innerHTML = value;
         return true;
      }
      else
      {
         parent.document.getElementById(id).value = value;
         return true;
      }
   }
   else
      return false;
}

function price_format(price, thousand_delim, decimal_delim, precision) {
	var thousand_delim = (arguments.length > 1 && thousand_delim !== false) ? thousand_delim : number_format_th;
	var decimal_delim = (arguments.length > 2 && decimal_delim !== false) ? decimal_delim : number_format_dec;
	var precision = (arguments.length > 3 && precision !== false) ? precision : number_format_point;

	if (precision > 0) {
		precision = Math.pow(10, precision);
		price = Math.round(price*precision)/precision;
		var top = Math.floor(price);
		var bottom = Math.round((price-top)*precision)+precision;

	} else {
		var top = Math.round(price);
		var bottom = 0;
	}

	top = top+"";
	bottom = bottom+"";
	var cnt = 0;
	for (var x = top.length; x >= 0; x--) {
		if (cnt % 3 == 0 && cnt > 0 && x > 0)
			top = top.substr(0, x)+thousand_delim+top.substr(x, top.length);

		cnt++;
	}

	return (bottom > 0) ? (top+decimal_delim+bottom.substr(1, bottom.length)) : top;
}


function updateCart(docroot)
{
   var total = 0;
   var array = '';


   // Update subtotal price
   for (var i = 1; i <= 100; i++)
   {
      if(getVal('v'+i) > 0)
      {
         var subtotal = (getVal('p'+i) * 1) * (getVal('q'+i) * 1);
         setVal('st'+i, price_format(subtotal, ' ', '.', 2));
         
         total = total + subtotal;
         array = array + getVal('v'+i) + '-' + getVal('q'+i) + '|';
      }
   }
   updateCartSession(docroot, array, total);
   


   
   notice('notice', '<font color=green>Einkaufswagen wurde aktualisiert.</font><br/>');

}


function updateCartSession(docroot, array, total)
{
   // Update Session
	document.getElementById('total_shipping').innerHTML = '<img src="templates/Default/images/ajax-loader-small.gif" border="0">';
	document.getElementById('total_tax').innerHTML = '<img src="templates/Default/images/ajax-loader-small.gif" border="0">';
	document.getElementById('total_price').innerHTML = '<img src="templates/Default/images/ajax-loader-small.gif" border="0">';
   //Build request options
    var opt = {
    // Use POST
    method: 'get',
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    },
    onSuccess: function() {
    	new Ajax.Updater('total_shipping', docroot+'/shopping_cart.php?action=update_shipping', { method: 'get' });
    	new Ajax.Updater('total_tax', docroot+'/shopping_cart.php?action=update_tax', { method: 'get' });
    	new Ajax.Updater('total_price', docroot+'/shopping_cart.php?action=update_total', { method: 'get' });
    }
   } 
   //Make request    
   var res = new Ajax.Request(docroot+'/shopping_cart.php?action=update&items=' + array + '&total=' + total, opt);
  	return false;
}



function removeFromCart(docroot, id, key){   Effect.SwitchOff('item' + id);
   setVal('q'+key, 0);
   updateCart(docroot);}

function changeState(country)
{
   if(country == 'United States')
   {
      document.getElementById('USstates').style.display = '';
      document.getElementById('r_state').style.display = 'none';
   }
   else if(country == 'Canada')
   {
      document.getElementById('USstates').style.display = '';
      document.getElementById('r_state').style.display = 'none';
   }
   else
   {
      document.getElementById('USstates').style.display = 'none';
      document.getElementById('r_state').style.display = '';
   }
}




function checkCreditCard()
{
var r = 2;

   if(!getVal('ccname'))
   {
      alert('You need to enter your card holder name');
      document.getElementById('ccname').focus();
      r = 1;
   }
   if(!getVal('ccnumber'))
   {
      alert('You need to enter Credit Card number before continue');
      document.getElementById('ccnumber').focus();
      r = 1;
   }
   if(!getVal('cvv2'))
   {
      alert('Security code can not be empty');
      document.getElementById('cvv2').focus();
      r = 1;
   }

   if (r == 2)
      document.getElementById('creditCardForm').submit();
}


