// All code copyright Layton-James Associates Ltd unless otherwise stated
// (c) Copyright 2003-2006 Layton-James Associates Ltd. All Rights Reserved.

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

// Function MM_preloadImages Copyright Marcomedia
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 Copyright Marcomedia
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 Copyright Marcomedia
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 Copyright Marcomedia
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];}
}

function jsNewLine( s ) {
    var string

    string = s ;
    return string.replace('[NL]','\n') ;
}

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;
}

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 strMonth=dtStr.substring(0,pos1)
	var strDay=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 : mm/dd/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
}

function is_SMDate(sValue)
{
    // validates an input field for dates formatted as dd-Mth-yy or (14-Mar-03)
    var sDate = sValue.toUpperCase();

    //alert('upper '+sDate) ;
    var bValid = false;

    if( sDate.length==0 || sDate == 'DD-MMM-YYYY') {
        //alert( 'bad length') ;
        return true;
    }

    var DateArray = sDate.split("-");

    // Convert the text month to a number - zero based month

    switch( DateArray[1] )
    {
        case "JAN": DateArray[1] = 1; break;
        case "FEB": DateArray[1] = 2; break;
        case "MAR": DateArray[1] = 3; break;
        case "APR": DateArray[1] = 4; break;
        case "MAY": DateArray[1] = 5; break;
        case "JUN": DateArray[1] = 6; break;
        case "JUL": DateArray[1] = 7; break;
        case "AUG": DateArray[1] = 8; break;
        case "SEP": DateArray[1] = 9; break;
        case "OCT": DateArray[1] = 10; break;
        case "NOV": DateArray[1] = 11; break;
        case "DEC": DateArray[1] = 12; break;
        default : return bValid ; break ;
    }

    // Use 1970 as the breaking point for determining whether the year
    // should be >= 2000 or 1900.

    if( DateArray[2].length==2) {
        if (DateArray[2] < 70) {
            DateArray[2] = 20 + DateArray[2];
        } else {
            DateArray[2] = 19 + DateArray[2];
        }
    }

    //alert('Passing '+DateArray[1]+'/'+DateArray[0]+'/'+DateArray[2])
    // check date string :: North American format
    return isDate(DateArray[1]+'/'+DateArray[0]+'/'+DateArray[2]) ;
}

function showDateFormat( oField, sFormat ) {
    if( oField.value != '' ) {
        return true ;
    }
    if( sFormat == 'uk') {
            oField.value = 'dd/mm/yyyy' ;
    }
    if( sFormat == 'us') {
        oField.value = 'mm/dd/yyyy' ;
    }
    if( sFormat == 'iso') {
        oField.value = 'yyyy-mm-dd' ;
    }
    if( sFormat == 'sm') {
        oField.value = 'dd-Mmm-yyyy' ;
    }
    if( sFormat == 'lm') {
        oField.value = 'dd-Month-yyyy' ;
    }
    return true ;
}

function clearDateFormat( oField, sFormat ) {
    if( oField.value == '' ) {
        return true ;
    }
    if( sFormat == 'uk') {
        if( oField.value == 'dd/mm/yyyy' ) {
            oField.value = '' ;
        }
    }
    if( sFormat == 'us') {
        if( oField.value == 'mm/dd/yyyy' ) {
            oField.value = '' ;
        }
    }
    if( sFormat == 'iso') {
        if( oField.value == 'yyyy-mm-dd' ) {
            oField.value = '' ;
        }
    }
    if( sFormat == 'sm') {
        if( oField.value == 'dd-Mmm-yyyy' ) {
            oField.value = '' ;
        }
    }
    if( sFormat == 'lm') {
        if( oField.value == 'dd-Month-yyyy' ) {
            oField.value = '' ;
        }
    }
    return true ;
}


var sStripHtmlRegEx = '/(<([^>]+)>)/ig' ;

