//General Functions

//------------------------------------
// check to see if input is alphabetic
function isAlphabetic(val)
{
	if (val.match(/^[a-zA-Z ]+$/))
	{
	return true;
	}
	else
	{
	return false;
	} 
}


//====================================
var dtCh= "/";

var minYear=1900;

var maxYear=2100;



function CheckEmpty(Val,Label) 

{ 

  if(truncateSpace(Val)=="") 

  {

   alert(Label) 

   return false;

  } 

  return true;

}

//========================================

function CheckEmptyMsg(Val,Label) 

{ 

  if(truncateSpace(Val)=="") 

  {

   alert("Please enter "+Label) 

   return false;

  } 

  return true;

}

//========================================

function IsAmount(sText)

{

   var ValidChars = "0123456789.";

   var IsAmount=true;

   var Char;

   if(sText=="") return false

   for (i = 0; i < sText.length && IsAmount == true;i++) 	

   { 

		 Char = sText.charAt(i); 

		 if (ValidChars.indexOf(Char) == -1) 

         {

    	     IsAmount = false;

         }

   }

   return IsAmount;

}
//========================================

function IsValidAmount(sText,Label)

{

	if(!IsAmount(sText))

	{

		//alert("Invalid "+Label+".. "+Label+" Must Have Numeric Values")
		alert("Please enter the valid donation amount \n For example: to donate $10.00, enter 10.00")
	
		return false;

	}

	AmtArray = sText.split(".")

	/*if(AmtArray.length>2)

	{

		alert("Invalid "+Label+".. "+Label+" Must have single Period for Float")

		return false;

	}*/

	if(AmtArray.length>1)

	{

		FloatAmt = AmtArray[1];

		if(FloatAmt.length>2)

		{

			alert("Invalid "+Label+"..  Float Value must have two Digits")

			return false;

		}

	}

	return true

}


//========================================


function CheckNumber(sText,Label)

{

   var ValidChars = "0123456789";

   var IsNumber=true;

   var Char;

   if(sText=="") {alert("Please enter the"+Label);return false}

   for (i = 0; i < sText.length && IsNumber == true;i++) 

   { 

      Char = sText.charAt(i); 

      if(ValidChars.indexOf(Char) == -1) 

      {

         IsNumber = false;

      }

   }

   if(!IsNumber)

   	alert(Label+ " must be numeric")

   return IsNumber;

}


//========================================

function truncateSpace(Val) 

{ 

 	while(''+Val.charAt(0)==' ') 

   	Val=Val.substring(1,Val.length); 

   	return Val 

}  

function trim(Val) 

{ 

 	while(''+Val.charAt(0)==' ') 

   	Val=Val.substring(1,Val.length); 

   	return Val 

}  

//========================================

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 IsFax(sText,Label)

{

   var ValidChars = "0123456789-() ";

   var IsNumber=true;

   var Char;

   if(sText=="") {alert("Please enter the "+Label);return false}

   for (i = 0; i < sText.length && IsNumber == true; i++) 

   { 

      Char = sText.charAt(i); 

      if(ValidChars.indexOf(Char) == -1) 

      {

         IsNumber = false;

      }

   }

   if(!IsNumber)

   	alert(Label+ " should not contain characters other than 0-9 - ()")

   return IsNumber;

}

//========================================





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 IsPasswordLength(sPass1,sPass2)

{

	if(trim(sPass1)=="")

	{

		alert("Password Should Not be Empty")

		return false

	}

	

	if(sPass1!=sPass2)

	{

		alert("Password Mismatch")

		return false

	}

	return true;

}
//========================================

function IsValidURL(strURL,n)

{

		strURL 		= strURL.toUpperCase();

//		alert(strURL)		

		if(strURL==""||strURL=="HTTP://") return false	

		thePrefix 	= strURL.substr(0,7).toUpperCase();

		var Prefix2 = strURL.substr(0,8).toUpperCase();

		if( thePrefix!="HTTP://" )

		{ 

			siteFlag = 0;

			return false;

		}

		else

			siteFlag = 1;	

		if ( Prefix2!="HTTPS://" && siteFlag == 0 ) 

			return false;

		if(n!=-1) if(strURL.split("/").length>n) 

			return false;

		

		return true

}

//========================================

function CheckEmailAddr(emailVal)

