/*
  $Id: general.js 1739 2007-12-20 00:52:16Z hpdl $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

function substr( f_string, f_start, f_length ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
 
    f_string += '';
 
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}

function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
 
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

$(document).ready(function() {
	var display_position = 0;
	var us_url = $('#uns_url').attr('href');
	$('#uns_url').attr('href', 'javascript:void(0);');

	$('#uns_email').change(function() {
		if ( $(this).val() != '' && verify(document.getElementById('newsletter')) != false )
		{
			$('#uns_url').attr('href', us_url + $(this).val());
		}
		else {
			$('#uns_url').attr('href', 'javascript:void(0);');
			return false;
		}
	});
	
	$('#transport_href').click(function(){
		$('#transport_small_cont').slideUp('slow', function(){
			$('#transport_large_cont').slideDown();												
		});
	});
	
	//$('#happy_christmas_box').fadeIn('slow');
	
	$('#happy_christmas_close').click(function(){
		$('#happy_christmas_box').fadeOut('slow');								   
	});
	
	$('#switch_standard_newest').click(function(){
		if(document.getElementById('img_box_list_newest').style.display == 'none' )
		{
			$('#switch_list_newest').removeClass('selected_list');
			$('#switch_simple_newest').removeClass('selected_img_list');
			$('#switch_standard_newest').addClass('selected_4box');
			
			$('#left a').each(function(i) {
				display_position = strpos($(this).attr('href'), 'display=');
				if ( display_position !== 0 )
				{
					$(this).attr('href', substr($(this).attr('href'), 0, display_position) + 'display=0');
				}
				else {
					$(this).attr('href', $(this).attr('href') + '&display=0');
				}
			});
			
			$('#img_simple_list_newest').hide();
			$('#img_fast_list_newest').hide();
			$('#img_box_list_newest').slideDown();
		}
	});
	
	$('#switch_simple_newest').click(function(){
		if(document.getElementById('img_simple_list_newest').style.display == 'none' )
		{
			$('#switch_list_newest').removeClass('selected_list');
			$('#switch_standard_newest').removeClass('selected_4box');
			$('#switch_simple_newest').addClass('selected_img_list');
			
			$('#left a').each(function(i) {
				display_position = strpos($(this).attr('href'), 'display=');
				if ( display_position !== false )
				{
					$(this).attr('href', substr($(this).attr('href'), 0, display_position) + 'display=1');
				}
				else {
					$(this).attr('href', $(this).attr('href') + '&display=1');
				}
			});
			
			$('#img_box_list_newest').hide();
			$('#img_fast_list_newest').hide();
			$('#img_simple_list_newest').slideDown();
		}
	});
	
	$('#switch_list_newest').click(function(){
		if(document.getElementById('img_fast_list_newest').style.display == 'none' )
		{
			$('#switch_standard_newest').removeClass('selected_4box');
			$('#switch_simple_newest').removeClass('selected_img_list');
			$('#switch_list_newest').addClass('selected_list');
			
			$('#left a').each(function(i) {
				display_position = strpos($(this).attr('href'), 'display=');
				if ( display_position !== false )
				{
					$(this).attr('href', substr($(this).attr('href'), 0, display_position) + 'display=2');
				}
				else {
					$(this).attr('href', $(this).attr('href') + '&display=2');
				}
			});
			
			$('#img_box_list_newest').hide();
			$('#img_simple_list_newest').hide();
			$('#img_fast_list_newest').slideDown();
		}
	});
	
	$('#switch_standard_recommended').click(function(){
		if(document.getElementById('img_box_list_recommended').style.display == 'none' )
		{
			$('#switch_list_recommended').removeClass('selected_list');
			$('#switch_simple_recommended').removeClass('selected_img_list');
			$('#switch_standard_recommended').addClass('selected_4box');
			
			$('#left a').each(function(i) {
				display_position = strpos($(this).attr('href'), 'display=');
				if ( display_position !== false )
				{
					$(this).attr('href', substr($(this).attr('href'), 0, display_position) + 'display=0');
				}
				else {
					$(this).attr('href', $(this).attr('href') + '&display=0');
				}
			});
			
			$('#img_simple_list_recommended').hide();
			$('#img_fast_list_recommended').hide();
			$('#img_box_list_recommended').slideDown();
		}
	});
	
	$('#switch_simple_recommended').click(function(){
		if(document.getElementById('img_simple_list_recommended').style.display == 'none' )
		{
			$('#switch_list_recommended').removeClass('selected_list');
			$('#switch_standard_recommended').removeClass('selected_4box');
			$('#switch_simple_recommended').addClass('selected_img_list');
			
			$('#left a').each(function(i) {
				display_position = strpos($(this).attr('href'), '&display=');
				if ( display_position !== false )
				{
					$(this).attr('href', substr($(this).attr('href'), 0, display_position) + '&display=1');
				}
				else {
					$(this).attr('href', $(this).attr('href') + '&display=1');
				}
			});
			
			$('#img_box_list_recommended').hide();
			$('#img_fast_list_recommended').hide();
			$('#img_simple_list_recommended').slideDown();
		}
	});
	
	$('#switch_list_recommended').click(function(){
		if(document.getElementById('img_fast_list_recommended').style.display == 'none' )
		{
			$('#switch_standard_recommended').removeClass('selected_4box');
			$('#switch_simple_recommended').removeClass('selected_img_list');
			$('#switch_list_recommended').addClass('selected_list');
			
			$('#left a').each(function(i) { 
				display_position = strpos($(this).attr('href'), '&display=');
				if ( display_position !== false )
				{
					$(this).attr('href', substr($(this).attr('href'), 0, display_position) + '&display=2');
				}
				else {
					$(this).attr('href', $(this).attr('href') + '&display=2');
				}
			});
			
			$('#img_box_list_recommended').hide();
			$('#img_simple_list_recommended').hide();
			$('#img_fast_list_recommended').slideDown();
		}
	});
});

function switch_listing(box) {
	if (document.getElementById(box).style.display == 'none' && box == 'img_box_list') {
		document.getElementById(box).style.display = 'block';
		document.getElementById('img_simple_list').style.display = 'none';
		document.getElementById('par_listing_list').style.display = 'none';
	} else if (document.getElementById(box).style.display == 'none' && box == 'img_simple_list') {
		document.getElementById(box).style.display = 'block';
		document.getElementById('img_box_list').style.display = 'none';
		document.getElementById('par_listing_list').style.display = 'none';
	} else if (document.getElementById(box).style.display == 'none' && box == 'par_listing_list') {
		document.getElementById(box).style.display = 'block';
		document.getElementById('img_box_list').style.display = 'none';
		document.getElementById('img_simple_list').style.display = 'none';
	}
}


function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "") {
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target = i;
        break;
      }
    }
  }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && (TargetForm.elements[i].type != "hidden") && (TargetForm.elements[i].type != "reset") && (TargetForm.elements[i].type != "submit") ) {
      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || (TargetForm.elements[i].type == "password") ) {
        TargetForm.elements[i].select();
      }

      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString) {
    TargetElement.value = "";
  }

  TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value)) {
    return true;
  } else {
    return false;
  }
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0) {
      return true;
    }
  } else {
    if ((intYear % 4) == 0) {
      return true;
    }
  }

  return false;
}

