
function display(i)
{
	document.getElementById(i).style.display="block";
}

function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}
function shutup_class(all)
{
	arrElements=getElementsByClass(all,null,null);
	for(var i=0; i<arrElements.length; i++)
	{
		arrElements[i].style.display="none";
	}	
}

//flag 0 use a pre-created project id, flag is 1 use a selected project id
function get_photo_data(flag)
{
    var photo_num = document.getElementById('photo_num').value;
    if(flag == 0)
    {
        var project_name = encodeURIComponent(document.getElementById('project_name').value);
        var project_id = encodeURIComponent(document.getElementById('project_id').value);
        var data="project_id="+project_id+"&project_name="+project_name;
    }else {
			var delete_project_id=encodeURIComponent(document.getElementById('project_id').value);
        var list = document.getElementById('project_list');
        var project_id = list.options[list.options.selectedIndex].value
        var data="project_id="+project_id+"&delete_tmp_project="+delete_project_id;       
    }
   
    data +="&photo_num="+photo_num+"&";   
    for(var i=0; i < photo_num; i++)
    {
        var id = "photo_id_"+i;
        var title = "photo_title_"+i;
        var photo_id = document.getElementById(id).value;
        var photo_title = encodeURIComponent(document.getElementById(title).value);
        data += id+"="+photo_id+"&"+title+"="+photo_title+"&";
    }
    return data;
}

function GetRadioValue(RadioName){
    var obj;   
    obj=document.getElementsByName(RadioName);
    if(obj!=null){
        var i;
        for(i=0;i<obj.length;i++){
            if(obj[i].checked){
                return obj[i].value;           
            }
        }
    }
    return null;
}


function get_project_price(total_credits)
{
    var photo_num = document.getElementById('photo_number').value;
	
	var credit=0;
	var photo=0;
	var reference=0;
    for(var i=1; i <= photo_num; i++)
    {
        var title = "photo_service_"+i;
        var photo_service = GetRadioValue(title); 
		if(photo_service=='deluxe')
		{
			credit=credit+4;
			photo++;
		}
		else
		{
			if(photo_service=='extensive')
			{
				credit=credit+2;
				photo++;
			}
			else
			{
				if(photo_service=='basic')
				{
					credit=credit+1;
					photo++;
				}
				else if(photo_service=='reference')
				{
					reference++;
				}
			}
		}
        
    }
	if(credit==1)
		credit_unit=' Credit';
	else
		credit_unit=' Credits';

	if(photo==1)
		photo_unit=' Photo';
	else
		photo_unit=' Photos';

	if(credit>0 && photo>0)
	{
		display('order_submit');
		if(reference==1)
			document.getElementById(total_credits).innerHTML="<div class='normal'><b>Service Ordered:</b> "+photo+photo_unit+"<br><b>Total Cost:</b> "+credit+credit_unit+"</div><div class='hint'><b>Note:</b> <span style='font-size:11px;color:#888;'>Reference pictures are only for reference, and <b>no</b> editing service will be provided for the photo which you set as Reference Picture. If you need us to edit it, please don't set it as Reference Picture.</span></div>";
		else if(reference>1)
			document.getElementById(total_credits).innerHTML="<div class='normal'><b>Service Ordered:</b> "+photo+photo_unit+"<br><b>Total Cost:</b> "+credit+credit_unit+"</div><div class='hint'><b>Note:</b> <span style='font-size:11px;color:#888;'>Reference pictures are only for reference, and <b>no</b> editing service will be provided for the <b style='color:#333;'> "+reference+" photos</b> which you set as Reference Picture. If you need us to edit them, please don't set them as Reference Picture.</span></div>";
		else
			document.getElementById(total_credits).innerHTML="<b>Service Ordered:</b> "+photo+photo_unit+"<br><b>Total Cost:</b> "+credit+credit_unit;
	}
	else
	{
		shutup('order_submit');
		document.getElementById(total_credits).innerHTML="<b>No service ordered for any photo</b>";
	}

}

