/*  
 *  Calligrafix Functions for tigerGreen
 */
 function getObject(obj) 
{
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}
 /*
 * Adds the trim function to a string.
 */
String.prototype.trim = function()
{
	var val = this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/, "$1");
	if ( val == "$1" ) {
	    val = ""; // fix for mozilla
	}
	return( val ); 
};

 /*
 	returns false and displays a message if 
 	there is nothing selected in the selection box
 */
 function hasSelected(form, fieldName, message)
 {
 		el = form.elements[fieldName];
 		for (i = 0; i < el.options.length; i++){
 			if (el.options[i].selected) {
 			    return true;
 			}
 		}
 		alert(message);
 		el.focus();
 		return false;
 }
 	
/* validate a required field display alert message if fails
 */
function validate_required(field,alerttxt)
{
    with (field)
    {
        if (value==null||value=="")
        {
            alert(alerttxt);return false;
        }
        else
        {
            return true;
        }
    }
}
 /*
  * returns false and displays message if
  * field of name fieldName does not contain
  * a minimum of 'length' characters.
  * length defaults to 0 if you leave it out.
  *
 */
 function isRequired(form, fieldName, message, length)
 {
 		if (typeof length == 'undefined') length = 0;
 		el = form.elements[fieldName];
 		var val = el.value.trim();
 		if ( val.length > length ){
 			return true;
 		}
 		else
 		{
 			alert(message);
 			el.focus();
 			return false;
 		}
 }
 
 /*
 * Compares two fields in a form
 * displays message if they aren't the same
 * and returns false.
 *
 */
 function areSame(form, fieldName1, fieldName2, message)
 {
 	el1 = form.elements[fieldName1];
 	el2 = form.elements[fieldName2];
 	if (el1.value == el2.value) return true;
 	else
 	{
 			alert(message);
 			el2.focus();
 			el2.select();
 			return false;
 	}
 
 }
 
 /*
  * Check if field value conforms to an email address format
  * return true or false
  * alert message on false
  */
function validate_email(field,alerttxt)
{
    with (field)
    {
        apos=value.indexOf("@");
        dotpos=value.lastIndexOf(".");
        if (apos<1||dotpos-apos<2) 
        {
            alert(alerttxt);return false;
        }
        else {return true;}
    }
}

 function isEmailAddress(form, fieldName, message)
 {
 
 	var result = false;
 
 	var el = form.elements[fieldName];
 	var theStr = el.value;
 
 	var index = theStr.indexOf("@");
 	if (index > 0)
 	{
 		var pindex = theStr.indexOf(".",index);
 		if ((pindex > index+1) && (theStr.length > pindex+1))
 			result = true;
 	}
 
 	if (result){
 		return true;
 	}else{
 		alert(message);
 		el.focus();
 		el.select();
 		return false;
 
 	}
 
 }
 
 /*
  * Is field within maximum length
  */
 function maxFieldLength(form, fieldName, maxLen, message)
 {
 	var element = form.elements[fieldName];
 	if (element.value.length > maxLen )
 	{
 		alert(message);
 		element.focus();
 		return false;
 	}
 }


/* 
 * update on typing a text counter
 */
function postProcessUpdateKeypress(tbox, e) {
  var val = tbox.value; 
  updateTextCharCounter(255,val);
}

/*
 * update character counter -- following twitter colours
 */
function updateTextCharCounter(maxval,value) {
  $('char-counter').innerHTML = maxval - value.length;
  
  if (value.length > 130) {
    $('char-counter').setStyle({ color: '#d40d12' });
  } else if (value.length > 120) {
    $('char-counter').setStyle({ color: '#5c0002' });
  } else {
    $('char-counter').setStyle({ color: '#cccccc' });
  }
};

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function getRadioValueId(RadioName)
{
    var rn = document.getElementById(RadioName);
    if(rn === null) return null;
    return rn.value;
}

function getRadioValue(RadioName)
{
    var rn = document.getElementsByName(RadioName);
    if(rn == null) return null;
    for (var i = 0; i < rn.length; i++)
    {
        if (rn[i].checked)
        {
            return rn[i].value;
        }
    }
    return null;
}

/* site wide popups */
function sendToPopUp(itemID){
	var url = "SendTo.aspx?item_id=" + itemID;
	myWindow = window.open( url , "myWindow", "height=400, width=400, resizable=yes, scrollbars=yes");
	myWindow.focus();
	myWindow.location = url;
}

function helpPopUp(topic){
	var url = "admin/help/default.htm#" + topic;
	myWindow = window.open( url , "myWindow", "height=600, width=400, resizable=yes, scrollbars=yes");
	myWindow.focus();
	myWindow.location = url;
}

/* end popups */

/* may be deprecated now */
function setPictureSize(value){
	maxWidth = value.substring(0, value.indexOf(":") );
	maxHeight = value.substring(value.indexOf(":") + 1, value.length  );
	mainForm.file_pic1_max_width.value = maxWidth;
	mainForm.file_pic1_max_height.value = maxHeight;
}

function launchCalendar(formName, inputName) {

    mywindow=open('calendar.html?form='+ formName + '&input=' + inputName,'myname','resizable=no,width=350,height=270');
    mywindow.focus();
    if (mywindow.opener == null) mywindow.opener = self;
}


function clearBox(box) 
{
    if(box.value==box.defaultValue) 
    {
        box.value = "";
    }	 	 
}
/* end deprecated checks */

/* --------------------------------------------------------------------- */

function checkDate(form, fieldName){
	el = form.elements[fieldName];
	if ( isDate(el.value) != true ){
 		el.focus();
 		el.select();
		return false;
	}
	return true;
}

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}

/* date checking */
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	}
	return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
