// JavaScript Document
//===========================================================================================
//AJAX FORM VALIDATION
//===========================================================================================
function validate_form_newsletter(action)
{
	var form_id = '_newsletter';
	var alert_txt = "";
	var field_valid = "";
	var email = document.getElementById('Email_Address');
	
	field_valid = 1;
	if(email.value.length < 5)
	{
		field_valid = 0;
		alert('1');
	}
	// - check where '@' is at
	p=email.value.indexOf('@');
	d=email.value.indexOf('.',p);
	if (p<1 || p==(email.value.length-2))
	{
		field_valid = 0;
		alert('2:'+p+'|email.value:'+email.value);
	}
	// - check where '.' is at        
	if (d<1 || d>(email.value.length-3))
	{
		field_valid = 0;
		alert('3');
	}
	// - make sure '@' is before '.'
	if (d<p)
	{
		field_valid = 0;
		alert('4');
	}

	if ((field_valid == 0) || (email.value == "Email Address"))
	{
		if (alert_txt.length > 0) 
		{
			alert_txt = alert_txt + "<br>";
		}
		alert_txt = alert_txt + "'Your Email' is not valid.";
	}
	
	if (alert_txt.length > 0) 
	{
		//alert(alert_txt);
		$('#form_container'+form_id).fadeOut('fast',function()
		{
			$('#form_alert'+form_id).html(alert_txt);
			$('#form_alert'+form_id).fadeIn('fast',function()
			{
				$('#form_alert'+form_id).fadeOut('fast',function()
				{
					$('#form_container'+form_id).fadeIn('fast');
				});
			}).delay(1500);
		});
		return false;
	}
	ajax_sendform_newsletter(email,action);
	return false;
}

function contact_form_master()
{
	var form_id = '_contact';
	var alert_txt = "";
	var name = document.getElementById('contact_name');
	var email = document.getElementById('contact_email');
	var phone = document.getElementById('contact_phone');
	var ad_type = document.getElementById('contact_ad_type');
	var comments = document.getElementById('contact_comments');
	
	$('#form_container'+form_id).fadeOut('fast',function()
	{
		$('#form_alert'+form_id).html('Validating');
		$('#form_alert'+form_id).fadeIn('fast',function()
		{
			alert_txt = validate_form_contact(form_id,name,email,phone,ad_type,comments);
			//alert(alert_txt);
			if (alert_txt.length > 0) 
			{		
				$('#form_alert'+form_id).fadeOut('fast',function()
				{
					$('#form_alert'+form_id).html(alert_txt);
					$('#form_alert'+form_id).fadeIn('fast',function()
					{
						$('#form_alert'+form_id).fadeOut('fast',function()
						{
							$('#form_container'+form_id).fadeIn('fast');
						});
					}).delay(1500);
				});
			}
			else
			{
				$('#form_alert'+form_id).fadeOut('fast',function()
				{
					$('#form_alert'+form_id).html('Sending....');
					$('#form_alert'+form_id).fadeIn('fast',function()
					{
						display_txt = ajax_sendform_contact(form_id,name,email,phone,ad_type,comments);
						name.value = 'Name';
						email.value = 'Email Address';
						phone.value = 'Phone';
						ad_type.selectedIndex=0;
						comments.value = 'Additional Comments';
						
						$('#form_alert'+form_id).fadeOut('fast',function()
						{
							$('#form_alert'+form_id).html(display_txt);
							$('#form_alert'+form_id).fadeIn('fast',function()
							{
								$('#form_alert'+form_id).fadeOut('fast',function()
								{
									$('#form_container'+form_id).fadeIn('fast');
								});
							}).delay(1500);
						});
					});
				});				
			}			
		});
	});	
	return false;
}

