// JavaScript Document
//步驟一總驗證
function CheckRegistrationDataStep1( str_obj_name_1 )
{
	if(	ConfirmEmail( "member_email" , "member_retype_email" )
	 && CheckCompanyName( "member_company_name" )
	 && CheckContactPerson( "member_contact_person_first_name" , "member_contact_person_last_name" )
	 && CheckPosition( "member_position" )	 
	 && CheckTelephone( "member_tel" )
	 && CheckWebsite( "member_website" )
	 && CheckInterestProducts( str_obj_name_1 )
	 /*
	 CheckEmail( "member_email" )
	 */
	 /*
	 && CheckAddress( "member_addr_1" )
	 && CheckCity( "member_city" )
	 && CheckState( "member_state" )
	 && CheckPostalCode( "member_postal_code" )
	 */
	)
	{
		//alert(3);
		NextStep();
	}
}

//電子郵件驗證
function CheckEmail( str_obj_id , str_obj_name_1 )
{
	var obj_email = document.getElementById( str_obj_id );
	
	var reg_email = new RegExp( "[a-zA-Z0-9\._\+]{1,}@[a-zA-Z0-9]{1,}" );	
	
	var bool_email_available = false;
	
	if( trim( obj_email.value ).length > 0 )
	{	
		if( obj_email.value.match( reg_email ) )
		{			
			request = createRequest();
			var url = "ajax_member_exists.php";
			request.open( "POST" , url , true );
			request.onreadystatechange = function()
			{
				if( Response() )
				{
					var obj_xmldoc = request.responseXML;
					
					var str_result = obj_xmldoc.getElementsByTagName( "result" ).item(0).childNodes[0].nodeValue;
					
					if( str_result == "1" )
					{
						obj_email.style.backgroundColor = '';
						//bool_email_available = true;
						CheckRegistrationDataStep1( str_obj_name_1 );
					}
					else
					{
						str_message = obj_xmldoc.getElementsByTagName( "message" ).item(0).childNodes[0].nodeValue;
						
						obj_email.style.backgroundColor = '#66CCFF';
						if( confirm( str_message ) )
						{
							location.href = '../membership/forgotten_pw.php'
						}
						else
						{
							obj_email.focus();	
						}
						
						//bool_email_available = false;	
					}
				}
			};
			request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
			request.send( "member_id=" + obj_email.value );
		}
		else
		{
			obj_email.style.backgroundColor = '#66CCFF';
			obj_email.focus();
			//bool_email_available = false;	
		}
	}
	else
	{
		obj_email.style.backgroundColor = '#66CCFF';
		obj_email.focus();
		//bool_email_available = false;
	}
}

//電子郵件驗證
function ConfirmEmail( str_obj_id_1 , str_obj_id_2 )
{
	var obj_email_1 = document.getElementById( str_obj_id_1 );
	var obj_email_2 = document.getElementById( str_obj_id_2 );
	
	if( obj_email_2.value == obj_email_1.value )
	{
		obj_email_2.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_email_2.style.backgroundColor = '#66CCFF';
		obj_email_2.focus();
		return false;
	}
}

//公司名稱驗證
function CheckCompanyName( str_obj_id )
{
	var obj_company = document.getElementById( str_obj_id );

if( trim( obj_company.value ).length > 0 )
	{	
		obj_company.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_company.style.backgroundColor = '#66CCFF';
		obj_company.focus();
		return false;
	}
}

//聯絡人驗證
function CheckContactPerson( str_obj_id_1 , str_obj_id_2 )
{
	var obj_contact_person_first_name = document.getElementById( str_obj_id_1 );
	var obj_contact_person_last_name = document.getElementById( str_obj_id_2 );
	
	if( trim( obj_contact_person_first_name.value ).length > 0 )
	{	
		obj_contact_person_first_name.style.backgroundColor = '';
		
		if( trim( obj_contact_person_last_name.value ).length > 0 )
		{	
			obj_contact_person_last_name.style.backgroundColor = '';
			return true;	
		}
		else
		{
			obj_contact_person_last_name.style.backgroundColor = '#66CCFF';
			obj_contact_person_last_name.focus();
			return false;
		}
	}
	else
	{
		obj_contact_person_first_name.style.backgroundColor = '#66CCFF';
		obj_contact_person_first_name.focus();
		return false;
	}
}

