String.prototype.right = extract_right;
String.prototype.left = extract_left;
String.prototype.trim = trim_spaces;

function trim_spaces(from_where) {
	var temp_string = this;
	if (arguments.length === 0) {
		from_where = "BOTH";
	}
	if (from_where.toUpperCase() == "LEFT" || from_where == "BOTH") {
		while (temp_string.left(1) == " ") {
			temp_string = temp_string.substring(1);
		}
	}
	if (from_where.toUpperCase() == "RIGHT" || from_where == "BOTH") {
		while (temp_string.right(1) == " ") {
			temp_string = temp_string.substring(0, temp_string.length - 2);
		}
	}
	return temp_string;
}
function extract_left(total_chars) {
	return this.substring(0, total_chars);
}
function extract_right(total_chars) {
	return this.substring(this.length - total_chars);
}

function display_error(lock){
  if(confirm_msg() == true){
    var temp_string = this
    if(temp_string == "true" || temp_string == "TRUE"){
      var msg = "No modification allowed on the selected row.\n"+
              "Detail: The selected row has been activated.";
      alert(msg);
      return false;
    }
    return true;
  }
  return false;
}
function confirm_msg(){
  if(confirm("Are you sure that you want to continue?") == true)
    return true;
  return false;
}

function validate_listevent(from, to){
	var d1 = from.trim();
	var d2 = to.trim();
	if(d1.length == 0 || d2.length == 0){
		alert("Please enter both From date and To date");
		return false;
	}
	return true;
}

function validate_registration(name, email, companyName, address, phone, fax){
  var f1 = name.trim();
  var f2 = email.trim();
  var f3 = companyName.trim();
  var f4 = address.trim();
  var f5 = phone.trim();
  var f6 = fax.trim();
  var msg = "";
  if(f1.length == 0)
    msg += "\n * Name must not be empty";
  if(f2.length == 0)
    msg += "\n * Email must not be empty";
  else{
  	if(isValidEmail(f2) == false)
  		msg += "\n * Invalid email address";
  }
  if(f3.length == 0)
    msg += "\n * Company name must not be empty";
  if(f4.length == 0)
  	msg += "\n * Contact address must not be empty";
  if(f5.length == 0)
  	msg += "\n * Phone number must not be empty";
  else{
  	if(checkInternationalPhone(f5)==false){
  		msg += "\n * Invalid phone number";
  	}
  }
  if(f6.length == 0){  
  }
  else{
  	if(checkInternationalPhone(f6)==false){
  		msg += "\n * Invalid fax number";
  	}
  }
  if(msg != ""){
    msg = "Please correctly enter ther following fields:\n"+msg;
    alert(msg);
    return false;
  }
  return true;
}

function isValidEmail(str) {
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr-1){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1){
		    return false;
		}

		if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 8;

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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}





