// JavaScript Document
function doWorking( )
{
	setSuccessMessage("");
	setMessage("");
}
function stopWorking( )
{
}
function clearFields()
{
	//document.getElementById( 'fname' ).value = "";
}
function showSuccess(cemail)
{
	//document.getElementById( 'emailsent' ).style.display = "";
	document.location = 'confirmation.php?cnt=' + cemail;
}
function setSuccessMessage( msg )
{
	document.getElementById( 'spmsg_top' ).innerHTML = msg;
	document.getElementById( 'spmsg_bottom' ).innerHTML = msg;
}
function setMessage( msg )
{
	document.getElementById( 'pmsg_top' ).innerHTML = msg;
	document.getElementById( 'pmsg_bottom' ).innerHTML = msg;
}

function get_cname( )
{
	return document.getElementById( 'cname' ).value;
}
function get_cphone( )
{
	return document.getElementById( 'cphone' ).value;
}
function get_cemail( )
{
	return document.getElementById( 'cemail' ).value;
}
function get_cfax( )
{
	return document.getElementById( 'cfax' ).value;
}

function get_puname( )
{
	return document.getElementById( 'puname' ).value;
}
function get_puphone( )
{
	return document.getElementById( 'puphone' ).value;
}
function get_puaddress( )
{
	return document.getElementById( 'puaddress' ).value;
}
function get_pucity( )
{
	return document.getElementById( 'pucity' ).value;
}
function get_puzip( )
{
	return document.getElementById( 'puzip' ).value;
}
function get_puemail( )
{
	return document.getElementById( 'puemail' ).value;
}
function get_pufax( )
{
	return document.getElementById( 'pufax' ).value;
}
function get_pucountry(){
	return document.getElementById('pucountry').value;
}
function get_pustate( )
{
	return document.getElementById( 'pustate' ).value;
}

function get_dname( )
{
	return document.getElementById( 'dname' ).value;
}
function get_dphone( )
{
	return document.getElementById( 'dphone' ).value;
}
function get_daddress( )
{
	return document.getElementById( 'daddress' ).value;
}
function get_dcity( )
{
	return document.getElementById( 'dcity' ).value;
}
function get_dzip( )
{
	return document.getElementById( 'dzip' ).value;
}
function get_demail( )
{
	return document.getElementById( 'demail' ).value;
}
function get_dfax( )
{
	return document.getElementById( 'dfax' ).value;
}
function get_dcountry(){
	return document.getElementById('dcountry').value;
}
function get_dstate( )
{
	return document.getElementById( 'dstate' ).value;
}

function get_bname( )
{
	return document.getElementById( 'bname' ).value;
}
function get_bphone( )
{
	return document.getElementById( 'bphone' ).value;
}
function get_baddress( )
{
	return document.getElementById( 'baddress' ).value;
}
function get_bcity( )
{
	return document.getElementById( 'bcity' ).value;
}
function get_bzip( )
{
	return document.getElementById( 'bzip' ).value;
}
function get_paymenttype( )
{
	return document.getElementById( 'paymenttype' ).value;
}
function get_bemail( )
{
	return document.getElementById( 'bemail' ).value;
}
function get_bfax( )
{
	return document.getElementById( 'bfax' ).value;
}
function get_bcountry(){
	return document.getElementById('bcountry').value;
}
function get_bstate( )
{
	return document.getElementById( 'bstate' ).value;
}

function get_pdescription( )
{
	return document.getElementById( 'pdescription' ).value;
}
function get_Packaging( )
{
	return document.getElementById( 'Packaging' ).checked;
}
function get_Crating( )
{
	return document.getElementById( 'Crating' ).checked;
}
function get_BlanketWrap( )
{
	return document.getElementById( 'Blanket Wrap' ).checked;
}
function get_NeedsAssembly( )
{
	return document.getElementById( 'Needs Assembly' ).checked;
}
function get_Stair_FlightCarry( )
{
	return document.getElementById( 'Stair_Flight Carry' ).checked;
}
function get_deductible( )
{
	return document.getElementById( 'deductible' ).value;
}
function get_irvc( )
{
	return document.getElementById( 'irvc' ).value;
}

function getText(objname)
{
	if (document.getElementById(objname) != null)
	{
		return document.getElementById(objname).value;
	}
	else
	{
		return "";
	}
}
function getCheckbox(objname)
{
	if (document.getElementById(objname) != null)
	{
		return document.getElementById(objname).checked;
	}
	else
	{
		return false;
	}
}

