/** 
 * Loads in a URL into a specified divName, and applies the function to 
 * all the links inside the pagination div of that page (to preserve the ajax-request) 
 * @param string href The URL of the page to load 
 * @param string divName The name of the DOM-element to load the data into 
 * @return boolean False To prevent the links from doing anything on their own. 
 */ 
function loadPiece(href,divName) {     
    $(divName).load(href, {}, function(){ 
        var divPaginationLinks = divName+" #pagination a"; 
        $(divPaginationLinks).click(function() {      
            var thisHref = $(this).attr("href"); 
            loadPiece(thisHref,divName); 
            return false; 
        }); 
    }); 
} 

//For Selecting/ deselecting check boxed
function selectDeselect(field, isCheck) {
	var boxes = document.getElementsByName(field);
	var boxes_checked = anyChecked();

	if(isCheck){
		if(document.getElementsByName(isCheck)[0].checked) setChecks(true);
		else setChecks(false);			
	}else{
		if(!boxes_checked) setChecks(true);
		else setChecks(false);
	}

	function setChecks( setting ) {
		for( var i=0; i < boxes.length; i++ ) {
			boxes[ i ].checked = setting;
		}
	}
	function anyChecked() {
		for( var i=0; i < boxes.length; i++ ) {
			if( boxes[i].checked == true) {
				return (true);
			} 
		}
		return (false);
	}
}

function checkMain(frm){
	var j = 0;
	for(i=0; i < frm.del.length; i++){
		if(frm.del[i].checked){
			j = j + 1;	
		}
	}
	if(frm.del.length != j){
		frm.chkNothing.checked = false;
	}else{
		frm.chkNothing.checked = true;
	}
}

function callblankdiv()
{
	document.getElementById('divloading').style.display='none';
}

function showloadingpag()
{
  new Ajax.Updater ('hidshowload', '/common/showloading');
  document.getElementById('divloading').style.display='block';
}


function validateContactForm()
{
	if(trim(document.getElementById("email").value, " ")=="")
	{
       alert("Please Enter E-mail.");
       document.getElementById("email").focus();
       return false;
    }
	
    if(!isEmail(document.getElementById("email").value))
	{
        alert("Please Enter Valid Email Address");
        document.getElementById("email").focus();
        return false;
    }	
	
	if(trim(document.getElementById("topic").value, " ")=="")
	{
       alert("Please Select Topic.");
       document.getElementById("topic").focus();
       return false;
    }	
	
	if(trim(document.getElementById("help").value, " ")=="")
	{
       alert("Please Enter Help Text.");
       document.getElementById("help").focus();
       return false;
    }	
	
    if(!isSpclChar(document.getElementById("help").value))
	{
        alert("Special chracters are not allowed in help text.");
        document.getElementById("help").focus();
        return false;
    }		
	
    //return true;
}


function validateReportForm()
{
	if(trim(document.getElementById("email").value, " ")=="")
	{
       alert("Please Enter E-mail.");
       document.getElementById("email").focus();
       return false;
    }
	
    if(!isEmail(document.getElementById("email").value))
	{
        alert("Please Enter Valid Email Address");
        document.getElementById("email").focus();
        return false;
    }	
	

		// RADIO BUTTON
	var operating_systemPass = -1;
	for (i=0; i<document.frm.operating_system.length; i++)
	{
		if(document.frm.operating_system[i].checked)
		{
			operating_systemPass = i;
		}
	}
	if (operating_systemPass == -1)
	{
		alert("Please select your operating system");
		return false;
	}
	
	var timesPass = -1;
	for (i=0; i<document.frm.times.length; i++)
	{
		if(document.frm.times[i].checked)
		{
			timesPass = i;
		}
	}
	if (timesPass == -1)
	{
		alert("Please select how often are you experiencing this bug");
		return false;
	}
		
	
	if(trim(document.getElementById("description").value, " ")=="")
	{
       alert("Please Enter Description.");
       document.getElementById("description").focus();
       return false;
    }	
	
    if(!isSpclChar(document.getElementById("description").value))
	{
        alert("Special chracters are not allowed in description.");
        document.getElementById("description").focus();
        return false;
    }		
	
    //return true;
}


function isEmail(aStr)
{
	var reEmail=/^[0-9a-zA-Z_'\.-]+\@[0-9a-zA-Z_\.-]+\.[0-9a-zA-Z_\.-]+$/;
	if(!reEmail.test(aStr))
	{
		return false;
	}
	return true;
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function isSpclCharEmail(stringToCheck){
	var iChars = "!#$%^&*()+=[]\\\';./{}|\":<>?";
	for (var i = 0; i < stringToCheck.length; i++) {
		if (iChars.indexOf(stringToCheck.charAt(i)) != -1) {
			return false;
		}
	}
	return true;
}

function isSpclChar(stringToCheck){
	var iChars = "!@#$%^&*()+=[]\\\';./{}|\":<>?";
	for (var i = 0; i < stringToCheck.length; i++) {
		if (iChars.indexOf(stringToCheck.charAt(i)) != -1) {
			return false;
		}
	}
	return true;
}
