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()
{
   // 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 Username Field
   if (checkUsername(document.getElementById('r_user').value, 1) == '')
         return false;
   else
      document.getElementById('usernameMSG').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)
{
   //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
       {
          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)
{
   // 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('shopping_cart.php?action=add_item&id=' + id + '&price=' + price + '&options=' + options, opt);

      // Notice
      notice('notice', 'Product is successfully added to your shopping cart.');
   }


   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()
{
   document.location.href = 'shopping_cart.php?action=view_cart';
}

function checkout()
{
   document.location.href = 'https://www.dancedresser.com/shopping_cart.php?action=checkout';
}

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 updateCart()
{
   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, '$' + subtotal);
         total = total + subtotal;
         array = array + getVal('v'+i) + '-' + getVal('q'+i) + '|';
      }
   }

   var total2 = total + (getVal('shipping') * 1) + (getVal('tax') * 1);


   updateCartSession(array, total);

   // Update total price
   setVal('total_price', total2);
   notice('notice', '<font color=green>Shopping cart is successfully updated.</font>');

}


function updateCartSession(array, total)
{
   // Update Session

   //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);
    }
   }
   //Make request
   new Ajax.Request('shopping_cart.php?action=update&items=' + array + '&total=' + total, opt);

   return false;
}



function removeFromCart(id, key)
{
   Effect.SwitchOff('item' + id);
   setVal('q'+key, 0);

   updateCart();
}

function changeState(country)
{
   if(country == 'United States')
   {
      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();
}




