/** 
* Manipulate <A HREF..> with this fungction, it will return an UrlLink
* e.g.: onclick="GotoLink('news.php'); return true;"
* Usage:
*/
function GotoLink(UrlLink)
{
	parent.document.location.href = UrlLink;
}

/** Javascript NS7 above compatible **/

/**
 * Sets/unsets the pointer in browse mode
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 *
 * @return  boolean  whether pointer is set or not
 *
 * Usage:
 */
function setPointer(theRow, theClassName) {
    if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {
        return false;
    }
    var row_cells_cnt = theRow.cells.length;
    for (var c = 0; c < row_cells_cnt; c++) {
        theRow.cells[c].className = theClassName;
    }

    return true;
} // end of the 'setPointer()' function

/**
 * Strip whitespace from the beginning and end of a string
 *
 * @param   string   string to strip
 *
 * @return  string  This function returns a string with whitespace stripped from the beginning and end of text
 *
 * Usage:
 */
function trim(text) {
	var i,j;

	for(i=0;text.charAt(i)==" " && i<text.length;i++) {}
	if(i==text.length) return "";
	for(j=text.length-1;text.charAt(j)==" " && j>-1;j--) {}
	return text.substring(i,j+1);
}

/**
 * Check whether the obj Value is blank
 *
 * @param   string   the object
 * @param   string   message to show
 *
 * @return  boolean  whether obj value is empty or not
 *
 * Usage:
 */

function CheckBlank(obj, message) {
	if(trim(obj.value) == "") {
		if(trim(message) != "") {
			obj.focus();
			alert(message);
		}
		return true;
	} else {
		return false;
	}
}

/**
 * Check whether the obj Value is blank
 *
 * @param   string   the object
 * @param   string   message to show
 *
 * @return  boolean  whether obj value is empty or not
 *
 * Usage:
 */

function TextareaMaxLen(obj,maxlen) {
	if(obj.value.length>=maxlen && event.keyCode!=8) {
		obj.value = obj.value.substring(0,maxlen);
		alert('The maximum length of ' + obj.title + ' is ' + maxlen);
		return false;
	}
}

/**
 * Check whether the obj Value is blank
 *
 * @param   string   the object
 * @param   string   message to show
 *
 * @return  boolean  whether obj value is empty or not
 *
 * Usage:
 */

function IsEmailAddressValid(sEmail){
	var MailAddrPattern = /^([A-Za-z0-9-_.]{1,}@([A-Za-z0-9]{1,}\.){1,}[A-Za-z0-9]{1,})$/;
	var arMatch = sEmail.match(MailAddrPattern);

	if(arMatch==null)
		return false;
	else
		return true;
}

/**
 * Check whether the obj Value is blank
 *
 * @param   string   the object
 * @param   string   message to show
 *
 * @return  boolean  whether obj value is empty or not
 *
 * Usage: onkeypress="javascript:NumberOnly(event.which)"
 *
 * Note: 8 = Backspace, 9 = Tab, 13 = Enter, 48...57 = 0...9, 190 = .
*              0 = Tab (NS only)
 */

function ClearInputValue(sHTML){
   var p,q,r,s,result;

   //Eliminate <INPUT TYPE=TEXT> value
   p = sHTML.indexOf('<INPUT',0);
   result = sHTML;
   while(p!=-1){
		if(r!=-1) q = sHTML.indexOf('type',p+1);
		if(q==-1){
		   r = result.indexOf('value=',p+1);
		   if(r!=-1){
			  s = result.indexOf('=',r+7);
			  q = result.indexOf('"',r+7);
			  if(q>s || q==-1) q = result.indexOf(' ',r+7);
			  if(q!=-1){
			    result = result.substring(0,r) + result.substring(q+1,result.length);
		      }
		   }
		}
		p = sHTML.indexOf('<INPUT',p+1);
   }

   //Eliminate TEXTAREA value
   q = result.indexOf('</TEXTAREA>');
   r = result.indexOf('>');
   if(q!=-1)
     result = result.substring(0,r+1) + result.substring(q,result.length);

   return result;
}

function Check_Select(strType) {
	var obj_len = document.getElementsByName("cboSelect[]").length;
	switch(strType) {
	case "All":
		for(i=0;i<obj_len;i++) {
			document.getElementsByName("cboSelect[]")[i].checked = true;
		}
		break;
	case "Not":
		for(i=0;i<obj_len;i++) {
			document.getElementsByName("cboSelect[]")[i].checked = false;
		}
		break;
	case "Inv":
		for(i=0;i<obj_len;i++) {
			document.getElementsByName("cboSelect[]")[i].checked = !document.getElementsByName("cboSelect[]")[i].checked;
		}
		break;
	}
}

function Check_Delete() {
	var obj_len = document.getElementsByName("cboSelect[]").length;

	for(i=0;i<obj_len;i++) {
		if(document.getElementsByName("cboSelect[]")[i].checked == true) { return true; }
	}
	alert("Error !\nPlase select at least one row to delete.");
	return false;
}

function isValidDate(dateStr) {
	var datePat = /^(\d{2})(\/|-)(\d{2})(\/|-)(\d{4})$/; //requires 4 digit year
	var matchArray = dateStr.match(datePat);

	if (matchArray == null) {
		//alert(dateStr + " Date is not in a valid format.")
		return false;
	}

	day = matchArray[1];
	month = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
	   }
	}
	return true;
}

/*
Adding row using innerHTML
*/
var ip_rowcount = 2
function AddRow(id,opcode,grd){
    var tbody = document.getElementById("div_sub"+grd);
    if(opcode==0) {
    	var row = document.createElement("tr");
		tbody.appendChild(row);
		row.innerHTML=document.getElementById("myBody"+grd).innerHTML;
		ip_rowcount ++;
    }else {
		var row = tbody.lastChild;
 		if (ip_rowcount > 2) {
			tbody.removeChild(row);
			ip_rowcount --;
		}
    }
  }

//** Declaring Valid Date character, minimum year and maximum year **\\
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// Provided by MyFreeTemplates.com
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//////////// Disabled mouse right click \\\\\\\\\\

var message="Function Disabled!";

function clickIE(){
  if (event.button==2){
	alert(message)	
	return false;
  }
}

function clickNS(e){
  if (document.layers||document.getElementById&&!document.all){
	if (e.which==2||e.which==3){
	  alert(message)	  
	  return false;
	}
  }
}

if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS;
}
else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE;
}

// Disable right click without message
 document.oncontextmenu=new Function("return false") 

// Disable right click with message
//document.oncontextmenu=new Function("alert(message);return false")

//////////// End Disabled mouse right click \\\\\\\\\\