/******************************************************************************
 * Module: my.js
 * Author: Scott Wang
 * Email: scott@bmalloc.com
 * Date: May 20, 07
 * Purpose: Common Javascript functions
 *****************************************************************************/

function replace_field(div_name,field)
{
	var ni = document.getElementById(div_name);
	//var newdiv = document.createElement('div');
    ni.innerHTML = field;
	//ni.appendChild(newdiv);
}

function add_field(div_name,field)
{
	var ni = document.getElementById(div_name);
	var newdiv = document.createElement('div');
	//var divIdName = 'my'+i+'Div';
	//newdiv.setAttribute('id',divIdName);
	newdiv.innerHTML = field;
	ni.appendChild(newdiv);
	//i++;
}

function rusure()
{
	return confirm("Are you sure to delete this entry?");
}

function docopy()
{
	var fields = new Array("name","email","address","phone",
	                       "city","provence","country");
	for (i=0;i< fields.length;i++){
		document.getElementsByName("shipping_" + fields[i])[0].value 
		  = document.getElementsByName(fields[i])[0].value;
	}
}
//add entry to select 
function add_entry(obj)
{
	var new_option = prompt("Please enter a new price type");
	new_option = new_option.replace(/^\s+|\s+$/g, '');
	if(new_option && new_option != ''){
		var select_obj = document.getElementById(obj);
		var tmp = document.createElement('option');
		tmp.text = new_option;
		tmp.value = new_option;
		select_obj.add(tmp);
		select_obj.selectedIndex = select_obj.length-1;
	}
} 

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//Note: No space allow in title and attr
//$pop_window_attr = "menubar=no,scrollbars=no,status=no,width=300,height=300,top=150,left=120";
function just_open_it(url,title, attr){
	window.open(url, title, attr);
}

function go(){
	var source_addr = document.getElementById('source_addr').value;
	var dest_addr = document.getElementById('dest_addr').value;
	var url = 'http://maps.google.com/maps?saddr=' 
						+ source_addr + '&daddr=' + dest_addr;
	window.open(url);
}

function change_image(direction){
	var images = document.getElementsByName('images');
	var image_index = parseInt(document.getElementById('image_index').value);
	var directions = new Array();
	directions['prev'] = -1;
	directions['next'] = 1;
	image_index += directions[direction];
	if(image_index < 1){
		image_index = images.length;
	}else if(image_index > images.length){
		image_index = 1;
	}
	
	var image_obj = document.getElementById('image_src');
	image_obj.src = images[image_index-1].value;
	document.getElementById('image_index').value = image_index + '';
}

function change_div_contents(new_div,old_div){
	document.getElementById(new_div).innerHTML
		 = document.getElementById(old_div).innerHTML;
}