function validate_form_contact(form_id,name,email,phone,ad_type,comments)
{
	var alert_txt = "";
	var field_valid = "";
			
	field_valid = 1;
	if(name.value.length < 2)
	{
		field_valid = 0;
	}
	if ((field_valid == 0) || (name.value == "Name"))
	{
		if (alert_txt.length > 0) 
		{
			alert_txt = alert_txt + "<br>";
		}
		alert_txt = alert_txt + "Your 'Name' is not valid.";
	}
		
	field_valid = 1;
	if(email.value.length < 5)
	{
		field_valid = 0;
	}
	// - check where '@' is at
	p=email.value.indexOf('@');
	d=email.value.indexOf('.',p);
	if (p<1 || p==(email.value.length-2))
	{
		field_valid = 0;
	}
	// - check where '.' is at        
	if (d<1 || d>(email.value.length-3))
	{
		field_valid = 0;
	}
	// - make sure '@' is before '.'
	if (d<p)
	{
		field_valid = 0;
	}

	if ((field_valid == 0) || (email.value == "Email Address"))
	{
		if (alert_txt.length > 0) 
		{
			alert_txt = alert_txt + "<br>";
		}
		alert_txt = alert_txt + "'Your Email' is not valid.";
	}
	
	field_valid = 1;
	if((phone.value.length < 10)||(phone.value.length > 17))
	{
		field_valid = 0;
	}
	
	s = phone.value;
	var v = "0123456789-() ";
	for (i=0; i < s.length; i++) {
	x = s.charAt(i);
	if (v.indexOf(x,0) == -1)
		field_valid = 0;
	}
	
	if ((field_valid == 0) || (phone.value == "Phone"))
	{
		if (alert_txt.length > 0) 
		{
			alert_txt = alert_txt + "<br>";
		}
		alert_txt = alert_txt + "'Phone' is not valid.";
	}	
	
	field_valid = 1;
	if(ad_type.value.length < 2)
	{
		field_valid = 0;
	}
	if ((field_valid == 0))
	{
		if (alert_txt.length > 0) 
		{
			alert_txt = alert_txt + "<br>";
		}
		alert_txt = alert_txt + "'Advertisement Type' is not valid.";
	}
	return alert_txt;
}
//===========================================================================================
//AJAX
//===========================================================================================
function ajax_sendform_newsletter(email,action)
{
	var form_id = '_newsletter';
	var passfail = '';
	bodyContent = $.ajax({
		  url: "newsletter_add.asp?action=" + action + "&email=" + encodeURIComponent(email.value),
		  global: false,
		  type: "POST",
		  //data: ({code : code}),
		  dataType: "html",
		  async:false,
		  success: function(msg){
			 passfail = 'success';
			 //alert("sucess msg:"+msg);
			 //alert("Shipping_ID=" + Shipping_ID + "&Shipping_Cost=" + Shipping_Cost + "&shipping_giftbox=" + shipping_giftbox);
		  },
		  error: function(msg){
			 passfail = 'error';
			 //alert("name=" + encodeURIComponent(name) + "&email=" + encodeURIComponent(email) + "&question=" + encodeURIComponent(question));
			 //alert("fail msg:"+msg);
		  }
	   }
	).responseText;
	//alert(passfail);
	//alert(bodyContent);
	if (passfail == 'success')
	{
	  
	  $('#form_container'+form_id).fadeOut('fast',function()
	  {
		  $('#form_alert'+form_id).html(bodyContent);
		  $('#form_alert'+form_id).fadeIn('fast',function()
		  {
			  $('#form_alert'+form_id).fadeOut('fast',function()
			  {
				  $('#form_container'+form_id).fadeIn('fast');
			  });
		  }).delay(1500);
	  });	
	  email.value = 'Email Address';
	}
	else
	{
	  $('#form_container'+form_id).fadeOut('fast',function()
	  {
		  $('#form_alert'+form_id).html('There was an error with your submital.');
		  $('#form_alert'+form_id).fadeIn('fast',function()
		  {
			  $('#form_alert'+form_id).fadeOut('fast',function()
			  {
				  $('#form_container'+form_id).fadeIn('fast');
			  });
		  }).delay(1500);
	  });
	}
}