function ajaxFunction(tpe)
{
	doWorking();
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if( (xmlHttp.readyState == 4) && (xmlHttp.status == 200) )
		{
			var result = xmlHttp.responseText;
			var sr = result.split("|");
			var success = sr[0];
			var msg = sr[1];
			if (success.indexOf("1") > -1)
			{
				clearFields();
				setSuccessMessage( msg );
				setMessage("");
				showSuccess(cemail);
			}
			else
			{
				setMessage(msg);
			}
			stopWorking();
		}
	}

	var cname = encodeURIComponent(get_cname( ));
	var cphone = get_cphone( );
	var cemail = get_cemail( );
	var cfax = get_cfax( );
	var puname = encodeURIComponent(get_puname( ));
	var puphone = get_puphone( );
	var puaddress = encodeURIComponent(get_puaddress( ));
	var pucountry = encodeURIComponent(get_pucountry());//
	var pucity = encodeURIComponent(get_pucity( ));
	var puzip = get_puzip( );
	var puemail = get_puemail( );
	var pufax = get_pufax( );
	var pustate = encodeURIComponent(get_pustate( ));
	var dname = encodeURIComponent(get_dname( ));
	var dphone = get_dphone( );
	var daddress = encodeURIComponent(get_daddress( ));
	var dcity = encodeURIComponent(get_dcity( ));
	var dzip = get_dzip( );
	var demail = get_demail( );
	var dfax = get_dfax( );
	var dcountry = encodeURIComponent(get_dcountry());//
	var dstate = encodeURIComponent(get_dstate( ));
	var bname = encodeURIComponent(get_bname( ));
	var bphone = get_bphone( );
	var baddress = encodeURIComponent(get_baddress( ));
	var bcity = encodeURIComponent(get_bcity( ));
	var bzip = get_bzip( );
	var paymenttype = get_paymenttype( );
	var bemail = get_bemail( );
	var bfax = get_bfax( );
	var bcountry = encodeURIComponent(get_bcountry());//
	var bstate = encodeURIComponent(get_bstate( ));
	var pdescription = encodeURIComponent(get_pdescription( ));
	var Packaging = get_Packaging( );
	var Crating = get_Crating( );
	var BlanketWrap = get_BlanketWrap( );
	var NeedsAssembly = get_NeedsAssembly( );
	var FlightCarry = get_Stair_FlightCarry( );
	var deductible = get_deductible( );
	var irvc = get_irvc( );

	var bsamecontact;
	var pusamecontact;
	var dsamecontact;
	var pusamebilling;
	var dsamebilling;

	if (document.getElementById('bsamecontact'))
	{
		bsamecontact = document.getElementById('bsamecontact').checked;
	}
	else
	{
		bsamecontact = '';
	}
	if (document.getElementById('pusamecontact'))
	{
		pusamecontact = document.getElementById('pusamecontact').checked;
	}
	else
	{
		pusamecontact = '';
	}
	if (document.getElementById('dsamecontact'))
	{
		dsamecontact = document.getElementById('dsamecontact').checked;
	}
	else
	{
		dsamecontact = '';
	}
	if (document.getElementById('pusamebilling'))
	{
		pusamebilling = document.getElementById('pusamebilling').checked;
	}
	else
	{
		pusamebilling = '';
	}
	if (document.getElementById('dsamebilling'))
	{
		dsamebilling = document.getElementById('dsamebilling').checked;
	}
	else
	{
		dsamebilling = '';
	}

	if (bsamecontact == 'true')
	{
		bname = cname;
		bemail = cemail;
		bphone = cphone;
		bfax = cfax;
	}
	if (pusamecontact == 'true')
	{
		puname = cname;
		puemail = cemail;
		puphone = cphone;
		pufax = cfax;
	}
	if (dsamecontact == 'true')
	{
		dname = cname;
		demail = cemail;
		dphone = cphone;
		dfax = cfax;
	}
	if (pusamebilling == 'true')
	{
		puname = bname;
		puemail = bemail;
		puphone = bphone;
		pufax = bfax;

		puaddress = baddress;
		pucity = bcity;
		puzip = bzip;
		pustate = bstate;
		pucountry = bcountry;//
	}
	if (dsamebilling == 'true')
	{
		dname = bname;
		demail = bemail;
		dphone = bphone;
		dfax = bfax;
		
		/*
		baddress = baddress;
		bcity = bcity;
		bzip = bzip;
		bstate = bstate;
		*/
		//by moha
		daddress = baddress;
		dcity = bcity;
		dzip = bzip;
		dstate = bstate;
		dcountry = bcountry;
	}

	var fieldVar;
	var fieldItem;
	var quote_quantity = '';
	var quote_description = '';
	var quote_length = '';
	var quote_width = '';
	var quote_height = '';
	var quote_weight = '';
	var unit_length = '';
	var unit_width = '';
	var unit_height = '';
	var quote_price = '';

	var quote_glass = '';

	var rownum = 0;
	if (document.getElementById( 'rownum' ) != null)
	{
		rownum = document.getElementById( 'rownum' ).value;
	}
	for (var i = 1; i <= rownum; i++)
	{
		fieldItem = document.getElementById('fieldVar' + i);

		if (fieldItem != null)
		{
			fieldVar = fieldItem.value.split("|");

			quote_quantity = quote_quantity + fieldVar[0] + '|';
			quote_description = quote_description + fieldVar[1] + '|';
			quote_length = quote_length + fieldVar[2] + '|';
			unit_length = unit_length + fieldVar[3] + '|';
			quote_width = quote_width + fieldVar[4] + '|';
			unit_width = unit_width + fieldVar[5] + '|';
			quote_height = quote_height + fieldVar[6] + '|';
			unit_height = unit_height + fieldVar[7] + '|';
			quote_weight = quote_weight + fieldVar[8] + '|';
			quote_price = quote_price + fieldVar[9] + '|';
			quote_glass = quote_glass + fieldVar[10] + '|';
		}
	}

	var imagenum = 0;
	if (document.getElementById( 'imagenum' ) != null)
	{
		imagenum = document.getElementById( 'imagenum' ).value;
	}

	var filename = '';
	var destname = '';

	for (var i = 1; i <= imagenum; i++)
	{
		if (document.getElementById('filename' + i) != null)
		{
			fieldItem = document.getElementById('filename' + i).value;
			if (fieldItem != null)
			{
				filename = filename + fieldItem + '|';
				destname = destname + document.getElementById('destname' + i).value + '|';
			}
		}
	}

	while (pdescription.search('”') > -1)
	{
		pdescription = pdescription.replace('”', '"');
	}

	var Hear;
	var HearDesc = '';
	if (getCheckbox('Magazine'))
	{
		Hear = 'Magazine';
	}
	else if (getCheckbox('Newspaper'))
	{
		Hear = 'Newspaper';
	}
	else if (getCheckbox('Newspaper'))
	{
		Hear = 'Magazine';
	}
	else if (getCheckbox('Television'))
	{
		Hear = 'Television';
	}
	else if (getCheckbox('Tradeshow'))
	{
		Hear = 'Tradeshow';
	}
	else if (getCheckbox('Recommendation'))
	{
		Hear = 'Recommendation';
	}
	else if (getCheckbox('Google'))
	{
		Hear = 'Google';
	}
	else if (getCheckbox('Yahoo'))
	{
		Hear = 'Yahoo';
	}
	else if (getCheckbox('MSN'))
	{
		Hear = 'MSN';
	}
	else if (getCheckbox('AOL'))
	{
		Hear = 'AOL';
	}
	else if (getCheckbox('ASK'))
	{
		Hear = 'ASK';
	}
	else if (getCheckbox('OtherInternet'))
	{
		Hear = 'OtherInternet';
		HearDesc = getText('OtherInternetDesc')
	}
	else if (getCheckbox('Other'))
	{
		Hear = 'Other';
		HearDesc = getText('OtherDesc')
	}

	// Add Pickup and Delivery Location and Loading Dock - HieuNT - 20090210
	var puLoc = '', dLoc= '', puLoading = '', dLoading = '';
	if (getCheckbox('puLoc_res')) {
		puLoc = 'Residential';
	} else {
		puLoc = 'Commercial/ Business';

		if (getCheckbox('puLoading_yes')) {
			puLoading = 'Yes';
		} else {
			puLoading = 'No';
		}
	}
	if (getCheckbox('dLoc_res')) {
		dLoc = 'Residential';
	} else {
		dLoc = 'Commercial/ Business';

		if (getCheckbox('dLoading_yes')) {
			dLoading = 'Yes';
		} else {
			dLoading = 'No';
		}
	}


	var data = "tpe=" + tpe + "&cname=" + cname + "&cphone=" + cphone + "&cemail=" + cemail + "&cfax=" + cfax;
	data = data + "&puname=" + puname + "&puphone=" + puphone + "&puaddress=" + puaddress + "&pucity=" + pucity + "&puzip=" + puzip + "&puemail=" + puemail + "&pufax=" + pufax + "&pustate=" + pustate+"&pucountry="+pucountry;
	data = data + "&dname=" + dname + "&dphone=" + dphone + "&daddress=" + daddress + "&dcity=" + dcity + "&dzip=" + dzip + "&demail=" + demail + "&dfax=" + dfax + "&dstate=" + dstate+"&dcountry="+dcountry;
	data = data + "&bname=" + bname + "&bphone=" + bphone + "&baddress=" + baddress + "&bcity=" + bcity + "&bzip=" + bzip + "&paymenttype=" + paymenttype + "&bemail=" + bemail + "&bfax=" + bfax + "&bstate=" + bstate+"&bcountry="+bcountry;
	data = data + "&pdescription=" + pdescription + "&Packaging=" + Packaging + "&Crating=" + Crating + "&BlanketWrap=" + BlanketWrap + "&NeedsAssembly=" + NeedsAssembly + "&FlightCarry=" + FlightCarry + "&deductible=" + deductible + "&irvc=" + irvc;
	data = data + "&quote_quantity=" + quote_quantity + "&quote_description=" + quote_description + "&quote_length=" + quote_length + "&unit_length=" + unit_length + "&quote_width=" + quote_width + "&unit_width=" + unit_width + "&quote_height=" + quote_height + "&unit_height=" + unit_height + "&quote_weight=" + quote_weight + "&quote_price=" + quote_price + "&quote_glass=" + quote_glass;
	data = data + "&filename=" + filename + "&destname=" + destname;
	data = data + "&Hear=" + Hear + "&HearDesc=" + HearDesc;
	data = data + "&puLoc=" + puLoc + "&puLoading=" + puLoading + "&dLoc=" + dLoc + "&dLoading=" + dLoading;

	if (cemail == "hieunt1980@gmail.com_test")
	{
		//alert("&Hear=" + Hear + "&HearDesc=" + HearDesc);
		alert(quote_glass);
	}
	else
	{
		// Checking required fileds - HieuNT - 20090210
		var isMissing = false;
		var errMessage = '';
		if (cname == "")
		{
			isMissing = true;
			errMessage = "Missing Contact name";
		}
		if (cphone == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Contact phone";
		}
		if (cemail == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Contact email";
		}
		if (bname == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Billing name";
		}
		if (bphone == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Billing phone";
		}
		if (baddress == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Billing address";
		}
		if (bcity == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Billing city";
		}
		if (bstate == 0 && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Billing state";
		}
		if(bcountry==""&&!isMissing){
			isMissing = true;
			errMessag = errMessage + "<br/>"+"Missing Billing country";
		}
		if (bzip == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Billing zipcode";
		}
		if (paymenttype == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Payment Type";
		}
		if (puname == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Pick Up name";
		}
		if (puphone == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Pick Up phone";
		}
		if (puaddress == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Pick Up address";
		}
		if (pucity == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Pick Up city";
		}
		if (pustate == 0 && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Pick Up state";
		}
		if(pucountry=="" && !isMissing){
			isMissing = true;
			errMessage = errMessage +"<br/>"+"Missing Pick Up country";
		}
		if (puzip == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Pick Up zipcode";
		}
		if (dname == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Delivery name";
		}
		if (dphone == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Delivery phone";
		}
		if (daddress == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Delivery address";
		}
		if (dcity == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Delivery city";
		}
		if (dstate == 0 && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Delivery state";
		}
		if(dcountry=="" && !isMissing){
			isMissing = true;
			errMessage = errMessage  +"<br/>"+"Missing Delivery country";
		}
		if (dzip == "" && !isMissing)
		{
			isMissing = true;
			errMessage = errMessage + "<br>" + "Missing Delivery zipcode";
		}

		if (isMissing)
		{
			setMessage(errMessage);
		}
		else
		{
			xmlHttp.open("POST","quote_mail.php",true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", data.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(data);
		}
	}
}
