// JavaScript Document
/*
googlemap mod
get direction
*/
/****GOOGLEMAP****/
function get_direction(frm){
	url = "http://maps.google.com/maps?";
	url = url + "f=" + frm.f.value;
	url = url + "&hl=" + frm.hl.value;
	url = url + "&saddr=" + frm.saddr.value;
	url = url + "&daddr=" + frm.daddr.value;
	document.getElementById('google_map').src = url;
	
	/*alert(document.getElementById('google_map').offsetTop);
	alert(document.getElementById('google_map').offsetHeight);*/
}
/***********/
function _onFocus(obj){
	obj.style.background = "#DADDB5";
}
function _lostFocus(obj){
	obj.style.background = "#FFFFFF";
}
/**/

function open_popup(url,popup_name,width,height,left,top){
	window.open(url,popup_name, 'height='+height+', width='+width+', left='+left+',top='+top+',toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no');
}
function validateEmail(email) {
	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	if (!re_mail.test(email.value)) {
		return false;
	}
	return true;
}
function onlyNumber(){
	if((event.keyCode<48)||(event.keyCode>57))
		event.returnValue=false;
}
function validNumber(number) {
	var re_number = /^([0-9])$/;
	if (!re_number.test(number.value)) {
		return false;
	}
	return true;
}
// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
	var value1 = record1.optText.toLowerCase();
	var value2 = record2.optText.toLowerCase();
	if (value1 > value2) return(1);
	if (value1 < value2) return(-1);
	return(0);
}

// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
	var value1 = record1.optText.toLowerCase();
	var value2 = record2.optText.toLowerCase();
	if (value1 > value2) return(-1);
	if (value1 < value2) return(1);
	return(0);
}

function sortSelect(selectToSort, ascendingOrder) {
	if (arguments.length == 1) ascendingOrder = true;    // default to ascending sort

	// copy options into an array
	var myOptions = [];
	for (var loop=0; loop<selectToSort.options.length; loop++) {
		myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
	}

	// sort array
	if (ascendingOrder) {
		myOptions.sort(sortFuncAsc);
	} else {
		myOptions.sort(sortFuncDesc);
	}

	// copy sorted options from array back to select box
	selectToSort.options.length = 0;
	for (var loop=0; loop<myOptions.length; loop++) {
		var optObj = document.createElement('option');
		optObj.text = myOptions[loop].optText;
		optObj.value = myOptions[loop].optValue;
		selectToSort.options.add(optObj);
	}
}
/*thanhnv 13Aug07*/
function validate(){
	return true;
}


function price_format(obj){
	//alert(obj.value);
	var $strInput = obj.value;
	
	$pos = $strInput.indexOf('.');
	if($pos > 0){
		$int = $strInput.substr(0,$pos);
		$float = $strInput.substr($pos,$strInput.length-$pos);
		$strInput = $int;
	}
	
	$numOuput = "";
	for(var $i = 0; $i < $strInput.length; $i++){
		$currChar = $strInput.charCodeAt($i);
		if($currChar >= 48 && $currChar <= 57)
			$numOuput += $strInput.charAt($i);
	}
	
	var strPrice = $numOuput;
	$price = "";
	
	$num = 0;
	$dec = 0;
	for(var $i=strPrice.length -1; $i >=0; $i--){
		/*if($dec == 2){
			$price =  strPrice.charAt($i) + "," + $price;  
			$num = 0;
		}
		else if ($num % 3 == 0 && $num > 0 ){
			$price =  strPrice.charAt($i) + "," + $price;  
		}else{
			$price = strPrice.charAt($i) + $price;
		}*/
		if ($num % 3 == 0 && $num > 0 ){
			$price =  strPrice.charAt($i) + "," + $price;  
		}else{
			$price = strPrice.charAt($i) + $price;
		}
			
		$dec++;
		$num++;
	}
	
	/*if($pos > 0) $price = $price+$float;*/
	
	obj.value = $price;
	return true;
}
/*function price_format(obj){
	//var $strInput = obj.value.replace(/.00/g,"");
	var $strInput = obj.value;
	
	$numOuput = "";
	
	for(var $i = 0; $i < $strInput.length; $i++){
		$currChar = $strInput.charCodeAt($i);
		if(($currChar >= 48 && $currChar <= 57) || ($currChar == 46))
			$numOuput += $strInput.charAt($i);
	}
	//var strPrice = obj.value.replace(/,/g,"");
	
	var strPrice = $numOuput;
	$price = "";
	
	$num = 0;
	$pos = strPrice.indexOf('.');
	if($pos > 0){
		$int = strPrice.substr(0,$pos);
		$float = strPrice.substr($pos,strPrice.length-$pos);
		strPrice = $int;
	}
	$found_point = false;
	for(var $i=strPrice.length -1; $i >=0; $i--){
		if ($num % 3 == 0 && $num > 0 )
			$price =  strPrice.charAt($i) + "," + $price;  
		else
			$price = strPrice.charAt($i) + $price;
		
		if(strPrice.charCodeAt($i) == 46)
			$found_point = true;
			
		if(!$found_point)
			$num++;
	}
	if($pos > 0) $price = $price+$float;
	//if(obj.name != "millage" && $price != "") $price += ".00";
	
	obj.value = $price;
	return true;
}*/


/*13Aug07- remove to common funtion*/
function ShowModel(id){
 if (document.getElementById){
        target = document.getElementById( id );
           if (target.style.display == "none"){
              target.style.display = "";
           } else {
              target.style.display = "none";
           }
     }
}

function flipDivs(cname,id)
{
if (id==null || id=="") id="00";

        // get all div elements
        var divs = document.getElementsByTagName('div');
        // with each div...
        for (var i = 0, j = divs.length; i < j; i++) {
                // alias this div
                var div = divs[i];
                // if this div contains the target class, hide this div
                if (div.className && div.className.indexOf(cname) != -1) div.style.display = 'none';
        }
        // if the id argument exists...
        if (id) {
                // alias the target div
                var div = document.getElementById(id);
                // if the target div exists...
                if (div) {
                        // tell this div to appear
                        div.style.display = 'block';
                }				
        }
        // prevent page from going anywhere
        return false;
}

/******COKKIE*****/
function eraseCookie(name){
	createCookie(name,"",-1);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*14Sep07*/
function check_all(obj, objImage) {
	if (obj) {
		if (obj.length) {
			value = obj[0].checked==false?true:false;
			
			for (i=0;i<obj.length;i++)
				obj[i].checked = value;
		}
		else {
			value = obj.checked==false?true:false;
			obj.checked = value;
		}
		/**/
		objImage.src = (value) ? srcImageClearAll : srcImageCheckAll; 
	}
}
/*10Jan08*/
function getFileExtension($file){
	return ($file.substring($file.lastIndexOf('.') + 1)).toUpperCase();
}