// JavaScript Document
<!-- Javascript for form to validate the form to make sure all fields have been filled in before submitting --->

function formIsValid()
{
	var arr=document.getElementsByTagName('*');
	
	for (var i=0; i<arr.length; i++)
	{
		if (arr[i].type=='text')
		{
			switch (arr[i].id) 
			{
				case '':
				case 'hi_mobile':
				case 'hi_paradigm':
				case 'hi_care':
				case 'hi_pinnacle':
				case 'hi_prodigy':
				case 'tri_fold_folder':
				case 'hi_summit':
				case 'hi_med':
				case 'hub_transfer':
				case 'flooring':
				case 'table_cloth':
				case 'pop_up_banner_stand':
				case 'giveaways':
					if (!isEmpty(arr[i].value))
					{
						if (isNaN(arr[i].value))
						{
							alert('Ooops! ' + getLabel(arr[i].id) + ' should contain only a number between 0 and 10000 ...');
							arr[i].focus();
							return false;
						}
					}
					break;
				case 'contact_email_address':
				case 'alternate_email_address':
					if (!isEmpty(arr[i].value))
					{
						if (arr[i].value.indexOf('.') == -1 || arr[i].value.indexOf('@') == -1)
						{
							alert('Ooops! ' + getLabel(arr[i].id) + ' is an invalid email address ...');
							arr[i].focus();
							return false;
						}
					}
					break;
				default: 
					if (isEmpty(arr[i].value)) {
						alert('Please enter ' + getLabel(arr[i].id) + ' continue...');
						arr[i].focus();
						return false;
					}
					if (arr[i].id == 'sales_rep_email_address')
					{
						if (arr[i].value.indexOf('.') == -1 || arr[i].value.indexOf('@') == -1)
						{
							alert('Ooops! ' + getLabel(arr[i].id) + ' is an invalid email address ...');
							arr[i].focus();
							return false;
						}
					}
					break;
			}
		}
		if (arr[i].type == 'select-one')
		{
			if (arr[i].selectedIndex == 0) 
			{
				alert('Please select ' + getLabel(arr[i].id) + ' continue...');
				arr[i].focus();
				return false;
			}
			else
			{
				if (arr[i].id == 'shipping_to_same_location')
				{
					if (arr[i].selectedIndex == 2)
					{
						if (isEmpty(document.getElementById('different_shipping_address').value))
						{
							alert('Please enter Different Shipping Address to continue ...');
							document.getElementById('different_shipping_address').focus();
							return false;
						}
					}
					else
					{
						document.getElementById('different_shipping_address').value = '';
					}
				}
			} 
		}
		if (arr[i].type == 'textarea') 
		{
			switch(arr[i].id)
			{
				case 'drawer_configuration':
				case 'other':
				case 'additional_comments':
				case 'additional_comments_shipping':
				case 'different_shipping_address':
				case 'additional_comments_marketing':
				case 'other_peripherals':
					break;
				default:
					if (isEmpty(arr[i].value)) 
					{
						alert('Please enter ' + getLabel(arr[i].id) + ' continue...');
						arr[i].focus();
						return false;
					}
					break;
			}
		}
	}
	
	return true;
}

function handleMaxLength(obj,maxLength)
{
	if (obj.value.length > maxLength-1)
	{
		obj.value=obj.value.substring(0,maxLength-1);
	}
}

function handleOnChange(obj)
{
	if (obj.id=='shipping_to_same_location')
	{
		if (obj.options[obj.selectedIndex].value == 2)
		{
			document.getElementById('different_shipping_address_div').style.display = 'block';
		}
		else
		{
			document.getElementById('different_shipping_address_div').style.display = 'none';
		}
	}
}

function getLabel(str)
{
	var tempArr=str.split('_');
	var tempStr = '';
	var ret = '';
	
	for (var i = 0; i < tempArr.length; i++)
	{
		if (tempArr[i] != 'of' && tempArr[i] != 'to')
		{
			if (i == 0)
			{
				if (tempArr[0] == 'hi')
				{
					tempArr[0]='hi-';
				}
				if (tempArr[0] == 'tri')
				{
					tempArr[0]='tri-';
				}
				if (tempArr[0] == 'hub')
				{
					tempArr[0]='hub/';
				}
			}
			tempStr = tempArr[i].charAt(0).toUpperCase() + tempArr[i].slice(1);
			tempArr[i] = tempStr;
		}
	}
	
	if (tempArr[0] == 'Hi-' || tempArr[0] == 'Tri-' || tempArr[0] == 'Hub-')
	{
			ret = tempArr.join('');
	}
	else
	{
			ret = (str=='cpu') ? 'CPU' : tempArr.join(' ');
	}
	
	return ret; 
}

function showCalendar(obj)
{
      
    $(obj).datepicker({
          onSelect: function(dateText, inst) { hideCalendar(obj);  }
    });
    
    $(obj).datepicker("show");
                        
}
    
function hideCalendar(obj)
{
  $(obj).datepicker("hide");
}

function isEmpty(str)
{
	if (trim(str).length==0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
	
function trim(str)
{
	return str.replace(/^\str+|\str+$/g,"");
}

function ltrim(str)
{
	return str.replace(/^\str+/,"");
}

function rtrim(str)
{
	return str.replace(/\str+$/,"");
}

