/**
 *
 * @Author Sam.zhang 
 * @Date   2007-11-6
 */
var actionPage="global_supplier_apply.php";
var targetPage="global_supplier_apply.html?action=f";

function check_service_name(){
	if(assembleParam(new Array("service_name"))==""){
		alert($("error_service_name").innerHTML);
		return false;
	}
	return true;
}

function check_company_name(){
	if($("company_name").value.trim()==""){
		alert($("error_company_name").innerHTML);
		return false;
	}
	return true;
}
function check_company_catagory(){
	if($("company_catagory").value.trim()==""){
		alert($("error_company_catagory").innerHTML);
		return false;
	}
	return true;
}

function check_country(){
	if($("country").value==""){
		alert($("error_country").innerHTML);
		return false;
	}
	return true;
}

function check_contact_person(){
	if($("contact_person").value.trim()==""){
		alert($("error_contact_person").innerHTML);
		return false;
	}
	return true;
}

function check_tel(){
	var tel2="";
	if ($('tel2').value != '') {
		if (/0[1-9]{0,3}$/.test($('tel2').value)) {
			tel2 = $('tel2').value.substr(1, $('tel2').value.length);
		}
	}
	
	tel2 = tel2 ? tel2 : "";
	
	var str_tel = String.format( "{0}-{1}-{2}" , $('tel1').value.trim() , tel2.trim() , $('tel3').value.trim() );
	
	if( /[0-9]{1,4}\-[0-9]{0,4}\-[0-9\-\*\s\#]{6,20}$/.test( str_tel ) ) {
		return true;
	}else{
		alert($("error_tel").innerHTML);
		return false;
	}

}

function check_fax(){
	var fax2="";
	if ($('fax2').value != '') {
		if (/0[1-9]{0,3}$/.test($('fax2').value)) {
			fax2 = $('fax2').value.substr(1, $('fax2').value.length);
		}
	}
	
	fax2 = fax2 ? fax2 : "";
	
	var str_fax = String.format( "{0}-{1}-{2}" , $('fax1').value.trim() , fax2.trim() , $('fax3').value.trim() );
	
	if( /[0-9]{1,4}\-[0-9]{0,4}\-[0-9\-\*\s\#]{6,20}$/.test( str_fax ) ) {
		return true;
	}else{
		alert($("error_fax").innerHTML);
		return false;
	}

}

function check_email(){
	if(!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test($("email").value.trim())){
		alert($("error_email").innerHTML);
		return false;
	}
	return true;
}

function check_company_address(){
	if($("company_address").value.trim()==""){
		alert($("error_company_address").innerHTML);
		return false;
	}
	return true;
}

function check_city(){
	if($("city").value.trim()==""){
		alert($("error_city").innerHTML);
		return false;
	}
	return true;
}

function check_state(){
	if($("state").value.trim()==""){
		alert($("error_state").innerHTML);
		return false;
	}
	return true;
}

function check_zip(){
	if($("zip").value.trim()==""){
		alert($("error_zip").innerHTML);
		return false;
	}
	return true;
}


function submit_data(){
	var errs=new Array();
/*
	errs.push(check_name());
	errs.push(check_keywords());
	errs.push(check_catagory());
	errs.push(check_desc());
	errs.push(check_pack_type());
	errs.push(check_quantity());
	errs.push(check_brief());
*/
	$("company_catagory").value=$("txt_catagory_path").innerHTML;
	
	if(check_service_name()
		&& check_company_name()
		&& check_company_catagory()
		&& check_country()
		&& check_contact_person()
		&& check_tel()
//		&& check_fax()
		&& check_email()
		&& check_company_address()
//		&& check_city()
		&& check_state()
//		&& check_zip()
	){
		
	}else{
		return false;
	}
	
	$("btn_submit").disabled=true;

	errorCodes=implode(",",errs);


	if(errorCodes==""){
		var varObjs=new Array(
						"service_name",
						"member_id",
						"company_catagory",
						"country",
						"contact_person",
						"company_name",
						"gender",
						"email",
						"tel1",
						"tel2",
						"tel3",
						"fax1",
						"fax2",
						"fax3",
						"company_address",
						"city",
						"state",
						"zip",
						"action"
						);


		/* 开始提交数据 */
		new Ajax.Request(actionPage, {
			method: 'post',
			parameters:assembleParam(varObjs),
			onSuccess: function(transport) {
				switch(parseInt(transport.responseText,10)){
					case 0:
//						alert($("apply_success").innerHTML);
						$("btn_submit").disabled=true;
						window.location.href=targetPage;
//						history.back();
//						guide("save_success",targetPage);
					break;
					case 1:
						alert($("apply_fail").innerHTML);
						$("btn_submit").disabled=false;
//						guide("save_fail");
					break;
					default:
//						guide("save_fail");
				}
/*				guide(transport.responseText,targetPage); */
			}
		});
		return true;
	}

	guide(errorCodes);
	return false;
}
 
function pre_submit(){
	$("company_catagory").value=$("txt_catagory_path").innerHTML;
	return true;
}

function choose_catagory(){
	 cfg_catagory_dialog.idAction='hdn_catagory_id';
	 cfg_catagory_dialog.txtAction='txt_catagory_path';
	 cfg_catagory_dialog.nodeSelectable=false;
	 cfg_catagory_dialog.levelLimit=2;
	 open_catagory_dialog("p_key1");
}

