// JavaScript Document

/*Ajax section */
function goForm(formId,targetChange)
{
	url=$(formId).getProperty("action");
	var request = new Request.HTML({url:url,
								   onSuccess: function(html)
								   {
										//Clear the text currently inside the results div.			
										$(targetChange).set('text','');
										$(targetChange).adopt(html);
								    }
								}).post($(formId));
}

function checkForm(formId)
{
	r=0;
	msg="";
	elements=$$("#"+formId+" .man");
	elements.each(function(ele,index){
		if(ele)
		{
			val=ele.value.trim();
			if(val=='' || val==null)
			{
				msg="请填妥所有项目";
				r=1; // Means value is NULL
				ele.focus();
			}
			else if(ele.hasClass('more6'))
			{
				if(val.length<6)
				{
					msg="请输入6位或以上的字母或数字";
					r=2;
					ele.focus();
				}
			}
			else if(ele.hasClass('check_ra'))
			{
				if(!ele.checked)
				{
					msg="请先阅读有关资料";
					r=3;
					ele.focus();
				}
			}
			else if(ele.hasClass('check_email'))
			{
				var pattern=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
				if(!pattern.test(val))
				{
					r=4;
					msg="请填写正确Email";
					ele.focus();
				}
			}
				
			
		}
		
		
	});

	if(r>0)
		alert(msg);
	
	return (r==0);
}

function openSmallWindow(link)
{
	window.open(link,"Ng","height=200,width=300,toolbar= no, menubar=no, scrollbars=no, resizable=no, location=no, status=no");
}

function myrefresh()
{
	window.top.location.reload();
}

/// TO handle photos
function displayPhotos(photos_id,path,position_id)
{
	photos=document.getElementById(photos_id).value;
	if(photos.length<=4)
		document.getElementById(photos_id).value='';
	if(photos.indexOf(",")==0)
	{
		photos=photos.substr(1,photos.length);
	}
	if(photos.indexOf(",")==photos.length)
	{
		photos=photos.substr(0,photos.length-1);
	}
	arr=photos.split(",");
	content="";
	for(i=0;i<arr.length;i++)
	{
		content+="<a href='#' onclick=\"removePhoto('"+photos_id+"','"+path+"',"+i+",'"+position_id+"')\"><img src='"+path+arr[i]+"' width='80' /></a>";
	}
	document.getElementById(position_id).innerHTML=content;
}

function removePhoto(photos_id,path,tag,position_id)
{
	if(confirm("Sure to remove this record ?"))
	{
		photos=document.getElementById(photos_id).value;
		if(photos.indexOf(",")==0)
		{
			photos=photos.substr(1,photos.length);
		}
		if(photos.indexOf(",")==photos.length)
		{
			photos=photos.substr(0,photos.length-1);
		}
		arr=photos.split(",");
		if(arr[tag])
		{
			//splice
			arr.splice(tag,1);
		}
		t=arr.join();
		if(t.length<=1)
			t='';
		else
			t=','+t;
		//alert(arr.join());
		document.getElementById(photos_id).value=t;
		displayPhotos(photos_id,path,position_id);
	}
	
}

function showMap(address)
{
	var map=null;
	var marker=null;
	var map = null;
	var geocoder = null;
    if (GBrowserIsCompatible())
    {
	    map = new GMap2(document.getElementById("map_canvas"));
	    map.setCenter(new GLatLng(25.050719, 121.471434), 13);
	    map.addControl(new GSmallMapControl());
 		map.addControl(new GMapTypeControl());
	    geocoder = new GClientGeocoder();
    }
    
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    
}

function changePage(path,target,page,type,tpage)
{
	now_page=$(type+"_page").value;
	if(page=='go')
		now_page++;
	else if(page=='back')
		now_page--;
	else
		now_page=page;
	now_page=(now_page>tpage)?1:now_page;
	now_page=(now_page<1)?tpage:now_page;
	var myRequest = new Request({method: 'post', url: path,
	onSuccess:function(responseText, responseXML){
		$(target).set("html",responseText);
		$(type+"_page").value=now_page;
	}
	});
	myRequest.send("type="+type+"&page="+now_page);
	
	
	if(type=='char')  //pro
		prefix="c_";
	else
		prefix="p_";
	areaid=prefix+"area";
	
	
	pageid=prefix+"page_"; // + ???
	setclass(areaid,$(pageid+now_page));
	/*
	nid="";
	pid="";
	nowId="";
	total=$$("#"+areaid+" a").length;
	$$("#"+areaid+" a").each(function(ele,i){
		if(ele.hasClass("on"))
		{
			tag=i+1;
			pid=pageid+(tag-1);
			nid=pageid+(tag+1);
			if(tag>=total)
				nid=pageid+"1";
			else if(tag<=1)
				pid=pageid+total;
		}
	});
	*/
	return false;
}

function setclass(area_id,ele)
{
	a="#"+area_id+" a";
	$$(a).each(function(e,i){
		e.removeClass("on");
	});
	ele.set("class","on");
}
