// vi_contact.js
// Written by Ken Gould
// 2010-03-31
// @2010 Vectron International
// 2010-07-29 Released
//


var txtStates = "";
var txtCountries = "";
var bStatesRequired = false ;
var rep_eml ;
var rep_name ;
var rep_id ;

////////////////////// show_contact_form ///////////////////
//
// show appopriate form
//
//
function show_contact_form(txt_form)
{
	var formURL ;
	var d = new Date();
	$('div#vi_contact_dlg').dialog('close') ;
	$('div#vi_contact_dlg').dialog('open') ;
	$('div#vi_contact_dlg').html('<div>Loading Form...</div>') ;
	formURL = "/contact/form_" + txt_form + ".htm?o=" + d.getMinutes() ;
	$.get(formURL, function(data) {
		$('div#vi_contact_dlg').html(data);
	 },'html');	 
	//$('div#vi_contact_dlg').html('Donec iaculis ante dictum diam mattis in vulputate risus molestie. Phasellus a ultricies odio. Praesent vitae mauris in diam tempus tincidunt quis eu velit. Aenean et quam at tortor sagittis molestie. Praesent mattis risus vel dolor ornare a dapibus elit tincidunt. Etiam feugiat risus a ante malesuada scelerisque sit amet venenatis nibh. Cras quis turpis quis sapien faucibus ornare. Sed sapien purus, porta a vestibulum eget, eleifend eleifend ipsum. Mauris id libero vehicula tellus ultrices semper. Etiam luctus egestas augue id porta. Donec enim urna, adipiscing nec euismod sed, sollicitudin eu erat. In hac habitasse platea dictumst. Nunc varius, urna at tristique pellentesque, neque mauris scelerisque augue, eu pharetra ligula eros eu elit. Etiam ut condimentum velit. Sed sit amet volutpat risus.')
	return ;
}


////////////////////// show_rep_form ///////////////////
//
// show appopriate sales rep form
//
//
function show_rep_form(txt_rep, region)
{
	var d = new Date();
	var form_file ;
	if (region == undefined)
		form_file = "/contact/form_SALES_REP.htm?o=" + d.getMinutes() ;
	else
		form_file = "/contact/form_" + region + "_SALES_REP.htm?o=" + d.getMinutes() ;
	// close it first
	$('div#vi_contact_dlg').dialog('close') ;
	$.get(form_file, function(data) {
		$('div#vi_contact_dlg').html(data);
		// show the dialog
		$('div#vi_contact_dlg').dialog('open') ;
	});		
	rep_eml = txt_rep ;
	rep_id = "#" + txt_rep ;
	rep_name = 'Contact - ' + $(rep_id).html() ;

	// update rep firm name and eml value
	$('div#vi_contact_dlg').dialog({
		focus: function(event, ui) {  
		set_rep_name() ;
		}
		});
	return ;
}

///////////////////// set_rep_name //////////////////////
//
//
function set_rep_name()
{
	// update rep firm name and eml value
	$('input#nContactID').val(rep_eml) ;
	$('div#rep_name_id').html(rep_name) ;
}


/////////////////////// show_rep_box //////////////////////
//
// show html snippet for file specified
// otherwise set html to 0 length and close
//
//
function show_rep_box(txt_rep)
{
	var txtDoc = "" ;
	switch(txt_rep)
	{
		case "NA":
			txtDoc = 'north_america_rep.htm' ;
			break ;
		case "EMEA":
			txtDoc = 'europe_africa_rep.htm' ;
			break ;
		case "ASIA":
			txtDoc = 'asia_rep.htm' ;
			break ;
		case "DIST":
			txtDoc = 'dist_rep.htm' ;
			break ;
		default:
			txtDoc = "" ;
	}
	if (txtDoc.length > 0)
	{
		$.get(txtDoc, function(data) {
		  $('div#rep_list_box').html(data) ;
		});
		$('div#rep_list_box').slideDown('slow') ;
		// hide data rows
		$('rep_row').css('display','none') ;
	}
	else
	{
		$('div#rep_list_box').slideDown('slow') ;
		$('div#rep_list_box').html('') ;
	}
	return ;
}


////////////////////////// check_country /////////////////////
//
// check country to prompt for state/provence
//
function check_country(fForm)
{
	if (fForm.value =="US" || fForm.value =="CA" || fForm.value == "MX")
	{
		$('div#txtStates').css('display','block') ;
		$('div#txtStates').focus() ;
		// table row not understood by all browsers
		try
		{
		$('tr#zip_code_row').css('display','table-row') ;
		}
		catch(err)
		{
		$('tr#zip_code_row').css('display','block') ;
		}
		bStatesRequired = true ;
	}
	else
	{
		$('div#txtStates').css('display','none') ;
		$('tr#zip_code_row').css('display','none') ;
		bStatesRequired = false ;
	}
}


//////////////////////////////////////////////////////////////
//
//
// JQuery ready
//
//
$(document).ready(
	function() {
		$('div#vi_contact_dlg').dialog({
			autoOpen: false ,
			show: 'blind',
			width: '605',
			position: 'top',
			modal: true,
			close: function(e, ui) {
				$('div#vi_contact_dlg').html('') ;
			}
		}) ;

	} ) ;
	
	
