function contains(smstring,lrgstring) {
	//returns true if lrgstring contains smstring.
	strlen1 = smstring.length
	strlen2 = lrgstring.length
	istrue = false	
	for (i=0;i<=strlen2;i++) {
		comp=lrgstring.substring(i-1,strlen2)
		comp = comp.substring(0,strlen1)		
		if (comp == smstring) {
			istrue = true
			break
		}
	}
	return istrue
}



function leftOf(smstring,lrgstring) {
	//returns leftmost characters of lrgstring up to smstring.
	//If user passes an empty string, change that to a space.
	if (smstring == ""){smstring = " "}
	strlen1 = smstring.length
	strlen2 = lrgstring.length
	foundat = 0
	for (i=0;i<=strlen2;i++) {
		comp=lrgstring.substring(i-1,strlen2)
		comp = comp.substring(0,strlen1)		
		if (comp == smstring) {
			foundat = i
			break
		}
	}
	return lrgstring.substring(0,(foundat-1))
}




function rightOf(smstring,lrgstring) {
	//returns the rightmost characters of lrgstring back to smstring.
	//If user passes an empty string, change that to a space.
	if (smstring == ""){smstring = " "}
	strlen1 = smstring.length
	strlen2 = lrgstring.length
	foundat = 0
	for (i=strlen2;i>=0;i--) {
		comp=lrgstring.substring(i-1,strlen2)
		comp = comp.substring(0,strlen1)		
		if (comp == smstring) {
			foundat = i
			break
		}
	}
	return lrgstring.substring(foundat,255)
}


function ReplaceString(sFull, sOld, sNew) {
  var sData = "";
  for (var i=0; i<sFull.length; i++) { 
  if (sFull.substring(i,i+sOld.length) == sOld) {
      sData = sData + sNew;
      i = i + sOld.length - 1;
  } else { sData = sData + sFull.substring(i,i+1) }
  }
  return sData;
}



function getinbetween(sLargeText,sFrom,sTo){
   sResult=leftOf(sTo,sLargeText)
   if (sResult != '' ) {
      sResult=rightOf(sFrom,sResult)
   }
   else
   {
      sResult='delimiters not found.'; 
   }
 
   return sResult;
}

function GO(){
   var sFileRef;
   var iIndex;
   var sVal;
   var ayahReference;
   var sText;
   var sMaxAccepted;
   var iLoopIndex;
   var sMatchedText;
   var iLoopCount;
   var iCurrentIterationValue;
   ayahReference= window.document.controlpanel.ayahreference.value ;
   ayahReference=ayahReference * 1
   iIndex=window.document.controlpanel.selectedSurah.selectedIndex;
   sVal  =window.document.controlpanel.selectedSurah.options[iIndex].value
   sText  =window.document.controlpanel.selectedSurah.options[iIndex].text
   
   //check if the selected surah has been syncronized already !...


   //===============================================

   // edit this area as the progress evolves
   sNotSyncBegin=0;
   sNotSyncTo=0;
   //===============================================   

   if ( (sVal >= sNotSyncBegin) && (sVal <= sNotSyncTo) )
   {
     alert('You have selected Sura no ' + sVal + '.\n\nSurah syncronizations from ' + sNotSyncBegin + ' to ' + sNotSyncTo + ' is still in progress. \n\nPlease try it later.') 
     return false;
   }
   sMaxAccepted=leftOf(')',sText)
   sMaxAccepted=rightOf('(',sMaxAccepted)
   sMaxAccepted =ReplaceString(sMaxAccepted," ","")
   sMaxAccepted = sMaxAccepted * 1
   iLoopCount = 0;
   
   if ( sMaxAccepted == '' || sMaxAccepted == 0 || sMaxAccepted == '0')
   {
     //get the max number from the top elements by cycling thru until finding a match
     for(var iLoopIndex=0;iLoopIndex<window.document.controlpanel.selectedSurah.options.length;iLoopIndex++) 
     {
       iCurrentIterationValue = window.document.controlpanel.selectedSurah.options[iLoopIndex].value;
       if ( iCurrentIterationValue != '' ) 
       {
       	 iLoopCount=iLoopCount + 1	
         if ( iLoopCount > 114 ) 
         {	
           if ( sVal == iCurrentIterationValue ) 
           {
             sMatchedText = window.document.controlpanel.selectedSurah.options[iLoopIndex].text
             sMaxAccepted=leftOf(')',sMatchedText)
             sMaxAccepted=rightOf('(',sMaxAccepted)
             sMaxAccepted =ReplaceString(sMaxAccepted," ","")
             sMaxAccepted = sMaxAccepted * 1
             break;
           }
         }
       }
     }
   }

   sFileRef="HTTP://ISLAMICITY.COM/MOSQUE/ARABICSCRIPT/AYAT/" + sVal + "/" + sVal + '_' + ayahReference + ".htm"


   if ( ayahReference != 0 ) 
   {
     if ( sVal == '' ) 
     {
	  alert('Please select a sura from the sura index.')
	  window.document.controlpanel.selectedSurah.focus();
     }
     else
     {
	  if ( ayahReference > sMaxAccepted   ) 
	  {
	     //larger number than the max ayah count is received 
             if ( contains('(',sText) )
             {
               sText = getinbetween(sText,'-','(');
             } 
	     alert('You requested to access the ayah numbered ' +  ayahReference + ' in the surah ' + sText + '.\n\nSince this surah contains a maximum of ' + sMaxAccepted + ' ayah\'s, your request cannot be fulfilled. \n\nPlease enter an ayah number that is less than ' + sMaxAccepted + '.')
             window.document.controlpanel.ayahreference.focus();       
     	     window.document.controlpanel.ayahreference.value='';

	  }
	  else
	  {
	     //window.location.href=sFileRef 
             window.document.controlpanel.action=sFileRef;
             window.document.controlpanel.submit();
             return true;
	  }
     }	
   }
   else
   {
     alert('Please enter a valid ayah number.');
     window.document.controlpanel.ayahreference.value='';
     window.document.controlpanel.ayahreference.focus();       
   }
   return false;  
}


function netscapeKeyPress(e) {
    if (e.which == 13){
        GO();
    }
}


function microsoftKeyPress() {
    if (window.event.keyCode == 13){
        window.document.controlpanel.ayahreference.value=window.document.controlpanel.ayahreference.value;
        GO();
    }
}

if (navigator.appName == 'Netscape' && navigator.appVersion > 3 ) {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = netscapeKeyPress;
}