function photo_batch(url, target_url, flag)
{
    fadein('saving');
    var xmlHttp=GetXmlHttpObject();
    var project_name = document.getElementById('project_name').value;
    var photo_data = get_photo_data(flag);
//    alert(url+photo_data);
    var project_id = 0;
    if(flag==0)    {
        project_id=document.getElementById('project_id').value;
    }else {
        var list = document.getElementById('project_list');
        project_id = list.options[list.options.selectedIndex].value
    }
   
    document.getElementById('btnCreate').disabled = true;
    document.getElementById('btnSave').disabled = true;

    if(xmlHttp)
    {
        xmlHttp.onreadystatechange=function()
        {
            if(xmlHttp.readyState==4)
            {
                ret = xmlHttp.responseText;
//                alert(ret);
                location.href=target_url+project_id+'/';
            }
        }
        xmlHttp.open("POST",url,true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(photo_data);
        return 1;
    }
    else
    {
        alert('unsupported browser');//document.getElementById(stamp).innerHTML="unsupported browser";
        return -1;
    }
}
function direct_import(base_url, upload_bar, imported_hint )
{
	url=encodeURIComponent(document.getElementById('image_url').value);
	post_url = base_url+"?import="+url;
	import_photo(post_url, upload_bar, imported_hint) ;
}
function import_photo(url, upload_bar, imported_hint )
{
	var xmlHttp=GetXmlHttpObject();
	display(upload_bar);
	if(xmlHttp)
	{
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				ret = xmlHttp.responseText;
				if(ret.indexOf('.jpg')>1)
				{
					if(imported_hint)
					{
						shutup(upload_bar);
						fadein(imported_hint);
						document.getElementById('imported_hint_img').innerHTML="<img src="+ret+" />";
					}
				}
				else
				{
					shutup(upload_bar);
					fadein('error_hint');				
				}

			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
	    alert('unsupported browser');
	    return -1;
	}	
}

function grid_edit(i,text)
{
	o=document.getElementById(text);
	v = o.value;
	if(v=="")
		v = v+i;
	else
		v = v+"\n"+i;
	o.value=v;
	if(o.style.display!="none")
		o.focus();
}



function scratchin(id)
{
	var o = document.getElementById(id);
    var i=0;
	var j=0;
	if(o!=null)
	{
		function change(){
			if(i>=100)
				i=100;
			else
			{
				i+=5;
			   o.style.filter = "Alpha(Opacity=" + i + ")"; //for IE    
				o.style.opacity = i/100; //for FF
			}
		}

		if(i<100)
			window.setInterval(change,3);
	}
}

function fadein(id)
{
	var o = document.getElementById(id);
	
	var h = o.style.height;
    var i=0;	
	if( (o) && o.style.display!="block")
	{
	   o.style.filter = "Alpha(Opacity=0)"; //for IE    
		o.style.opacity = 0; //for FF	
		o.style.display="block";
		function change(){
			if(i>=100)
				i=100;
			else
			{
				i+=20;
			   o.style.filter = "Alpha(Opacity=" + i + ")"; //for IE    
				o.style.opacity = i/100; //for FF
			}

		}

		if(i<100)
			window.setInterval(change,2);

	}
}

function fadeout(id)
{
	var o = document.getElementById(id);
    var i=100;	
	if(o)
	{
	   o.style.filter = "Alpha(Opacity=100)"; //for IE    
		o.style.opacity = 1; //for FF	
		function change(){
			if(i<5)
			{
				i=0;
				shutup(id);
				o.style.display="none";
			}
			else
			{
				i=i-10;
			   o.style.filter = "Alpha(Opacity=" + i + ")"; //for IE    
				o.style.opacity = i/100; //for FF
			}

		}

		if(i>5)
			window.setInterval(change,2);
		else
		{
				shutup(id);
				o.style.display="none";
		}		
			

	}
}


function scroll_left(id,start_point,distance)
{
	var o = document.getElementById(id);
	var i=start_point;


	o.style.left = i+'px';
	if(o!=null)
	{
		function change()
		{
			if(i<=distance)
			{
				i=distance;				
			}
			else
			{
				delta=(i-distance)/6;
				if(delta<0.5)
					delta=0.5;
				i-=delta;
				o.style.left = i+'px' ;    
			}
		}
	}

	if(i>distance)
		window.setInterval(change,1);

	
}

function scroll_right(id,start_point,distance)
{
	var o = document.getElementById(id);
	var i=start_point;

	o.style.left = parseInt(i)+'px';
	if(o!=null)
	{
		function change()
		{
			if(i>=distance)
			{
				i=distance;
			}
			else
			{
				delta=(distance-i)/6;
				if(delta<0.5)
					delta=0.5;
				i+=delta;
				o.style.left = i+'px' ;    
			}
		}
	}

	if(i<distance)
		window.setInterval(change,1);
}

function index_show_next(gg)
{
	var mm=parseInt(document.getElementById('demo_current').value);
	
	demo_old='demo'+mm;
			
	if(mm==gg)
		NO=1;
	else
		NO=mm+1;

	document.getElementById('demo_current').value=NO;
	
	demo_new='demo'+NO;
	document.getElementById(demo_new).style.display='block';
	//alert(mm);
	scroll_left(demo_old,0,'-587');
	scroll_left(demo_new,587,0);

}

function index_show_prev(gg)
{
	var mm=parseInt(document.getElementById('demo_current').value);
	
	demo_old='demo'+mm;
			
	if(mm==1)
		NO=gg;
	else
		NO=mm-1;

	document.getElementById('demo_current').value=NO;
	
	demo_new='demo'+NO;
	document.getElementById(demo_new).style.display='block';
	//alert(mm);
	scroll_right(demo_old, 0 ,587);
	scroll_right(demo_new, -587 ,0);
	

}

var STOP; 

function show_demo()
{
	var mm=parseInt(document.getElementById('demo_current').value);
	
	index_show_next(5);

	STOP=setTimeout("show_demo()",3000);

}
function show_demo_stop()
{
	clearTimeout(STOP);
}
function changeAgin()
{
	show_demo();
}



function hightlight(id,color)
{
	o=document.getElementById(id);
	o.style.border="1px solid "+color;
	o.style.zindex="1000";
}
function unhightlight(id,color)
{
	o=document.getElementById(id);
	o.style.border="1px solid "+color;
	o.style.zindex="2";
}
function  show_border (i,all)
{

	for(j=1;j<=all;j++)
	{
		if(j!=i)
			document.getElementById(j).style.border="none";
	}
	document.getElementById(i).style.border="2px solid #F5B73E";	
}

function  upload_alert (i,msg,l,lmsg,upload_bar)
{
	if(document.getElementById(i).value=="")
	{
		alert (msg);
		return false;
	}
	else
	{
		if(document.getElementById(l).checked!=true)
		{
			alert (lmsg);
			return false;
		}	
		else
		{
			display(upload_bar);
			return true;
		}
	}
}


function do_inner(i,j)
{
	document.getElementById(i).style.display = "block";
	document.getElementById(i).innerHTML=document.getElementById(j).innerHTML;
}


function set_class_bg(all,color)
{
	arrElements=getElementsByClass(all,null,null);
	for(var i=0; i<arrElements.length; i++)
	{
		arrElements[i].style.backgroundColor=color;
	}	
}

function set_id_bg(id,color)
{

	document.getElementById(id).style.backgroundColor=color;
}


function do_ajax(property, id)
{
	var xmlHttp=GetXmlHttpObject();
	var url="/do_ajax.php?"+property+"&rand="+Math.random();

	//var rbox=document.getElementById(id);
	//rbox.innerHTML="<img src='http://www.tucia.com/img/ajax_load_tiny.gif' />";

	if(xmlHttp)
	{
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				
				if(property.indexOf("faq_add_view")>0)
				{
					set_id_bg(id, '#fbffcc');
				}
				
				//rbox.innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
		//rbox.innerHTML="error";
	}
}




function display_shutup(i)
{
	var o=document.getElementById(i);
	if(o.style.display!="block")
		o.style.display="block";
	else
		o.style.display="none";
}

function display_select(i,width)
{
	document.getElementById(i).style.width=width;
}

function checkbox_display(check,i)
{
	v=document.getElementById(check).checked;
	if(v)
		document.getElementById(i).style.display="block";
	else
		document.getElementById(i).style.display="none";
}

function checkbox_shutup(check,i)
{
	v=document.getElementById(check).checked;
	if(v)
		document.getElementById(i).style.display="none";
	else
		document.getElementById(i).style.display="block";
}


function shutup(i)
{
	var o=document.getElementById(i);
	if(o)
		o.style.display="none";
}

function shutup2(i,j)
{
	document.getElementById(i).style.display="none";
	document.getElementById(j).style.display="none";
}

function switch_button(self,i,value,cancel)
{
	if(document.getElementById(self).value!=cancel)
	{
		fadein(i);
		document.getElementById(self).value=cancel;
	}
	else
	{
		document.getElementById(i).style.display="none";
		document.getElementById(self).value=value;
	}

}


function server_origin_swicth(pic,server)
{
	p=document.getElementById(pic);
	s=document.getElementById(server);

	if(s.style.display!="block")
	{
		s.style.display="block";
		p.style.display="none";
		document.getElementById("grid_bottom").style.display="none";
		document.getElementById("grid_box").style.display="none";	
		document.getElementById("grid_switch").value="显示网格";	
	
	}
	else
	{
		s.style.display="none";		
		p.style.display="block";
		document.getElementById("grid_bottom").style.display="block";

	}
}


function   check_num(NUM)  
{  
    var i,strTemp,flag=true;  
    strTemp="0123456789";  
    if (NUM.length==0)
		flag=false;
	else
	{
		for   (i=0;i<NUM.length;i++)  
		{  
			if(strTemp.indexOf(NUM.charAt(i))==-1)  
			flag=false;   
		} 
		return flag;
	}
}  


function GetXmlHttpObject()
{
	var xmlHttp=null; 
	if (window.XMLHttpRequest)// Moz, FF, NN, Op
	{
	  xmlHttp = new XMLHttpRequest(); 
	}
	
	else if(window.ActiveXObject)//IE
	{
	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	}
	return xmlHttp;
}


function grid_resizing(url,grid_box,type,click,width,height)
{
	var xmlHttp=GetXmlHttpObject();
	var url=url+"?type="+type+"&click="+click+"&width="+width+"&height="+height+"&rand="+Math.random();
	if(xmlHttp)
	{
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				document.getElementById(grid_box).innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
		document.getElementById(grid_box).innerHTML="unsupported browser";
	}
}

