//Do tabs
function toggleTabs(strDivID) {
 $('div#tabbed div#tabbedA').css('display', 'none');
 $('div#tabbed div#tabbedB').css('display', 'none');
 $('div#tabbed div#tabbedC').css('display', 'none');
 $('ul#tabs').removeClass().addClass(strDivID);

 $('div#tabbed div#' + strDivID).css('display', 'block');
}//function

function showImage(intImageID) {
 $.get('ajax/getimage.php', { id: intImageID }, function(xmlData) {
   $(xmlData).find('image').each( function() {
    intWidth = $(this).find('width').text();
    intHeight = $(this).find('height').text();
    intExtraHeight = parseInt($(this).find('height').text()) + 100;
    strSrc = $(this).find('src').text();
    var popupWindow = window.open('image.php?type=360&id=' + intImageID + '&w=' + intWidth + '&h=' + intHeight + '&src=' + strSrc,'popupWindow','height=' + intExtraHeight + ',width=' + intWidth);
   });
  });
}//function

function showStaticImage(intImageID) {
 $.get('ajax/getimage.php', { id: intImageID }, function(xmlData) {
   $(xmlData).find('image').each( function() {
    intWidth = $(this).find('width').text();
    intHeight = $(this).find('height').text();
    intExtraHeight = parseInt($(this).find('height').text()) + 100;
    strSrc = $(this).find('src').text();
    var popupWindow = window.open('image.php?type=static&id=' + intImageID + '&w=' + intWidth + '&h=' + intHeight + '&src=' + strSrc,'popupWindow','height=' + intExtraHeight + ',width=' + intWidth);
   });
  });
}//function

//Do tabs
function addRow(objButton) {

 intThisRowNumber = getRowCount();
 intNextRowNumber = intThisRowNumber + 1;

 strQuantity = $('input#inputQuantity' + intThisRowNumber).val();
 $('input#inputQuantity' + intThisRowNumber).parent('td').append(strQuantity);
 $('input#inputQuantity' + intThisRowNumber).css('display', 'none');

 strQuality = $('select#selectQuality' + intThisRowNumber).val();
 $('select#selectQuality' + intThisRowNumber).parent('td').append(strQuality);
 $('select#selectQuality' + intThisRowNumber).css('display', 'none');

 if ($('input#inputType360' + intThisRowNumber).attr('checked')) {
  strType = '360';
 } else {
  strType = 'Static';
 }//if
 $('input#inputType360' + intThisRowNumber).parent('td').append(strType);
 $('input#inputType360' + intThisRowNumber).parent('td').find('label').each( function() {$(this).css('display', 'none')});
 $('input#inputType360' + intThisRowNumber).css('display', 'none');
 $('input#inputTypeStatic' + intThisRowNumber).css('display', 'none');

 if ($('input#inputWatermarkYes' + intThisRowNumber).attr('checked')) {
  strWatermark = 'Yes';
 } else {
  strWatermark = 'No';
 }//if
 $('input#inputWatermarkYes' + intThisRowNumber).parent('td').append(strWatermark);
 $('input#inputWatermarkYes' + intThisRowNumber).parent('td').find('label').each( function() {$(this).css('display', 'none')});
 $('input#inputWatermarkYes' + intThisRowNumber).css('display', 'none');
 $('input#inputWatermarkNo' + intThisRowNumber).css('display', 'none');

 objButton.attr('value', 'Remove');
 objButton.removeClass('add').addClass('remove');
 objButton.attr('id', 'remove' + objButton.attr('id'));

 tableRow = $('<tr></tr>');

 tableCell = $('<td></td>');
 tableCell.append('<input type="text" name="Quantity' + intNextRowNumber + '" id="inputQuantity' + intNextRowNumber + '" class="numeric" />');
 tableRow.append(tableCell);

 tableCell = $('<td></td>');
 selectElement = $('<select name="Quality' + intNextRowNumber + '" id="selectQuality' + intNextRowNumber + '">');
 selectElement.append('<option value="Basic">Basic</option>');
 selectElement.append('<option value="Standard" selected="selected">Standard</option>');
 selectElement.append('<option value="Super">Super</option>');
 tableCell.append(selectElement);
 tableRow.append(tableCell);

 tableCell = $('<td></td>');
 tableCell.append('<input type="radio" name="Type' + intNextRowNumber + '" value="360" checked="checked" id="inputType360' + intNextRowNumber + '" class="radio" />');
 tableCell.append('<label for="inputType360' + intNextRowNumber + '">360&deg;</label>');
 tableCell.append('<input type="radio" name="Type' + intNextRowNumber + '" value="Static" id="inputTypeStatic' + intNextRowNumber + '" class="radio" />');
 tableCell.append('<label for="inputTypeStatic' + intNextRowNumber + '">Static</label>');
 tableRow.append(tableCell);

 tableCell = $('<td></td>');
 tableCell.append('<input type="radio" name="Watermark' + intNextRowNumber + '" value="1" id="inputWatermarkYes' + intNextRowNumber + '" class="radio" />');
 tableCell.append('<label for="inputWatermarkYes' + intNextRowNumber + '">Yes</label>');
 tableCell.append('<input type="radio" name="Watermark' + intNextRowNumber + '" value="0" checked="checked" id="inputWatermarkNo' + intNextRowNumber + '" class="radio" />');
 tableCell.append('<label for="inputWatermarkNo' + intNextRowNumber + '">No</label>');
 tableRow.append(tableCell);

 tableCell = $('<td></td>');
 tableCell.append('<input type="button" value="Add" id="button' + intNextRowNumber + '" class="button add" />');
 tableRow.append(tableCell);

 $('form#quoteForm tbody').append(tableRow);
 intRowCount++;
 removeQuote();
 doButtons();

}//function