{
	if (emailVal.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
	{
	return true;
	}
	else
	{
	return false;
	} 

}


//========================================

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,Label)

{

	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 for "+Label)

		return false

	}

	if (strMonth.length<1 || month<1 || month>12)

	{

		alert("Please enter a valid month for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])

	{

		alert("Please enter a valid day for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)

	{

		alert("Please enter a valid 4 digit Year ");// between "+minYear+" and "+maxYear+" for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)

	{ 

		alert("Please enter a valid date for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	return true

}

//========================================

function isValidDate(dtStr,Label)

{

	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 for "+Label)

		return false

	}

	if (strMonth.length<1 || month<1 || month>12)

	{

		alert("Please enter a valid month for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])

	{

		alert("Please enter a valid day for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)

	{

		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+" for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)

	{

		alert("Please enter a valid date for "+Label+"\nDate Format is mm/dd/yyyy")

		return false

	}

	return true

}

//========================================

function CheckUnCheck(frm,tmpVal)

{

	obj		= eval("document."+frm);



	ctrlPtr	= obj.elements;

	var n = obj.elements.length;

	for(i = 0 ;i<n;i++)

	{

		if(obj.elements[i].name == "chkRecord[]" )

		{

			if( tmpVal == 1 )

			{

				obj.elements[i].checked = 0;

			}

			else

			{

				obj.elements[i].checked = 1;	

			}

		}//end of if

	}//end of for

}//end of function


//========================================

function DeleteSelected(frm,hfName)

{

	obj		= eval("document."+frm);

	objHf	= eval("document."+frm+"."+hfName);

	ctrlPtr	= obj;

	var n = obj.elements.length;

	var flg;

	for(i = 0 ;i<n;i++)

	{

		if(ctrlPtr.elements[i].name == "chkRecord[]" )

		{

			if( ctrlPtr.elements[i].checked  == 1 )

			{

				flg = 1;

				break;	

			}

		}//end of if

	}//end of for	

	if(flg == 1)

	{

		if(confirm("Are you sure to delete selected records?"))

		{

			objHf.value = 1;

			obj.submit();

		}

	}

	else

	{

		alert("You first need to select the record to delete");

	}

}//end of function



//========================================

function SendNewsletter(frm,hfName)

{

	obj		= eval("document."+frm);

	objHf	= eval("document."+frm+"."+hfName);

	ctrlPtr	= obj;

	var n = obj.elements.length;

	var flg;

	for(i = 0 ;i<n;i++)

	{

		if(ctrlPtr.elements[i].name == "chkRecord[]" )

		{

			if( ctrlPtr.elements[i].checked  == 1 )

			{

				flg = 1;

				break;	

			}

		}//end of if

	}//end of for	

	if(flg == 1)

	{

		if(confirm("Are you sure to send newsletter to selectd users"))

		{

			objHf.value = 1;

			obj.action = "newnewsletter.php";

			obj.submit();

		}

	}

	else

	{

		alert("You first need to select the record");

	}

}//end of function



//========================================

function SingleDelete(frm,hfName,val)

{

	if(confirm("Are you sure to delete record?"))

	{

		obj			= eval("document."+frm);

		objHf		= eval("document."+frm+"."+hfName);

		objHf.value = val;

		obj.submit();

	}



}//end of function

//========================================

function commonSetPage(frm,pageno)

{

//	alert("Testing--Paging");

	

	obj	= eval("document."+frm);

	obj.page.value = pageno;

	obj.action = "";

	obj.target ="_self";

	obj.submit();

}

// pasing value to another page during refresh

function AddToTemp(prodid)

{

	obj = document.sublist;

	ctrlPtr = obj.elements;

	var n = obj.elements.length;

	var flg ;

	var StrProdIds = '';

	for(i = 0 ;i<n;i++)

	{

		if(obj.elements[i].name == "chkRecord[]" )

		{

		// alert("ProdId="+prodid+" , Val="+obj.elements[i].value);

			if(obj.elements[i].value == prodid)

			{

			flg = obj.elements[i].checked;

			}

		}

	}

	if(document.sublist.hfTmpIds.value == "")

	{

		document.sublist.hfTmpIds.value = prodid;

	}

	else

	{

		document.sublist.hfTmpIds.value = document.sublist.hfTmpIds.value + ","+prodid;

	}

	var Arr = document.sublist.hfTmpIds.value.split(",");

	

	for(var i=0;i<Arr.length;i++)

	{

	if(Arr[i] == prodid)

	{

	// alert("Arr Val="+Arr[i]+"Prod="+prodid+"---"+flg);

		if(flg ==1)

		{

		

			if(StrProdIds == "")

			{

			StrProdIds = prodid;

			}

			else

			{

			StrProdIds = StrProdIds+","+prodid;

			}

		}

	}

	else

	{

		if(StrProdIds == "")

		{

			StrProdIds =Arr[i];

		}

		else

		{

			StrProdIds = StrProdIds+","+Arr[i];

		}

		//alert(Arr[i]);

	}

	}

	if( flg == 1 )

	{

		if(StrProdIds == "")

		{

			document.sublist.hfTmpIds.value = prodid;

		}

		else

		{

			document.sublist.hfTmpIds.value = StrProdIds;

		}

	}

	else

	{

		document.sublist.hfTmpIds.value = StrProdIds;

	}

	//alert("all"+document.sublist.hfTmpIds.value);

	 //alert(document.sublist.hfTmpIds.value);

}



// end of refresh function passing another page

function PopupImage(img)

{

	var w=500,h=500;

  var winl = (screen.width-w)/2;

  var wint = (screen.height-h)/2;

  if (winl < 0) winl = 0;

  if (wint < 0) wint = 0;

  var settings = 'height=' + h + ',';

  settings += 'width=' + w + ',';

  settings += 'top=' + wint + ',';

  settings += 'left=' + winl + ',';

  settings += 'scrollbars=yes';

  if(img=="")

  {

  alert("No Enlarge Image");

  }else{



    var str = window.location.protocol + '//' + window.location.hostname;

	//stmichaelsmdwaterfront1\images\property_images

	var url=str + "/fram/image/gallery_image/" +img;

	window.open(url,'welcome',settings);

	}

	//window.open(url,'welcome',settings);

	//alert(url);

	//alert(settings);

	

    //return str;

}
//========================================

function delete_record(form,id,no)
{
 var checked=1;
 var checkbox=document.getElementsByName('chkRecord[]');
 for(var i=0;i<checkbox.length;i++)
 {
	if(checkbox[i].checked==true)
	{
	 checked=1;
	 break;
	}
	else
	{
	if(id!="")
	{
	 checked=1;
	 break;
	} else	{
	         checked=0;
	      }
	}
	 
 }
 if(checked==1)
 {
  if(no=="single")
  {
   if(confirm("Are you sure you want to delete this record?")) {
    form.action.value="delete_single";
	form.id.value=id;
    form.submit();
	}
  }
  else
  {
   if(confirm("Are you sure you want to delete the selected records?")) {    
     form.action.value="hfDelMultiple";
	
    form.submit();}
  }
 }
 else
 alert("Please select a record");
 
}

//========================================
// JavaScript Document
// Check all

function checkAllFields(ref)
{
var chkAll = document.getElementById('checkAll');
var checks = document.getElementsByName('chkRecord[]');
var removeButton = document.getElementById('removeChecked');
var boxLength = checks.length;
var allChecked = false;
var totalChecked = 0;
	if ( ref == 1 )
	{
		if ( chkAll.checked == true )
		{
			for ( i=0; i < boxLength; i++ )
			checks[i].checked = true;
		}
		else
		{
			for ( i=0; i < boxLength; i++ )
			checks[i].checked = false;
		}
	}
	else
	{
		for ( i=0; i < boxLength; i++ )
		{
			if ( checks[i].checked == true )
			{
			allChecked = true;
			continue;
			}
			else
			{
			allChecked = false;
			break;
			}
		}
		if ( allChecked == true )
		chkAll.checked = true;
		else
		chkAll.checked = false;
	}
	for ( j=0; j < boxLength; j++ )
	{
		if ( checks[j].checked == true ) {		  		  	
		  totalChecked++;
		}
    }
	if(removeButton != null) removeButton.value = "Delete ["+totalChecked+"] Selected";
}
function setActiveTab(tabId) {
  //var activeElement = document.getElementsByClassName('active');
  //   if(typeof(activeElement) != 'undefined') {
  //     activeElement[0].className = '';
  //   }
  document.getElementById(tabId).className = tabId + 'Active active';
}

