﻿var IE = document.all?true:false; // Just IE detector.

var brLine = "\r\n";
var loca = location.href.toString()
var currentLang = (loca.search('lang=0')!=-1 || loca.search('lang')==-1)?'en':'tc';
var currentSection = loca;
currentSection = currentSection.match(/section=\d*/);
if(currentSection == null){
	currentSection = 'nth';
}else{
	currentSection = currentSection[0].replace('section=', '');
}
if(loca.search('action=thankyou')!=-1){
	if(currentLang == 'en'){
		alert('Thank you for your enquiry. We shall contact you soon.');
	}else{
		alert('多謝您的查詢，我們會盡快與您聯絡。');
	}
}

$(function() {
	var thePath = location.href.match(/^.*\/AsiaSat/);
	
	if(loca.search('=80')!=-1){
		//alert($('input[@name*="earth_lat"]').length);
		if($('input[@name*="earth_lat"]').length == 1){
			$('input[@name*="earth_lat"]').parent('td').parent('tr').prev().after('<tr><td class="text" valign="top" bgcolor="#fffef2" bordercolor="#fffef2" id="dropdown1"></td><td class="text" valign="top" bgcolor="#fffef2" bordercolor="#fffef2" id="dropdown2"></td></tr>');
			if(currentLang=='en'){
				$('#dropdown1').append('Country <select id="dropDownCountry"></select>&nbsp;');
				$('#dropdown2').append('City <select id="dropDownCity"></select>');
			}else{
				$('#dropdown1').append('國家 <select id="dropDownCountry"></select>');
				$('#dropdown2').append('城市 <select id="dropDownCity"></select>');
			}
			$.ajax({
				type: "GET",
				url: "country_and_city.php",
				data: 'lang='+((currentLang=='en')?0:1)+'&type=country',
				dataType: "xml",
				success: function(xml){
					if(currentLang=='en'){
						$('#dropDownCountry').append('<option value="----">-- Please Select --</option>');
					}else{
						$('#dropDownCountry').append('<option value="----">-- 請選擇 --</option>');
					}
					$(xml).find('country').each(function(){
						$('#dropDownCountry').append('<option value="'+$(this).find('id').text()+'">'+$(this).find('caption').text()+'</option>');
					});
					$('#dropDownCountry').bind('change', function(){
						 getCitiesByCountryId($(this).val());
					});
					$('input[@name="earth_lat"]').val('');
					$('input[name="esl"]').removeAttr('checked');
					$('input[@name="earth_lon"]').val('');
				},
				fail: function(){
					alert('error');
				}
			});
		}
	}
	function getCitiesByCountryId(countryID){
		$.ajax({
			type: "GET",
			url: "country_and_city.php",
			data: 'lang='+((currentLang=='en')?0:1)+'&type=city&country='+countryID,
			dataType: "xml",
			success: function(xml){
				$('#dropDownCity').empty();
				if(currentLang=='en'){
					$('#dropDownCity').append('<option value="----">-- Please Select --</option>');
				}else{
					$('#dropDownCity').append('<option value="----">-- 請選擇 --</option>');
				}
				$(xml).find('city').each(function(){
					$('#dropDownCity').append('<option value="longitude:'+$(this).find('longitude').text()+'+latitude:'+$(this).find('latitude').text()+'">'+$(this).find('caption').text()+'</option>');
				});
				$('#dropDownCity').bind('change', function(){
					if($('#dropDownCity').val()!='----'){
						fillIn($('#dropDownCity').val());
					}
				});
				$('input[@name="earth_lat"]').val('');
				$('input[name="esl"]').removeAttr('checked');
				$('input[@name="earth_lon"]').val('');
			},
			fail: function(){
				alert('error');
			}
		});
	}
	function fillIn(theValue){
		var laAnLo = theValue.split('+');
		var latVal = laAnLo[1].replace('latitude:', '');
		$('input[@name="earth_lat"]').val(latVal);
		$('input[name="esl"][value="'+((latVal*1>=0)?"north":"south")+'"]').attr('checked', 'checked');
		$('input[@name="earth_lon"]').val(laAnLo[0].replace('longitude:', ''));
	}

    $('table').attr('border', 0);
	
	$('img[@class^="btn_o"]').each(function(){
		var theImgType = $(this).attr('src').toString().match(/\.[a-z][a-z][a-z][a-z]?$/);
		var theExt = $(this).attr('class').toString().match(/btn_[a-zA-Z0-9]*/gi).toString().replace('btn', '');
		$(this).mouseover(function(){
			$(this).attr('src', $(this).attr('src').toString().replace(''+theImgType,''+theExt+theImgType));
	    }).mouseout(function(){
			$(this).attr('src', $(this).attr('src').toString().replace(''+theExt+theImgType,''+theImgType));
	    });
	});
	
	$('img').each(function (){
		if($(this).attr('alt')){
			if($(this).attr('src').toString().search('btn_top')!=-1){
				$(this).attr('alt', ((currentLang=='en')?'Top':'頁首'));
				$(this).attr('title', ((currentLang=='en')?'Top':'頁首'));
			}
		}else if ($(this).attr('id') != "Image321" && $(this).attr('id') != "Image331"){
				$(this).attr('alt', ((currentLang=='en')?'images':'圖片'));
				$(this).attr('title', ((currentLang=='en')?'images':'圖片'));
			}
	});
	//alert($('#mainContent table[bgcolor="#dceed7"] tbody tr').find('td[bgcolor]').length)
	$('#mainContent table[bgcolor="#dceed7"] tbody tr td:not([bgcolor])').each(function(){
		if(typeof $(this).attr('style') == 'undefined'){
			$(this).css('background-color', '#FFFFFF');
			$(this).css('border', '#ffffff 1px solid');
		}
	});
	$('#mainContent ul li').each(function (){
		if($(this).parents().filter('ul').length == 1){
			$(this).wrapInner('<div style="padding: 0px 0px 0px 0px; float:left; text-align:left;width:90%;"></div>');
			$(this).prepend('<div style="float:left; width:10; padding: 0px 3px 0px 0px;"><img src="/asiasat/images/bullet_news.gif" title="*" alt="*" align="left" vspace="2"></div>');
		}else{
			$(this).parent().parent().prevAll().children().filter('img').attr('src', '/asiasat/images/bullet_news.gif');
			$(this).wrapInner('<div style="padding: 0px 0px 0px 0px; float:left; text-align:left;width:90%;"></div>');
			$(this).prepend('<div style="float:left; width:10; padding: 0px 3px 0px 0px;"><img src="images/bullet_sitemap.gif" title="*" alt="*" align="left" vspace="2"></div>');
		}
	});
	
	var satellite_fleet_btn_path = new Array();
	$('td.satellite_fleet_btn').each(function(){
		if (currentLang=='en'){
			satellite_fleet_btn_path[$(this).children('a').html().toString().match(/\d/)] = $(this).children('a').attr('href');
		}else if (currentLang=='tc'){
			satellite_fleet_btn_path[tcNo2enNo($(this).children('a').html().toString())] = $(this).children('a').attr('href');
		}
	});
	var footPrintCoords = {};
	$('area').each(function(){
		eval('footPrintCoords.'+$(this).attr('id')+' = "'+$(this).attr('coords')+'"');
		$(this).attr('target','_self');
		$(this).attr('href', satellite_fleet_btn_path[$(this).attr('id')]);
	});
	$('#main_foot_print').attr('usemap', '\#map2');
	
	$('td.satellite_fleet_btn').children().mouseover(function(){
		$(this).css({ cursor:"hand" });
		if (currentLang=='en'){
			$('#main_foot_print').attr('src', $('#main_foot_print').attr('src').toString().replace(/_0[2345]\./,'_0'+$(this).html().toString().match(/\d/)+'.'));
			if(IE){
				$('map:eq(0)').empty();
				$('map:eq(0)').append('<area alt="More Details" title="More Details" id="s'+$(this).html().toString().match(/\d/)+'" target="_self" href="'+satellite_fleet_btn_path[$(this).html().toString().match(/\d/)]+'" coords="'+eval('footPrintCoords.s'+$(this).html().toString().match(/\d/))+'">');
			}else{
				$('#main_foot_print').attr('usemap', '\#map'+$(this).html().toString().match(/\d/));
			}
			$(this).removeAttr('href');
		}else if (currentLang=='tc'){ 
			$('#main_foot_print').attr('src', $('#main_foot_print').attr('src').toString().replace(/_0[2345]\./,'_0'+tcNo2enNo($(this).html().toString())+'.'));
			if(IE){
				$('map:eq(0)').empty();
				$('map:eq(0)').append('<area alt="更多詳細資料" title="更多詳細資料" id="s'+tcNo2enNo($(this).html().toString())+'" target="_self" href="'+satellite_fleet_btn_path[tcNo2enNo($(this).html().toString())]+'" coords="'+eval('footPrintCoords.s'+tcNo2enNo($(this).html().toString()))+'">');
			}else{
				$('#main_foot_print').attr('usemap', '\#map'+tcNo2enNo($(this).html().toString()));
			}
			$(this).removeAttr('href');
		}
	});
	
	// Pop-up defined size window for satellite coverage map
	$('td.satellite_fleet_btn').children().click(function(){
		if (currentLang=='en'){
			// jQuery plugin
			$.jqURL.loc(currentLang.toUpperCase()+'/upload/images/satellite_fleet/footprint_sat_0'+$(this).html().toString().match(/\d/)+'pop.jpg',{w:783,h:440,wintype:'_blank'});
		}
		else if (currentLang=='tc'){ 
			$.jqURL.loc(currentLang.toUpperCase()+'/upload/images/satellite_fleet/footprint_sat_0'+tcNo2enNo($(this).html().toString())+'pop.jpg',{w:783,h:440,wintype:'_blank'});
		}
	});
	
	$('div[id^="left_level_2_"]').mouseover(function(){
		$('div[id^="left_level_3_"]').each(function(){
			$(this).css('display', 'none');
		});
		var tempid = $(this).attr("id").toString().match(/\d$/);
		var temp = "#left_level_3_" + tempid;
		if(document.getElementById("left_level_3_" + tempid) == null){
		}else{
			$(temp).css('top', $(this).offset().top - (IE?3:0));
			$(temp).css('left', $(this).offset().left + $(this).width() + 7 - (IE?3:0));
			$(temp).css('display', 'inline');
		}
    });
});