//職稱驗證
function CheckPosition( str_obj_id )
{
	var obj_position = document.getElementById( str_obj_id );
	
	if( trim( obj_position.value ).length > 0 )
	{	
		obj_position.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_position.style.backgroundColor = '#66CCFF';
		obj_position.focus();
		return false;
	}
}

//網址驗證
function CheckWebsite( str_obj_id )
{
	var obj_website = document.getElementById( str_obj_id );
	
	if( trim( obj_website.value ).length > 0 )
	{	
		obj_website.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_website.style.backgroundColor = '#66CCFF';
		obj_website.focus();
		return false;
	}
}
/*
//公司網站驗證
function CheckWebsite( str_obj_id )
{
	var obj_website = document.getElementById( str_obj_id );
	
	if( trim( obj_website.value ).length > 0 )
	{	
		obj_website.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_website.style.backgroundColor = '#66CCFF';
		obj_website.focus();
		return false;
	}
}
*/
//聯絡電話驗證
function CheckTelephone( str_obj_id )
{
	var obj_tel = document.getElementById( str_obj_id );
	
	//var reg_tel = new RegExp( "^[0-9]{1,}-[0-9]{1,}-[0-9]{1,}" );
	var reg_tel = /^[0-9|\+|\-|\#|\(|\)|\s]+$/i;
	
	if( trim( obj_tel.value ).length > 0 )
	{	
		if( trim( obj_tel.value ).match( reg_tel ) )
		{
			obj_tel.style.backgroundColor = '';
			return true;
		}
		else
		{
			obj_tel.style.backgroundColor = '#66CCFF';
			obj_tel.focus();
			return false;	
		}
	}
	else
	{
		obj_tel.style.backgroundColor = '#66CCFF';
		obj_tel.focus();
		return false;
	}
}

//公司地址驗證
function CheckAddress( str_obj_id )
{
	var obj_address = document.getElementById( str_obj_id );
	
	if( trim( obj_address.value ).length > 0 )
	{	
		obj_address.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_address.style.backgroundColor = '#66CCFF';
		obj_address.focus();
		return false;
	}
}

//城市驗證
function CheckCity( str_obj_id )
{
	var obj_city = document.getElementById( str_obj_id );
	
	if( trim( obj_city.value ).length > 0 )
	{	
		obj_city.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_city.style.backgroundColor = '#66CCFF';
		obj_city.focus();
		return false;
	}
}

//行政區驗證
function CheckState( str_obj_id )
{
	var obj_state = document.getElementById( str_obj_id );
	
	if( trim( obj_state.value ).length > 0 )
	{	
		obj_state.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_state.style.backgroundColor = '#66CCFF';
		obj_state.focus();
		return false;
	}
}

//郵遞區號驗證
function CheckPostalCode( str_obj_id )
{
	var obj_postal_code = document.getElementById( str_obj_id );
	
	if( trim( obj_postal_code.value ).length > 0 )
	{	
		obj_postal_code.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_postal_code.style.backgroundColor = '#66CCFF';
		obj_postal_code.focus();
		return false;
	}
}


//步驟二總驗證
function CheckRegistrationDataStep2()
{
	if(	CheckKnowMethod( "know_method" , "know_method_specify" )
	 && CheckPreferences()
	 && CheckContactYou( "contact_you" )
	 && CheckAppIntendTo( "app_intend_to" , "app_intend_specify" ) 
	 && CheckInquiringProductPurpose( "inquiring_product_purpose" , "purpose_specify" )
	 && CheckWhenNeedProductFor( "when_need_product_for" , "when_need_product_specify" )
	)
	{
		NextStep();
	}
	else
	{
		//alert( "false" );
	}
}

//得知LTW的方法驗證
function CheckKnowMethod( str_obj_id_1 , str_obj_id_2 )
{
	var obj_know_method = document.getElementById( str_obj_id_1 );
	var obj_know_method_specify = document.getElementById( str_obj_id_2 );

	if( obj_know_method.value == 0 )
	{				
		obj_know_method.style.backgroundColor = '#66CCFF';
		obj_know_method.focus();
		return false;
	}
	else
	{
		obj_know_method.style.backgroundColor = '';
		
		if( trim( obj_know_method_specify.value ).length > 0 )
		{
			obj_know_method_specify.style.backgroundColor = '';
			return true;
		}
		else
		{
			obj_know_method_specify.style.backgroundColor = '#66CCFF';
			obj_know_method_specify.focus();
			return false;
		}
	}
}

//LTW聯絡你的人員驗證
function CheckContactYou( str_obj_id )
{
	var obj_contact_you = document.getElementById( str_obj_id );

	if( obj_contact_you.value == 0 )
	{				
		obj_contact_you.style.backgroundColor = '#66CCFF';
		obj_contact_you.focus();
		return false;
	}
	else
	{
		obj_contact_you.style.backgroundColor = '';
		return true;
	}
}

//個人偏好驗證
function CheckPreferences()
{
	if(	CheckCatalog( "receive_catalog" )
	 ||	CheckNewsletter( "receive_newsletter" )
	 ||	CheckHotDeal( "inform_hot_deal" )
	 || Check2DDrawing( "2d_drawing" )
	 || CheckAssemblyInstruction( "assembly_instruction" )
	// ||	CheckMembership( "consider_designer_forum" )
	 || CheckNoneAbove( "none_of_the_above" )
	)
	{
		return true;
	}
	else
	{
		//alert( "Specify Your Preferences (How do you want to be informed with LTW), please" );
		
		document.getElementById( "none_of_the_above" ).checked = true;
		
		return true;
	}
}

//Catalog
function CheckCatalog( str_obj_id )
{
	var obj_catalog = document.getElementById( str_obj_id );
	
	return obj_catalog.checked;
}

//電子報
function CheckNewsletter( str_obj_id )
{
	var obj_newsletter = document.getElementById( str_obj_id );
	
	return obj_newsletter.checked;
}

//Hot Deal
function CheckHotDeal( str_obj_id )
{
	var obj_hotdeal = document.getElementById( str_obj_id );
	
	return obj_hotdeal.checked;
}

//Hot Deal
function Check2DDrawing( str_obj_id )
{
	var obj_2d_drawing = document.getElementById( str_obj_id );
	
	return obj_2d_drawing.checked;
}

//Hot Deal
function CheckAssemblyInstruction( str_obj_id )
{
	var obj_assembly_instruction = document.getElementById( str_obj_id );
	
	return obj_assembly_instruction.checked;
}

//Designer Forum membership
function CheckMembership( str_obj_id )
{
	var obj_membership = document.getElementById( str_obj_id );
	
	return obj_membership.checked;
}

//none of the above
function CheckNoneAbove( str_obj_id )
{
	var obj_none_above = document.getElementById( str_obj_id );
	
	return obj_none_above.checked;
}

//設定偏好設定資料
function SetPreferencesByNoneAbove()
{
	var obj_catalog = document.getElementById( "receive_catalog" );
	var obj_newsletter = document.getElementById( "receive_newsletter" );
	var obj_hotdeal = document.getElementById( "inform_hot_deal" );
	/*var obj_membership = document.getElementById( "consider_designer_forum" );*/
	var obj_2d_drawing = document.getElementById( "2d_drawing" );
	var obj_assembly_instruction = document.getElementById( "assembly_instruction" );
	var obj_none_above = document.getElementById( "none_of_the_above" );
	
	if( obj_none_above.checked == true )
	{
		obj_catalog.checked = false;
		obj_newsletter.checked = false;
		obj_hotdeal.checked = false;
		/*obj_membership.checked = false;*/
		obj_2d_drawing.checked = false;
		obj_assembly_instruction.checked = false;
	}
}

//設定偏好設定資料
function SetPreferencesByOtherOfNoneAbove()
{
	var obj_catalog = document.getElementById( "receive_catalog" );
	var obj_newsletter = document.getElementById( "receive_newsletter" );
	var obj_hotdeal = document.getElementById( "inform_hot_deal" );
	/*var obj_membership = document.getElementById( "consider_designer_forum" );*/
	var obj_2d_drawing = document.getElementById( "2d_drawing" );
	var obj_assembly_instruction = document.getElementById( "assembly_instruction" );
	var obj_none_above = document.getElementById( "none_of_the_above" );
	
	if( obj_catalog.checked == true
	 ||	obj_newsletter.checked == true
	 ||	obj_hotdeal.checked == true
	 ||	obj_2d_drawing.checked == true
	 ||	obj_assembly_instruction.checked == true
	/* ||	obj_membership.checked == true */
	)
	{		
		obj_none_above.checked = false;
	}
}

//會員有興趣的產品驗證
function CheckInterestProducts( str_obj_name_1 )
{
	var arr_interest_products = document.getElementsByName( str_obj_name_1 );
	var bool_interest_products_selected = false;
		
	for( i = 0 ; i < arr_interest_products.length ; i++ )
	{
		if( arr_interest_products[i].checked )
		{
			bool_interest_products_selected	 = true;
			break;
		}			
	}
	
	if( !bool_interest_products_selected )
	{
		alert( "Select What products are you interested in, please" );
		return false;
	}
	else
	{
		return true;	
	}
}

//產品應用驗證
function CheckAppIntendTo( str_obj_id_1 , str_obj_id_2 )
{
	var obj_app_intend_to = document.getElementById( str_obj_id_1 );
	var obj_app_intend_specify = document.getElementById( str_obj_id_2 );

	if( obj_app_intend_to.value == 0 )
	{				
		obj_app_intend_to.style.backgroundColor = '#66CCFF';
		obj_app_intend_to.focus();
		return false;
	}
	else
	{
		obj_app_intend_to.style.backgroundColor = '';
		
		if( trim( obj_app_intend_specify.value ).length > 0 )
		{
			obj_app_intend_specify.style.backgroundColor = '';
			return true;
		}
		else
		{
			obj_app_intend_specify.style.backgroundColor = '#66CCFF';
			obj_app_intend_specify.focus();
			return false;
		}
	}
}

//詢問產品用意驗證
function CheckInquiringProductPurpose( str_obj_id_1 , str_obj_id_2 )
{
	var obj_inquiring_product_purpose = document.getElementById( str_obj_id_1 );
	var obj_purpose_specify = document.getElementById( str_obj_id_2 );

	if( obj_inquiring_product_purpose.value == 0 )
	{				
		obj_inquiring_product_purpose.style.backgroundColor = '#66CCFF';
		obj_inquiring_product_purpose.focus();
		return false;
	}
	else
	{
		obj_inquiring_product_purpose.style.backgroundColor = '';
		
		if( trim( obj_purpose_specify.value ).length > 0 )
		{
			obj_purpose_specify.style.backgroundColor = '';
			return true;
		}
		else
		{
			obj_purpose_specify.style.backgroundColor = '#66CCFF';
			obj_purpose_specify.focus();
			return false;
		}
	}
}

//詢問產品用意驗證
function CheckWhenNeedProductFor( str_obj_id_1 , str_obj_id_2 )
{
	var obj_when_need_product_for = document.getElementById( str_obj_id_1 );
	var obj_when_need_product_specify = document.getElementById( str_obj_id_2 );

	if( obj_when_need_product_for.value == 0 )
	{				
		obj_when_need_product_for.style.backgroundColor = '#66CCFF';
		obj_when_need_product_for.focus();
		return false;
	}
	else
	{
		obj_when_need_product_for.style.backgroundColor = '';
		
		if( trim( obj_when_need_product_specify.value ).length > 0 )
		{
			obj_when_need_product_specify.style.backgroundColor = '';
			return true;
		}
		else
		{
			obj_when_need_product_specify.style.backgroundColor = '#66CCFF';
			obj_when_need_product_specify.focus();
			return false;
		}
	}
}

//--------------------------------------------------------------------------
//回至上一步驟
function PreviousStep()
{
	request = createRequest();
	var url = "ajax_previous.php";
	request.open( "POST" , url , true );
	request.onreadystatechange = function()
	{
		if( Response() )
		{
			document.getElementById( "registration_form" ).submit();
		}
	};
	request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
	request.send( null );
}

//下一步驟
function NextStep()
{
	request = createRequest();
	var url = "ajax_next.php";
	request.open( "POST" , url , true );
	request.onreadystatechange = function()
	{
		if( Response() )
		{
			document.getElementById( "registration_form" ).submit();
		}
	};
	request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
	request.send( null );
}
//--------------------------------------------------------------------------

function CheckResendActiveEMail()
{
	var obj_org_email = document.getElementById( "member_org_email" );
	var obj_first_name = document.getElementById( "member_contact_person_first_name" );
	var obj_last_name = document.getElementById( "member_contact_person_last_name" );
	var obj_new_email = document.getElementById( "member_new_email" );
	var obj_verifying_code = document.getElementById( "verifying_code" );
	
	if( trim( obj_org_email.value ).length > 0 )
	{
		obj_org_email.style.backgroundColor = '';
		
		if( trim( obj_first_name.value ).length > 0 )
		{
			obj_first_name.style.backgroundColor = '';
			
			if( trim( obj_last_name.value ).length > 0 )
			{
				obj_last_name.style.backgroundColor = '';
				
				if( trim( obj_new_email.value ).length > 0 )
				{
					obj_new_email.style.backgroundColor = '';				
					
					if( trim( obj_verifying_code.value ).length > 0 )
					{
						obj_verifying_code.style.backgroundColor = '';	
						
						document.getElementById( "resend_active_email_form" ).submit();
					}
					else
					{
						obj_verifying_code.style.backgroundColor = '#66CCFF';
						obj_verifying_code.focus();
					}
				}
				else
				{
					obj_new_email.style.backgroundColor = '#66CCFF';
					obj_new_email.focus();
				}	
			}
			else
			{
				obj_last_name.style.backgroundColor = '#66CCFF';
				obj_last_name.focus();
			}
		}
		else
		{
			obj_first_name.style.backgroundColor = '#66CCFF';
			obj_first_name.focus();
		}	
	}
	else
	{
		obj_org_email.style.backgroundColor = '#66CCFF';
		obj_org_email.focus();
	}
}
//--------------------------------------------------------------------------


function CheckInquiringForm()
{
	if( CheckInquiringContactPerson( "inquiring_contact_person" )	 
	 && CheckCompanyName( "inquiring_company_name" )	 
	 && CheckPosition( "inquiring_position" )
	 && CheckWebsite( "inquiring_website" )
	 && CheckInquiringEmail( "inquiring_email" ) 
	 //&& CheckAddress( "inquiring_address" )
	 && CheckTelephone( "inquiring_tel" )
	 && CheckInquiringMessage( "inquiring_message" )
	)		
	{
		document.getElementById( "sales_inquiring_form" ).submit();
	}
}

//聯絡人驗證
function CheckInquiringContactPerson( str_obj_id )
{
	var obj_contact_person = document.getElementById( str_obj_id );
	
	if( trim( obj_contact_person.value ).length > 0 )
	{
		obj_contact_person.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_contact_person.style.backgroundColor = '#66CCFF';
		obj_contact_person.focus();
		return false;
	}
}

//電子郵件驗證
function CheckInquiringEmail( str_obj_id )
{
	var obj_email = document.getElementById( str_obj_id );
	
	if( trim( obj_email.value ).length > 0 )
	{
		obj_email.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_email.style.backgroundColor = '#66CCFF';
		obj_email.focus();
		return false;
	}
}

//詢問內容驗證
function CheckInquiringMessage( str_obj_id )
{
	var obj_message = document.getElementById( str_obj_id );
	
	if( trim( obj_message.value ).length > 0 )
	{
		obj_message.style.backgroundColor = '';
		return true;	
	}
	else
	{
		obj_message.style.backgroundColor = '#66CCFF';
		obj_message.focus();
		return false;
	}
}
//--------------------------------------------------------------------------

//會員升級總驗證
function CheckUpgradeData( str_obj_name_1 )
{
	if(	CheckKnowMethod( "know_method" , "know_method_specify" )
	 && CheckPreferences()
	 && CheckContactYou( "contact_you" )
	 //&& CheckInterestProducts( str_obj_name_1 )
	 && CheckAppIntendTo( "app_intend_to" , "app_intend_specify" ) 
	 && CheckInquiringProductPurpose( "inquiring_product_purpose" , "purpose_specify" )
	 && CheckWhenNeedProductFor( "when_need_product_for" , "when_need_product_specify" )
	)
	{
		NextUpgradeStep();
	}
	else
	{
		//alert( "false" );
	}
}

//--------------------------------------------------------------------------
//下一步驟
function NextUpgradeStep()
{
	request = createRequest();
	var url = "ajax_upgrade_next.php";
	request.open( "POST" , url , true );
	request.onreadystatechange = function()
	{
		if( Response() )
		{
			document.getElementById( "registration_form" ).submit();
		}
	};
	request.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
	request.send( null );
}
//--------------------------------------------------------------------------