function removeRow(objButton) {
 objButton.parents('tr').remove();
 removeQuote();
}//function

function getRowCount() {
 return intRowCount;
 //$('form#quoteForm tbody tr').length;
}//function

function doButtons() {
 $('form#quoteForm input.add:button').unbind().click( function() {
  if (checkFields($(this).attr('id'))) {
   addRow($(this));
  }//if
 });
 $('form#quoteForm input.remove:button').unbind().click( function() {
  removeRow($(this));
 });
}//function

function checkFields(strButtonId) {
 intRowNumber = strButtonId.replace(/[^0-9]/g,'');
 strErrors = '';

 //Check quantity
 strQuantity = $('form#quoteForm input#inputQuantity' + intRowNumber).val();
 $('form#quoteForm input#inputQuantity' + intRowNumber).val(strQuantity.replace(/[^0-9]/g,''));

 if ($('form#quoteForm input#inputQuantity' + intRowNumber).val() == '') {
  strErrors = strErrors + "\r\n- Quantity is empty";
 } else if ($('form#quoteForm input#inputQuantity' + intRowNumber).val() != parseInt($('form#quoteForm input#inputQuantity' + intRowNumber).val())) {
  strErrors = strErrors + "\r\n- Quantity is a numeric field";
 }//if

 if ($('form#quoteForm input#inputQuantity' + intRowNumber).val() > 999) {
  strErrors = strErrors + "\r\n- Quantity should be realistic!";
 }//if

 if (strErrors == '') {
  return true;
 } else {
  alert("Please complete all fields correctly before you add this product to your quote:\r\n" + strErrors);
  return false;
 }//if
}//function

function removeQuote() {
 $('div#liveQuote').empty();
}//function

$(document).ready(function() {

	//Do add to favourites
 var newLI = $('<li id="ctaFav"><a title="Add ProductPics.co.uk to your browser favourites" id="link-fav" href="javascript:void(false)">add to favourites</a></li>');
 $('ul#actions').append(newLI);

 if (window.sidebar) {
   $('a#link-fav').click(function() {window.sidebar.addPanel(document.title,self.location,'')});

 } else if (window.external) {
  $('a#link-fav').click(function() {window.external.AddFavorite(self.location,document.title)});

 } else if (window.opera) {
  $('a#link-fav').click(function() {
       var e = document.createElement('a');
       e.setAttribute('href',self.location);
       e.setAttribute('title',document.title);
       e.setAttribute('rel','sidebar');
       e.click();
    });

 }//if

 $('ul#tabs li#tabA').click( function() {toggleTabs('tabbedA');} );
 $('ul#tabs li#tabB').click( function() {toggleTabs('tabbedB');} );
 $('ul#tabs li#tabC').click( function() {toggleTabs('tabbedC');} );

 //Show intro tab by default
 toggleTabs('tabbedA');
 $('ul#tabs li').hover( function() {$(this).addClass('hover');}, function() {$(this).removeClass('hover');} );

 intRowCount = 1;
 doButtons();

 $('input#submitButton').click( function() {

  //Get the id of the current button
  /*
  strID = false;
  $('form#quoteForm input:button').each( function() {
   alert($(this).attr('id'));
   alert($(this).css('display'));

   if ($(this).css('display') != 'none' && $(this).attr('id') != 'submitButton') {
    strID = $(this).attr('id');
   }//if
  });

  if ( (strID != false) && checkFields(strID)) {
  */
   $('form#quoteForm input:button').attr('disabled', 'true');
   $('form#quoteForm input#input-RowCount').attr('value', intRowCount);
   $(this).val('Retrieving quote...').attr('disabled', 'true');
   $.post('getquote.php', $('form#quoteForm').serialize(), function(xmlData) {

    $(xmlData).find('quote').each( function() {
     strQuote = $(this).text();
     strQuantity = $(this).attr('quantity');
     strUnitPrice = $(this).attr('unitprice');
    });

    removeQuote();

    buttonElement = $('<input type="button">');
    buttonElement.attr('value', 'Order Online >>');
    buttonElement.click( function() {
    	$('form#quoteForm').attr('action', 'orderonline').submit();
    });

    $('div#liveQuote').append('<p>The price for your ' + strQuantity + ' product images is: <span class="highlight">&pound;' + strQuote + '</span> + VAT. This works out at a price-per-image of <span class="highlight">&pound;' + strUnitPrice + '</span> + VAT.</p>');
    $('div#liveQuote').append(buttonElement);
    $('form#quoteForm input:button').attr('disabled', false);
    $('form#quoteForm input#submitButton').val('Get Quote!').attr('disabled', false);

   });

  //}//if

  return false;
 });

});