function isValidPhoneNumber(phoneNumber)
{
	var regPhone = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
    if (phoneNumber.match(regPhone)){
    	return true;
    }
    else {
        return false;
    }
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
}
function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
		if(arr[index] == ele)
			found = true;
		else
			index++;
	return found;
}
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
	return index;
}

/* trim function  */
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
		while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
		} //End While
	return strTemp;
} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

function CurrencyFormatted(amount)
{
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function CommaFormatted(amount)
{
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.',2);
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if(n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length < 1) { amount = n; }
    else {
        if(parseInt(d) == 0){
        	amount = n;
        } else {
        	amount = n + '.' + d;
        }

    }
    amount = minus + amount;
    return amount;
}

function formatMoneyValue(amount)
{
	var amount1 = CurrencyFormatted(amount);
	var amount2 = CommaFormatted(amount1);
	return "$ " + amount2 + " USD";
}

function changePhoto(img_name,img_src)
{
    document[img_name].src=img_src;
}

function showPopupImage(thiswidth,thisheight,thisURL) {
    var theWIN;
	var thisWIN;
	var thisURL;
	var thiswidth;
	var thisheight;
	thisWIN = self;
	if (thisWIN.name == "theImg") { theWIN = thisWIN }
	if (!theWIN || theWIN.closed) {
		var x = parseInt(window.screen.availWidth)/2-parseInt(thiswidth+10)/2;
        var y = parseInt(window.screen.availHeight)/2-parseInt(thisheight+10)/2;
        theWIN = window.open(thisURL,"theImg","toolbar=0,location=0,directories=0,status=0,scrollbars=no,resizable=no,copyhistory=0 ,width=" + thiswidth + ",height=" + thisheight + ",top="+y+",left="+x,screeny=0,screenx=0);
 	    theWIN.focus() ;
    } else {
	    theWIN.location.href = thisURL ;
        theWIN.resizeTo(thiswidth,thisheight) ;
	}
}

function showPopupWindow(thiswidth,thisheight,thisURL, winName) {
    var theWIN;
	var thisWIN;
	var thisURL;
	var thiswidth;
	var thisheight;
	thisWIN = self;
	if (thisWIN.name == winName) { theWIN = thisWIN}
	if (!theWIN || theWIN.closed) {
		var x = parseInt(window.screen.availWidth)/2-parseInt(thiswidth+10)/2;
        var y = parseInt(window.screen.availHeight)/2-parseInt(thisheight+10)/2;
        theWIN = window.open(thisURL,winName,"toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=no,copyhistory=0 ,width=" + thiswidth + ",height=" + thisheight + ",top="+y+",left="+x,screeny=0,screenx=0);
 	    theWIN.focus() ;
    } else {
	    theWIN.location.href = thisURL ;
        theWIN.resizeTo(thiswidth,thisheight) ;
	}
}
function loadFailure() {
    alert("'" + this.name + "' failed to load.");
    return true;
}
function setCookie(cookieName,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=cookieName+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ";path=/";
}
/*script using in list page*/

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function parentCheckBoxOnclick(parentForm, status, subCheckBoxName)
{
	for (var i=0; i<parentForm.elements.length; i++) {
		if(parentForm.elements[i].type == "checkbox" && parentForm.elements[i].name == subCheckBoxName) {
		   parentForm.elements[i].checked = status;
		}
	}
}


function subCheckBoxOnClick(subCheck, parentCheckBox)
{
    if (subCheck.checked == false)
    {
        parentCheckBox.checked = false;
    }
    else
    {
        status = true;

        subChecks = document.getElementsByName(subCheck.name);

        for (i=0; i<subChecks.length; i++)
        {
            if (subChecks[i].checked == false)
            {
                status = false;
                break;
            }
        }
    }
}

function checkSubCheckBoxIsChecked(form, subCheckBoxName, actionName)
{

    var isAnyChecked = false;
    for(var i=0; i<form.elements.length; i++)
        if( form.elements[i].type == "checkbox" &&
            form.elements[i].name == subCheckBoxName   &&
            form.elements[i].checked == true)
            {
                isAnyChecked = true;
            }
    return isAnyChecked;
}

function checkSubCheckBoxTotal(form, subCheckBoxName)
{
    var count = 0;
	for (var i=0; i<form.elements.length; i++) {
	    if(form.elements[i].type == "checkbox"
          && form.elements[i].name == subCheckBoxName
          && form.elements[i].checked == true ) {
	    	count ++;
        }
    }
    return count;
}
//return array values of checboxes
function getSelectedSubCheckBoxValues(form, subCheckBoxName)
{
    var checkedItemValues = new Array();
    var j = 0;
    for (var i=0; i<form.elements.length; i++) {
	    if(form.elements[i].type == "checkbox"
          && form.elements[i].name == subCheckBoxName
          && form.elements[i].checked == true ) {
	    	checkedItemValues[j] = form.elements[i].value;
	    	j++;
        }
    }
    return checkedItemValues;
}
function disableAllUnCheckItems(form, subCheckBoxName)
{
    for (var i=0; i<form.elements.length; i++) {
        if(form.elements[i].type == "checkbox"
          && form.elements[i].name == subCheckBoxName
          && form.elements[i].checked == false ) {
        	form.elements[i].disabled = true;
        }
    }
}
function enableAllCheckItems(form, subCheckBoxName)
{
    for (var i=0; i<form.elements.length; i++) {
        if(form.elements[i].type == "checkbox"
          && form.elements[i].name == subCheckBoxName
          && form.elements[i].disabled == true) {
            form.elements[i].disabled = false;
        }
    }
}

function init0HTimestampCookie()
{
  var value = YAHOO.util.Cookie.get("timestampOf0hToday", Number);
  var updateCookie = true;
  if (value != null) {
	  var dateAt0h = new Date(1000*value);
	  var now = new Date();
	  if ( (now.getFullYear() == dateAt0h.getFullYear())
			  && (now.getMonth() == dateAt0h.getMonth())
			  && (now.getDate() == dateAt0h.getDate()) ) {
		  updateCookie = false;
	  }
  }
  if (updateCookie) {
	  var now = new Date();
	  var dateAt0h = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	  var timestampAt0h = Math.round(dateAt0h.getTime() / 1000);
	  YAHOO.util.Cookie.set("timestampOf0hToday", timestampAt0h, {
		    path: "/"
		});
	  //window.location.href = window.location.href; // reload to make sure server has this cookie
  }
}

function urlSafeBase64_encode(data)
{
	var result = base64_encode(data);
    result = str_replace(['+', '/', '=', '\r', '\n'], ['-', '_', '', '', ''], result);
    return result;
}

function urlSafeBase64_decode($data)
{
	var result = str_replace(['-', '_'], ['+', '/'], data);
	var len = result.length
    var mod4 = len % 4;
    if (mod4 > 0) {
    	var str = '====';
        result = result + str.substr(mod4);
    }
    result = base64_decode(result);
    return result;
}

function viewPdf(url){
    var winPdf = window.open(url, 'ViewPDF', "width=680, height=500, scrollbars=yes, menubar=no, location=no, status=no, resizable=yes, toolbars=no");
}

function html_decode (data) {

    var result = str_replace(['&amp;', '%2F'], ['&', '/'], data);
    return result;
}

function isNeedUpgradeBrowser() {
	var result = true;
	var version = parseInt(getBrowserVersion());

	if(jQuery.browser.msie && version >= 8){
		result = false;
	}
	if(jQuery.browser.chrome && version >= 7){
		result = false;
	}
	if(jQuery.browser.safari && version >= 500 ){
		result = false;
	}
	if(jQuery.browser.mozilla && version >= 3){
		result = false;
	}
	return result;
}

function getBrowserVersion(){
	var userAgent = navigator.userAgent.toLowerCase();
	jQuery.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var version = 0;

	// Is this a version of IE?
	if(jQuery.browser.msie){
		userAgent = jQuery.browser.version;
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}

	// Is this a version of Chrome?
	if(jQuery.browser.chrome){
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		jQuery.browser.safari = false;
	}

	// Is this a version of Safari?
	if(jQuery.browser.safari){
		userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}

	// Is this a version of Mozilla?
	if(jQuery.browser.mozilla){
	//Is it Firefox?
		if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
			userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
			userAgent = userAgent.substring(0,userAgent.indexOf('.'));
			version = userAgent;
		}
		// If not then it must be another Mozilla
		else{
		}
	}

	// Is this a version of Opera?
	if(jQuery.browser.opera){
		userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
		userAgent = userAgent.substring(0,userAgent.indexOf('.'));
		version = userAgent;
	}
	return version;
}