//////////////////////////// showNARow ///////////////////////////
//
//
//
//
function showNARow(fForm)
{
	var row_id = '#NA_ROW_' + fForm.value ;
	var row_id_n ;
	// clear all rows
	$('tr.rep_row').hide() ;
	// show base row
	$(row_id).show() ;
	var i ;
	for (i=1; i< 10;i++)
	{
		row_id_n = row_id + "_"+ i	;
		$(row_id_n).show() ;
	}
}

//////////////////////////// showASIARow ///////////////////////////
//
//
//
//
function showASIARow(fForm)
{
	var row_id = '#ASIA_ROW_' + fForm.value ;
	var row_id_n ;
	// clear all rows
	$('tr.rep_row').hide() ;
	// show base row
	$(row_id).show() ;
	var i ;
	for (i=1; i< 10;i++)
	{
		row_id_n = row_id + "_"+ i	;
		$(row_id_n).show() ;
	}
}


//////////////////////////// showASIARow ///////////////////////////
//
//
//
//
function showEMEARow(fForm)
{
	var row_id = '#EMEA_ROW_' + fForm.value ;
	var row_id_n ;
	// clear all rows
	$('tr.rep_row').hide() ;
	// show base row
	$(row_id).show() ;
	var i ;
	for (i=1; i< 10;i++)
	{
		row_id_n = row_id + "_"+ i	;
		$(row_id_n).show() ;
	}
}

////////////////////////////// get_countries /////////////////////
//
//
//
function get_countries()
{
	if (txtCountries.length ==0)
	{
		$.get('country_list.htm', function(data) {
		txtCountries = data ;
		});		
	}
	return txtCountries ;
}

///////////////////////////// get_form_xml /////////////////////////
//
// return xml of form elements
//
//
function get_form_xml(frmName, txtRoot)
{
	var s ;
	var j ;
	s = "<?xml version=\"1.0\"?>\n" ;
	s += "<" + txtRoot + ">\n" ;
	var x=document.getElementById(frmName);
	for (var i=0;i<x.length;i++)
	  {
	  switch (x.elements[i].type.toLowerCase())
	  {
	  	case "text":
	  	case "hidden":
	  	case "password":
	  	case "selection":
	  	case "textarea":
	  		s += "<form_element>\n<name><![CDATA[" + x.elements[i].name + "]]></name>\n" + 
				"<type><![CDATA[" + x.elements[i].type + "]]></type>\n" +
				"<frm_value><![CDATA[" +   x.elements[i].value + "]]></frm_value>\n</form_element>\n" ;
	  		break ;
	  	case "checkbox":
	  	case "radio":
	  			if (x.elements[i].checked==true) 
				s += "<form_element>\n<name><![CDATA[" + x.elements[i].name + "]]></name>\n" + 
					"<type><![CDATA[" + x.elements[i].type + "]]></type>\n" +
					"<frm_value><![CDATA[" +   x.elements[i].value + "]]></frm_value>\n</form_element>\n" ;
	  		break ;	  		
	  	default:
	  		s += "<form_element>\n<name><![CDATA[" + x.elements[i].name + "]]></name>\n" + 
				"<type><![CDATA[" + x.elements[i].type + "]]></type>\n" +
				"<frm_value><![CDATA[" +   x.elements[i].value + "]]></frm_value>\n</form_element>\n" ;
	  }
	  }  
	s += "</" + txtRoot + ">\n" ;
	return s ;
}  

//////////////////////////// submit_contact_form //////////////////////////
//
//
//
//
//
function submit_contact_form(txtForm, txt_email)
{
	var frmID ;
	frmID = "#" + txtForm ;
	// set up response
	str = "<a href=\"http://www.vectron.com\"><img src=\"http://www.vectron.com/images/vectron_logo_2.gif\" alt=\"VI Logo\" width=\"100\" border=\"0\"></a><br>"
	str += "<p class=\"dialog_title\">Thank you for your interest in Vectron International.<br><br>We have sent your request " ;
	str += "to the appropriate people.  You should hear back via the email you provided shortly.<br><br>A copy of this " ; 
    str += "request has been sent to " + txt_email + " </p><p style=\"text-align:center;\"><input type=\"submit\" name=\"Close\" value=\"Close\"" ;
	str += "onClick=\"$(\'div#vi_contact_dlg\').dialog(\'close\') ;\"></p>" ;
	// get xml
	var xml ;
	xml = get_form_xml (txtForm, "vi_contact_form") ;
	//alert ("XML Ouput:\n" + xml );
	// post data
	$.post("/contact/vi_contact.asp", { txtForm: txtForm, txtXML: xml , txtOption: "NEW_CONTACT", txtEMail: txt_email },
	   function(data){
	   var txtResponse = data ;
	   if (txtResponse == "Success")
			$('div#vi_contact_dlg').html(str) ;
	   else
			$('div#vi_contact_dlg').html(txtResponse) ;
		//alert("Data Loaded: " + data);
	   },'text');	
	return false ;
}