// Array of un highlighted classes
var normalClass = new Array('outline', 'outline restrictedtld') ;
// Array of highlighted classes - must contain same number of
// elements as normalClass
var highlightClass = new Array('outline_hl', 'outline restrictedtld_hl') ;
// note: normal classes are checked first so if a match is found it'll toggle to the
// highlight.  This can cause unhighlighting to fail if the same class name is present
// in both normalClass and highlightClass

// array of row fields + num to highlight
var rowFields = new Array('row', 'domain', 'tld', 'da', 'sl', 'ex', 'category', 'country', 'regacc', 'ns1', 'ns2', 'dest', 'tm', 'slive') ;
// names of fields to store when highlighted
var storeFields = new Array('domain', 'tld') ;
// row field vlaues to store when highlighted
var storedValues = new Array() ;

function stringifyStoredValues() {
    return JSON.stringify(storedValues) ;
}

function getRowValue(iRow ) {
    var sString = '' ;

    // loop through fields to store
    for (iStoreIndex in storeFields) {
        // the the field exists in this view
        if( document.getElementById( storeFields[iStoreIndex] + iRow ) ) {
            sString = sString + document.getElementById( storeFields[iStoreIndex] + iRow ).innerHTML ;
        } // end if
    }
    // remove html and store the domain name
    sString = sString.replace(/(<([^>]+)>)/ig,"") ;
    return sString ;
}

function unStoreValue( iRow ) {
    var iIndex = 0 ;
    var sString = getRowValue( iRow ) ;

    // loop through fields to store to get index of item
    for (iUnStoreIndex in storedValues) {
        // the the field exists in this view
        if( sString == storedValues[iUnStoreIndex] ) {
            iIndex = iUnStoreIndex ;
            //alert( 'found del item at index ' + iIndex ) ;
        } // end if
    }

    // remove html and store the domain name
    storedValues.splice( iIndex, 1 )  ;
    return true ;
}

function storeValue( iRow ) {
    // remove html and store the domain name
    storedValues.push( getRowValue( iRow ) ) ;
    return true ;
}

function isTableRowSelected( iRow ) {
    var sString = '' ;

    // remove html and store the domain name
    sString = getRowValue( iRow ) ;

    // is it in the stored values array
    // loop through fields to get stored value
    for (iSelectIndex in storedValues) {
        if( sString == storedValues[iSelectIndex]) {
            return true ;
        }
    } // for
    return false ;
}

// Functions for table row highlighting
function highlightRow( iRow ) {
    // loop through fields
    for (iIndex in rowFields) {
        // the the field exists in this view
        if( document.getElementById( rowFields[iIndex] + iRow ) ) {
            // change the class
            if( !changeClass( document.getElementById( rowFields[iIndex] + iRow ) ) ) {
                return false ;
            }// end if else changeClass
        } // end if
    } // end for

    if( isTableRowSelected(iRow) ){
        unStoreValue(iRow)  ;
    } else {
        storeValue(iRow)  ;
    }

    return true ;
}


function changeClass( oObject ) {
    // loop through normal classes
    for (iClsIndex in normalClass) {
        // if we match then toggle to opposite class
        if( oObject.className == normalClass[iClsIndex]) {
            // Change the class
            oObject.className = highlightClass[iClsIndex] ;
            return true ;
        }
    } // end for

    // loop through highlight classes to turn off value
    for (iClsIndex in highlightClass) {
        // if we match then toggle to opposite class
        if( oObject.className == highlightClass[iClsIndex]) {
            // change the class
            oObject.className = normalClass[iClsIndex] ;
            return true ;
        }
    } // end for
    return true ;
}


String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
  if (parm == "") return false;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function isNum(parm)
{
    return isValid(parm,numb);
}
function isLower(parm) {
    return isValid(parm,lwr);
}
function isUpper(parm) {
    return isValid(parm,upr);
}
function isAlpha(parm) {
    return isValid(parm,lwr+upr);
}
function isAlphaNum(parm) {
    return isValid(parm,lwr+upr+numb);
}

function jsAddSlashes(str) {
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\0/g,'\\0');
    return str;
}
function jsStripSlashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\\\/g,'\\');
    str=str.replace(/\\0/g,'\0');
    return str;
}
