String.prototype.endsWith = function( eString)
{
    var reg = new RegExp( eString + "$");
    return reg.test( this);
}

String.prototype.startsWith = function( eString)
{
    var reg = new RegExp( "^" + eString);
    return reg.test( this);
}

//
// Added by Nguyen Thanh Chung
//

/*Select check all checkbox*/
function checkAll(objCheckAll, obj)
{    
    //Check a All checkbox
    var check = document.getElementsByName(obj);     
    for(i = 0; i < check.length ;i ++)
    {
        check[i].checked = objCheckAll.checked;
    }
}

/*Check CheckBoxs was checked?*/
function isChecked(objCheck)
{
    //Check a collection checkbox is checked
    var check = document.getElementsByName(objCheck);
    
    if (check && check.length > 0)
    {
        for(i = 0; i < check.length ;i ++)
        {
           if(check[i].checked) 
           {  
              return confirm("You sure want to delete?");
           }
        }     
    }
    
    alert("You must select item to delete");
    return false;
} 

function isCheckedAnAlert(objCheck, alertquest, todo)
{
    //Check a collection checkbox is checked
    var check = document.getElementsByName(objCheck);
    
    if (check && check.length > 0)
    {
        for(i = 0; i < check.length ;i ++)
        {
           if(check[i].checked) 
           {  
              return confirm(alertquest);
           }
        }     
    }
    
    alert("You must select item to " + todo);
    return false;
}

/*Check CheckBoxs was checked?*/
function isCheckedItem(objCheck)
{
    //Check a collection checkbox is checked
    var check = document.getElementsByName(objCheck);
    
    if (check && check.length > 0)
    {
        for(i = 0; i < check.length ;i ++)
        {
           if(check[i].checked) 
           {  
              return true;
           }
        }     
    }
    
    alert("You must select at least one item");
    return false;
}

//return false if checkboxs were checked
function isMultiCheck(objCheck)
{
    var checks = document.getElementsByName(objCheck);
    var count = 0;
    if(checks.length > 0)
    {
        for(i = 0;i < checks.length; i ++)
        {
            if(checks[i].checked)
                count = count + 1;
            if(count == 2)
            {
                alert("You must select one item");
                return false;
            }
        }
        return true;
    }
}