function ajax_sendform_contact(form_id,name,email,phone,ad_type,comments)
{
	//alert('here');
	//alert('name:'+name.value+', email:'+email.value+', phone:'+phone.value+', ad_type:'+ad_type.value+', comments:'+comments.value);
	
	var passfail = '';
	if (comments.value == 'Additional Comments') 
	{
		comments.value = '';
	}
	
	bodyContent = $.ajax({
		  url: "ajax_sendform.asp?name=" + encodeURIComponent(name.value) + "&email=" + encodeURIComponent(email.value) + "&phone=" + encodeURIComponent(phone.value) + "&ad_type=" + encodeURIComponent(ad_type.value)+ "&comments=" + encodeURIComponent(comments.value),
		  global: false,
		  type: "POST",
		  //data: ({code : code}),
		  dataType: "html",
		  async:false,
		  success: function(msg){
			 passfail = 'success';
		  },
		  error: function(msg){
			 passfail = 'error';
		  }
	   }
	).responseText;
		
	return bodyContent;
}
//===========================================================================================
//toggle tab display, show hid tables
//===========================================================================================
function toggle_tabs(ID,limit)
{
	for (i = 0; i < limit; i++){ 
		toggle_display('tab_'+i,'hide');
	}
	for (i = 0; i < limit; i++){ 
		toggle_display('tab_'+i+'_alt','show');
	}
	toggle_display('tab_'+ID,'show');
	toggle_display('tab_'+ID+'_alt','hide');
}
//===========================================================================================
//toggle style for tab
//===========================================================================================
function change_style_tabs(ID,style_name,bg_img,limit)
{
	for (i = 0; i < limit; i++){ 
		e = document.getElementById('tab_'+i);
		e.className = '';
		e.style.background = "";
	}
	el = document.getElementById(ID);
	//el.className = style_name;
	el.style.background = "url(" + bg_img + ")";
	//alert('ID:'+ID+'|style:'+style_name);
	
	
}
//===========================================================================================
//add option to dropdown box
//===========================================================================================
function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
function changeOption(selectbox,index_value,text,value )
{
var optn = selectbox.options[index_value];
optn.text = text;
optn.value = value;
}
function deleteOption(selectbox,index_value)
{
	selectbox.options[index_value] = null;
}
//===========================================================================================
//orderType dynamic edit
//===========================================================================================
function order_type_edit(e,current_value,current_id,index_value)
{
	var a = document.getElementById(e);
	set_cookie('OrderType_prev_value', a.innerHTML, '1/1/2099');
	if (current_id.length > 0)
	{
		a.innerHTML = '<input name=\"OrderType_Name\" value=\"' + current_value + '\">&nbsp;<input type=\"button\" name=\"Submit\" value=\"Submit\" class=\"form_button\" onclick=\"window.open(\'admin_case_order_type2.asp?ID=\' + this.form.OrderType_ID.value + \'&index_value=' + index_value + '&OrderType_Name=\' + this.form.OrderType_Name.value, \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Delete\" value=\"Delete\" class=\"form_button\" onclick=\"window.open(\'admin_case_order_type_delete.asp?ID=\' + this.form.OrderType_ID.value + \'&index_value=' + index_value + '\', \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Cancel\" value=\"Cancel\" class=\"form_button\" onclick=\"opener.document.getElementById(\'casetype\').innerHTML = getCookie(\'OrderType_prev_value\');\"><input type=\"hidden\" name=\"OrderType_ID\" value=\"' + current_id + '\">';
	}
	else
	{
		a.innerHTML = '<input name=\"OrderType_Name\" value=\"' + current_value + '\">&nbsp;<input type=\"button\" name=\"Submit\" value=\"Submit\" class=\"form_button\" onclick=\"window.open(\'admin_case_order_type2.asp?ID=\' + this.form.OrderType_ID.value + \'&OrderType_Name=\' + this.form.OrderType_Name.value, \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Cancel\" value=\"Cancel\" class=\"form_button\" onclick=\"window.location.reload();\"><input type=\"hidden\" name=\"OrderType_ID\" value=\"' + current_id + '\">';
	}
}
//===========================================================================================
//numberlocations dynamic edit
//===========================================================================================
function case_num_locations_edit(e,current_value,current_id,index_value)
{
	var a = document.getElementById(e);
	set_cookie('NumLocations_prev_value', a.innerHTML, '1/1/2099');
	if (current_id.length > 0)
	{
		a.innerHTML = '<input name=\"NumLocations_Name\" value=\"' + current_value + '\">&nbsp;<input type=\"button\" name=\"Submit\" value=\"Submit\" class=\"form_button\" onclick=\"window.open(\'admin_case_num_locations2.asp?ID=\' + this.form.NumLocations_ID.value + \'&index_value=' + index_value + '&NumLocations_Name=\' + this.form.NumLocations_Name.value, \'remote1\', \'width=1,height=1,resizable=1\');\">&nbsp;<input type=\"button\" name=\"Delete\" value=\"Delete\" class=\"form_button\" onclick=\"window.open(\'admin_case_num_locations_delete.asp?ID=\' + this.form.NumLocations_ID.value + \'&index_value=' + index_value + '\', \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Cancel\" value=\"Cancel\" class=\"form_button\" onclick=\"window.location.reload();\"><input type=\"hidden\" name=\"NumLocations_ID\" value=\"' + current_id + '\">';
	}
	else
	{
		a.innerHTML = '<input name=\"NumLocations_Name\" value=\"' + current_value + '\">&nbsp;<input type=\"button\" name=\"Submit\" value=\"Submit\" class=\"form_button\" onclick=\"window.open(\'admin_case_num_locations2.asp?ID=\' + this.form.NumLocations_ID.value + \'&NumLocations_Name=\' + this.form.NumLocations_Name.value, \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Cancel\" value=\"Cancel\" class=\"form_button\" onclick=\"window.location.reload();\"><input type=\"hidden\" name=\"NumLocations_ID\" value=\"' + current_id + '\">';
	}
}
//===========================================================================================
//comments dynamic edit
//===========================================================================================
function case_comments_edit(e,current_value,current_id,index_value)
{
	var a = document.getElementById(e);
	set_cookie('Comments_prev_value', a.innerHTML, '1/1/2099');
	if (current_id.length > 0)
	{
		a.innerHTML = '<input name=\"Comments_Name\" value=\"' + current_value + '\">&nbsp;<input type=\"button\" name=\"Submit\" value=\"Submit\" class=\"form_button\" onclick=\"window.open(\'admin_case_comments2.asp?ID=\' + this.form.Comments_ID.value + \'&index_value=' + index_value + '&Comments_Name=\' + this.form.Comments_Name.value, \'remote1\', \'width=1,height=1,resizable=1\');\">&nbsp;<input type=\"button\" name=\"Delete\" value=\"Delete\" class=\"form_button\" onclick=\"window.open(\'admin_case_comments_delete.asp?ID=\' + this.form.Comments_ID.value + \'&index_value=' + index_value + '\', \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Cancel\" value=\"Cancel\" class=\"form_button\" onclick=\"window.location.reload();\"><input type=\"hidden\" name=\"Comments_ID\" value=\"' + current_id + '\">';
	}
	else
	{
		a.innerHTML = '<input name=\"Comments_Name\" value=\"' + current_value + '\">&nbsp;<input type=\"button\" name=\"Submit\" value=\"Submit\" class=\"form_button\" onclick=\"window.open(\'admin_case_comments2.asp?ID=\' + this.form.Comments_ID.value + \'&Comments_Name=\' + this.form.Comments_Name.value, \'remote1\', \'width=1,height=1\');\">&nbsp;<input type=\"button\" name=\"Cancel\" value=\"Cancel\" class=\"form_button\" onclick=\"window.location.reload();\"><input type=\"hidden\" name=\"Comments_ID\" value=\"' + current_id + '\">';
	}
}
//===========================================================================================
//submitting form functions
//===========================================================================================
function submitform(formName) 
{
  a = eval('document.' + formName)
  a.submit();
}
function submitform_encode(formName, region_id) 
{
  a = eval('document.' + formName)
  a.submit();
}