$(document).mouseup(function(){
	$('div[id^="left_level_3_"]').each(function(){
		$(this).css('display', 'none');
	});
});

function tcNo2enNo(tcnoStr){
	if (tcnoStr.search('二')!=-1)
		return 2;
	else if (tcnoStr.search('三')!=-1)
		return 3;
	else if (tcnoStr.search('四')!=-1)
		return 4;
	else if (tcnoStr.search('五')!=-1)
		return 5;
	else return 0;
}
function submitContactUs(){
	var go = '';
	if (currentLang == 'en'){
		if(typeof $("input[@name=R1]:checked").val() == 'undefined'){ go += '- Please select your saluation.\n';}
		if(checknoscriptANDhavestr($("input[@name=name1]").val().toString())){ go += '- Please enter your first name.\n';}
		if(checknoscriptANDhavestr($("input[@name=name2]").val().toString())){ go += '- Please enter your last name.\n';}
		if(checknoscript($("input[@name=title]").val().toString())){ go += '- Title is not in good format\n';}
		if(checknoscriptANDhavestr($("input[@name=company]").val().toString())){ go += '- Please enter your company name.\n';}
		if(checknoscript($("textarea[@name=address]").val().toString())){ go += '- Address is not in good format\n';}
		if(checknoscriptANDhavestr($("input[@name=city]").val().toString())){ go += '- Please enter city.\n';}
		if(checknoscript($("input[@name=other]").val().toString())){ go += '- Specified country is not in good format\n';}

		if(checknoscript($("input[@name=phone_country]").val().toString())){ go += '- Phone - Country code is not in good format\n';}
		if(checknoscript($("input[@name=phone_area]").val().toString())){ go += '- Phone - Area code is not in good format\n';}
		if(checknoscript($("input[@name=phone]").val().toString())){ go += '- Phone - Phone No. is not in good format\n';}

		if(checknoscript($("input[@name=fax_country]").val().toString())){ go += '- Fax - Country code is not in good format\n';}
		if(checknoscript($("input[@name=fax_area]").val().toString())){ go += '- Fax - Area code is not in good format\n';}
		if(checknoscript($("input[@name=fax]").val().toString())){ go += '- Fax - Fax No. is not in good format\n';}

		/*** Filter out phone and fax to allow integer ONLY. ***/
		//$("input[@name=phone_country]").val($("input[@name=phone_country]").val().toString().replace(/\D/gi,''));
		//$("input[@name=phone_area]").val($("input[@name=phone_area]").val().toString().replace(/\D/gi,''));
		$("input[@name=phone]").val($("input[@name=phone]").val().toString().replace(/\D/gi,''));
		//$("input[@name=fax_country]").val($("input[@name=fax_country]").val().toString().replace(/\D/gi,''));
		//$("input[@name=fax_area]").val($("input[@name=fax_area]").val().toString().replace(/\D/gi,''));
		$("input[@name==fax]").val($("input[@name=fax]").val().toString().replace(/\D/gi,''));

		if(checknoscript($("input[@name=website]").val().toString())){ go += '- Company Web Site is not in good format\n';}

		if($("select[@name=Country] option:selected").val() == 0){ go += '- Please select the country.\n';}
		if(!checkemail($("input[@name=email]").val().toString())){ go += '- Please enter a valid email.\n';}
		if(checknoscriptANDhavestr($("textarea[@name=question]").val().toString())){ go += '- Please enter your questions / suggestions.\n';}
		if(($("textarea[@name=question]").val().toString().length > 2000)){ go += '- Questions / suggestions should not more than 2000 characters.\n';}
		if(($("textarea[@name=address]").val().toString().length > 2000)){ go += '- Address should not more than 2000 characters.\n';}
	}else if (currentLang == 'tc'){
		if(typeof $("input[@name=R1]:checked").val() == 'undefined'){ go += '- Please select your saluation.\n';}
		if(checknoscriptANDhavestr($("input[@name=name1]").val().toString())){ go += '- Please enter your name.\n';}

		if(checknoscript($("input[@name=title]").val().toString())){ go += '- Title is not in good format\n';}
		if(checknoscriptANDhavestr($("input[@name=company]").val().toString())){ go += '- Please enter your company name.\n';}
		if(checknoscript($("textarea[@name=address]").val().toString())){ go += '- Address is not in good format\n';}
		if(checknoscriptANDhavestr($("input[@name=city]").val().toString())){ go += '- Please enter city.\n';}

		if(checknoscript($("input[@name=phone_country]").val().toString())){ go += '- Phone - Country code is not in good format\n';}
		if(checknoscript($("input[@name=phone_area]").val().toString())){ go += '- Phone - Area code is not in good format\n';}
		if(checknoscript($("input[@name=phone]").val().toString())){ go += '- Phone - Phone No. is not in good format\n';}

		if(checknoscript($("input[@name=fax_country]").val().toString())){ go += '- Fax - Country code is not in good format\n';}
		if(checknoscript($("input[@name=fax_area]").val().toString())){ go += '- Fax - Area code is not in good format\n';}
		if(checknoscript($("input[@name=fax]").val().toString())){ go += '- Fax - Fax No. is not in good format\n';}

		/*** Filter out phone and fax to allow integer ONLY. ***/
		//$("input[@name=phone_country]").val($("input[@name=phone_country]").val().toString().replace(/\D/gi,''));
		//$("input[@name=phone_area]").val($("input[@name=phone_area]").val().toString().replace(/\D/gi,''));
		$("input[@name=phone]").val($("input[@name=phone]").val().toString().replace(/\D/gi,''));
		//$("input[@name=fax_country]").val($("input[@name=fax_country]").val().toString().replace(/\D/gi,''));
		//$("input[@name=fax_area]").val($("input[@name=fax_area]").val().toString().replace(/\D/gi,''));
		$("input[@name==fax]").val($("input[@name=fax]").val().toString().replace(/\D/gi,''));

		if(checknoscript($("input[@name=website]").val().toString())){ go += '- Company Web Site is not in good format\n';}
		if(checknoscriptANDhavestr($("input[@name=Country]").val().toString())){ go += '- Please enter country.\n';}
		if(!checkemail($("input[@name=email]").val().toString())){ go += '- Please enter a valid email.\n';}
		if(checknoscriptANDhavestr($("textarea[@name=question]").val().toString())){ go += '- Please enter your questions / suggestions.\n';}
		if(($("textarea[@name=question]").val().toString().length > 2000)){ go += '- Questions / suggestions should not more than 2000 characters.\n';}
		if(($("textarea[@name=address]").val().toString().length > 2000)){ go += '- Address should not more than 2000 characters.\n';}
	}
	
	if(go.length == 0){
		$('form').append('<input type="hidden" name="fromsection" value="'+currentSection+'" />');
		document.form.method = 'POST';
		document.form.action = 'mail.php';
		document.form.submit();
	}else{
		go = 'Please follow:\n\n' + go;
		alert(go);
	}
	return false;
}

function checkemail(str){
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true		
}

function checknoscriptANDhavestr(source){
	var patt1 = new RegExp(/<([^>]*)[/script ]([^<]*)/gi);
	return (patt1.test(source) || source.length == 0);
}

function checknoscript(source){
	var patt1 = new RegExp(/<([^>]*)[/script ]([^<]*)/gi);
	return patt1.test(source);
}

function keyControl(me,e,condition){ // onkeypress="return keyControl(this,event,/[^A-Za-z 0-9]/gi);"
	var keynum;
	var keychar;
	var numcheck;

	if(window.event){
		keynum = e.keyCode;
	}else if(e.which){
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);
	numcheck = condition;
	return !numcheck.test(keychar);
}