function createOptions( begin, end, selected ){
	for ( i=begin; i<=end; i++ ){
		if( i==selected)
			document.write("<option selected value="+i+">"+i+"</option>")
		else	
			document.write("<option value="+i+">"+i+"</option>")
	}
}
function createOptionsFromArray( arrs, selected ){
	createOptionsFrom2Arrays( arrs, arrs, seleted )
}
function createOptionsFrom2Arrays( values, displays, selected ){
	for ( i=0; i<=values.length; i++ ){
		if( arrs[i]==selected)
			document.write("<option selected value='"+values[i]+"'>"+displays[i]+"</option>")
		else	
			document.write("<option value='"+values[i]+"'>"+displays[i]+"</option>")
	}
}

function getTimezoneOffset(){ 
   return( new Date().getTimezoneOffset() ) 
} 

function selectDatetime(d) {
  var arg = new Object();
  arg.str_datetime = d;
  arg.time_comp = true;
  if( window.showModalDialog ){
	var rtn = window.showModalDialog(
	'/static/calendar_en.html',
	arg,
	'dialogWidth=210px;dialogHeight=240px;status:no;scroll=no;'
	);
  return (rtn == null ? "" : rtn);
  }
  else{
	  alert("Please use IE6+ or firefox3.0' . Or you can directly input the datetime, format is 'YYYY-MM-DD HH:mm:ss'")
  }
}


function valideForm( frm ){
    warning=''  
    if( frm.name.value=='' )
     	warning+='You should give a name to this timer.\n'
	
    if( frm.localStartTime.value=='' )
     	warning+="You should set the timer's start time.\n"
	
    if( frm.loopType[1].checked ){
    	if( frm.intervalSeconds.value=='') 
     		warning+='You should set the interval time for this loop timer.\n'
     	else if(  frm.intervalSeconds.value.search(/\D/) >=0  )
     		warning+='Interval time value should be an integer.\n'
     	else if(  frm.intervalSeconds.value<60  )
     		warning+='Interval time value should be larger than 60.\n'
	}
    if( frm.type[0].checked ){
        if( frm.url.value=='') 
            warning+='You should set the URL address.\n'    
        else if( ! frm.url.value.match(/https?:\/\//i)) 
            warning+='URL should start with http:// or https:// \n'    
    }	
    else if( frm.type[1].checked ){
    	if( frm.mailSubject.value=='') 
     		warning+='You should set the email mailSubject.\n'  
    	if( frm.mailTo.value=='' || frm.mailTo.value.indexOf('@') < 0) 
     		warning+='You should set the email receiver(mailTo) correctly.\n'      
    }
    if( warning=='' )
		return true;
	
   	alert(warning);
   	return false; 
}

function ajax_submit( key ){
//ajax submit form.
    //alert( key )
	frm = document.getElementById('form_'+key)
	if( frm==null ){
		alert("no form find: "+'form_'+key);
		return false
		}
	if( valideForm( frm ) ){
		ajaxAnywhere.formName='form_'+key
		ajaxAnywhere.submitAJAX(); return false;
	
	}
	return false;
}

function updateTime(f,s) {
   if( f.value.length==19 ){
		l=f.value; 

		d = new Date(l.substring(0,4),
		eval( l.substring(5,7)-1),
		l.substring(8,10),
		l.substring(11,13),
		l.substring(14,16),
		l.substring(17,19) ) 
		s.value=d.getUTCFullYear()+"-" + 
		eval(d.getUTCMonth()+1)+"-"+ 
		d.getUTCDate()+" "+ 
		d.getUTCHours()+":"+ 
		d.getUTCMinutes  ()+":"+ 
		d.getUTCSeconds () 
   }
}

function change( div1, div2){   
	if( document.getElementById(div1) !=null )
		document.getElementById(div1).style.display='block'
	if( document.getElementById(div2) !=null )
		document.getElementById(div2).style.display='none'   
}

function showTask( key, loopType, type ){   
	taskDiv=document.getElementById('div_'+key)
	if( taskDiv.style.display=='block' )
		taskDiv.style.display='none';
	else {
		taskDiv.style.display='block' 		
		loopDiv=document.getElementById('loop_'+key) 
 
		urlDiv=document.getElementById('url_'+key)
		emailDiv=document.getElementById('email_'+key)
		if( type=='url' ){
			urlDiv.style.display='block';
			emailDiv.style.display='none';
		}
		else{
			urlDiv.style.display='none';
			emailDiv.style.display='block';
		} 
	}
	return false;
}