function show_alert()
{
	alert('Im here');	
}
function show_hide_columna(tableID, col_no) 
{
	  var table  = document.getElementById(tableID);
	  var rows = table.getElementsByTagName('tr');
	  var cells;
   
	  for (var row=1; row<rows.length;row++) 
	  {
		  cells = rows[row].getElementsByTagName('td');
   
		  if(cells[col_no].style.display!="none")
		  {
			  cells[col_no].style.display="none";
		  }
		  else
		  {
			  cells[col_no].style.display="block";
		  }
	  }
 } 
 function show_hide_column(tableID, col_ID, col_Name) 
{
	  var browser = navigator.appName;
	  if(browser.indexOf("Netscape")>=0) show_value = 'block';
	  if(browser.indexOf("Microsoft")>=0) show_value = 'inline';
	
	  var table  = document.getElementById(tableID);
	  var rows = table.getElementsByTagName('tr');
	  var show_location = document.getElementById('show_location');
	  var cells;
	  var mode = "null";
	  var show_loc_val = ' | ' + '<span onclick=\"javascript:show_hide_column(\'data_table\',\'' + col_ID + '\',\'' + col_Name+ '\');\">' + col_Name+ '</span>';
   
	  for (var row=1; row<rows.length;row++) 
	  {
		  cells = rows[row].getElementsByTagName('td');
		  //cells = rows[row].getElementById(col_ID);
   		  for (var cell=0; cell<cells.length;cell++) 
	  	  {
			 cell_id = cells[cell].getAttribute("col_ID")
			 if(cell_id  == col_ID)
			 {				  
				 if(cells[cell].style.display!="none")
				  {
					  cells[cell].style.display="none";
					  mode = "add";
				  }
				  else
				  {
					  cells[cell].style.display=show_value;
					  cells[cell].style.display="";
					  mode = "remove";
				  }
			 }
		  }
		  //if ((mode == "remove")&&(row == 1))
		  //{
			  	//rows[row].innerHTML = rows[row].innerHTML.replace('style=\"display: block;\"', '');
				//alert(rows[row].innerHTML);
		  //}
	  }
	 
	 //alert(mode);
	 if (mode == "add")
	  {
		show_location.innerHTML = show_location.innerHTML + show_loc_val;  
	  }
	  else
	  {
		  show_location.innerHTML = show_location.innerHTML.replace(show_loc_val, "");
		  //alert(show_loc_val);
	  }
	  //alert('at ending:' + show_location.innerHTML+' ===== '+show_loc_val);
 } 