// Utility function to obtain a valid XMLHttpRequest object.
function CreateXmlHttpRequestObject()
{
    var xmlObj;
    if (window.ActiveXObject)
    {
        try
        {
           xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e)
        {
           xmlObj = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    else
        xmlObj = new XMLHttpRequest();
        
    return xmlObj;
}

/* Common values for the ReadyState of the XMLHttpRequest object */
var READYSTATE_UNINITIALIZED = 0;
var READYSTATE_LOADING = 1;
var READYSTATE_LOADED = 2;
var READYSTATE_INTERACTIVE = 3;
var READYSTATE_COMPLETE = 4;

/* Common values for HTTP status codes */
var HTTPSTATUS_OK = 200;

/* Bind data to combobox */
function BindData(objddl,tableName,value)
{
    var xmlHttpObj = CreateXmlHttpRequestObject();
    if (xmlHttpObj)
    {
        // Obtain a reference to the <SELECT> drop list control.
        var ddl = objddl;
        ddl.disabled=true;
        // We want this request synchronous
        xmlHttpObj.open("GET", rootURL + "AJAXServer.ashx?ajaxAct=" + tableName + "&id=" + value, true);

        xmlHttpObj.onreadystatechange = function()
        {
            if (xmlHttpObj.readyState == READYSTATE_COMPLETE)
            {   
                // If the request was ok (ie. equal to a Http Status code of 200)            
                if (xmlHttpObj.status == HTTPSTATUS_OK)
                {   
                    var xmlDoc = xmlHttpObj.responseXML;
                    var nodeLocation = xmlDoc.getElementsByTagName('root');
                    for(i = 0; i < nodeLocation.length; i ++)
                     {
                        htmlCode = document.createElement('option');
                        ddl.options.add(htmlCode);
                        if (window.ActiveXObject)
                        {
                            htmlCode.text = nodeLocation[i].childNodes[1].childNodes[0].nodeValue;
                            htmlCode.value = nodeLocation[i].childNodes[0].childNodes[0].nodeValue;
                        }
                        else
                        {   
                            htmlCode.text = nodeLocation[i].childNodes[2].childNodes[0].nodeValue;
                            htmlCode.value = nodeLocation[i].childNodes[1].childNodes[0].nodeValue;
                        }
                        
                     }
                     ddl.disabled = false;                           
                }
            }
        }
           
    }
    // Execute the request            
    xmlHttpObj.send("SomeDataToSend");    
}

/* Clear item in combobox*/
function ClearItems(obj,itemText)
{
    var ddl = obj;            
    while(ddl.length > 0)
        ddl.remove(0);      
    var htmlCode = document.createElement('option');
    ddl.options.add(htmlCode);
    htmlCode.text = itemText;   
    htmlCode.value = "-1";  
    ddl.disabled = "disabled";                   
}

function HideGridEditFields(gridid, checkboxName)
{    
    
    var grid = document.getElementById(gridid);
    if(grid != null)
    {
    var editAnchors = grid.getElementsByTagName('a');
    var chkDels = document.getElementsByName(checkboxName);
    
    editAnchors[0].style.display = 'none';
    chkDels[0].style.display = 'none';
    }
}

//Show Hide Category and Location
function HideShow(arrayControl,objHidden,flag,imageName)
{
    var displayImage = document.getElementById(imageName);
    var srcPlus = "../Images/collapsed.gif";
    var srcMinus = "../Images/expanded.gif";
    if(flag == 1)
    {
        if(objHidden !=null)
        {
            
            if(objHidden.value.length != 0)
            {
                if(objHidden.value == "none")
                {
                    objHidden.value = "block";
                    displayImage.src = srcMinus;
                }
                else
                {
                    objHidden.value = "none";
                    displayImage.src = srcPlus;
                }
            }
            else
            {                            
                objHidden.value = "block";
                displayImage.src = srcMinus;
            }
        }                            
    }
    else
    if(objHidden.value == "none")
    {                
        displayImage.src = srcPlus;
    }
    else
    {                
        displayImage.src = srcMinus;
    }
    
        
    var obj;
    for(i=0;i < arrayControl.length; i++)
    {                                
        obj = document.getElementById(arrayControl[i]);
        if(obj!=null)
        {
            obj.style.display = objHidden.value;                              
        }
    }
    
}


function switchId(arrayID,flag)
{    
    var hiddenName = arrayID[arrayID.length - 2];    
    var imageName = arrayID[arrayID.length - 1];
	var srcPlus = "../Images/c.gif";
    var srcMinus = "../Images/e.gif";  
    var objHidden;
    if (document.getElementById)
    {
        objHidden = document.getElementById(hiddenName);
        var objImage = document.getElementById(imageName);
        //hideallids(arrayID);     
        if(flag == "1")
        {
            if(objHidden.value == "none")
            {            
                hideallids(arrayID);
                objImage.src = srcPlus;
                objHidden.value = "block";
            }
            else
            {            
                showallids(arrayID);            
                objImage.src = srcMinus;
                objHidden.value = "none"
            }
            alert("objHidden.value: " + objHidden.value);
        }    
        else
        {            
            if(objHidden.value == "none")
            {   
                showallids(arrayID);
                objImage.src = srcPlus;
                objHidden.value = "block";
            }
            else
            {            
                hideallids(arrayID);
                objImage.src = srcMinus;
                objHidden.value = "none"
            }
            alert("objHidden.value11: " + objHidden.value);
        }    
    }    
    else        
    {
        if (document.layers) 
        { // Netscape 4
		    if(document.hiddenName.display == 'none')
		    {
		        hideallids(arrayID);    
		        objImage.src = srcMinus;
		    }
		    else
		    {
		        showallids(arrayID);		    
		        objImage.src = srcPlus;
		    }
	    }
	    else 
	    { // IE 4
		    if(document.all.hiddenName.style.display == 'none')
		    {
		        hideallids(arrayID);
		        objImage.src = srcMinus;
		    }
		    else
		    {
		        showallids(arrayID);
		        objImage.src = srcPlus;
		    }
	    }
    }  
}


function hideallids(ids){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length - 2;i++){
		hidediv(ids[i]);
	}			  
}

function showallids(ids){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length - 2;i++){
		showdiv(ids[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6	    
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4		
			document.id.display = 'block';
		}
		else { // IE 4		
			document.all.id.style.display = 'block';
		}
	}
}

//For add or remove items in XListBox
function isExist(List, Text, Value)
{
  tLength = List.options.length;
  for (var i=0; i < tLength; i++)
  {
    if ((List.options[i].text == Text) && (List.options[i].value == Value))
    {
	    return true;
    }
  }
  return false;
}

function AddCategory()
{   
  var obj_calwindow;      
  obj_calwindow = window.open('../JobPostingCategory.aspx','abc','width=280,height=130,status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes');      
  obj_calwindow.opener = window;
  obj_calwindow.focus();	  
  return false;
}


function AddLocation()
{   
  var obj_calwindow;      
  obj_calwindow = window.open('../JobPostingLocation.aspx','abc','width=280,height=130,status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes');      
  obj_calwindow.opener = window;
  obj_calwindow.focus();	  
  return false;
}

function ReplaceText(objID,textValue)
{
    var objKeyword=document.getElementById(objID);
    if (objKeyword.value==textValue)
    {
        objKeyword.value="";
        return;
    }
    
    if(objKeyword.value == "")
    {
        objKeyword.value=textValue;
        return;
    }
}
