/******************************** TO REMOVE LEADING SPACES.******************************/
/* required to pass string value only */ 
function popup_affiliate(){
	document.location.href= '/aff_redirect.php';	
}
function goto_affiliate(url){
	document.location.href= '/aff_redirect.php';	
}
function popup_rhp(){
	document.location.href= '/aff_redirect.php';	
}
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

/******************************** TO REMOVE TRAILING SPACES.******************************/
/* required to pass string value only */ 

function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}


/******************************** TO REMOVE TRAILING & LEADING SPACES.******************************/
/* required to pass string value only */ 

function trim(str) {
   return rtrim(ltrim(str));
}

function validateEmail(fld)
{
	var my=fld.value;
	var attherate=my.indexOf("@");
	var lastattherate = my.lastIndexOf("@")
	var dotpos=my.lastIndexOf(".");
	var posspace = my.indexOf(" ");
	var totallen = my.length;
	
	if (attherate<=0 || dotpos<=0 || attherate > dotpos || (dotpos-attherate)<=1 || (dotpos == totallen-1) || posspace > -1 || attherate!=lastattherate)
		return false;
	else
		return true;
}	

function validateForm(frm,arr_compulsoryfields,arr_friendlynames)
{

	for(i=0;i<arr_compulsoryfields.length;i++)
	{
		myfld = eval("frm."+arr_compulsoryfields[i])
		
		if(trim(myfld.value) == "")
		{
			alert(arr_friendlynames[i]+" is not entered. Please enter "  + arr_friendlynames[i] + ".");			
			myfld.focus();
			return false;
		}		
	}
	return true;
}

function popupWindow(mypage, myname, w, h, boolscroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	//winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+boolscroll+',resizable'
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+boolscroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

/******************************** TO CHANGE ALL CHECKBOXES ******************************/
/* 

	Checkboxes must have an id attribute in the form cb0, cb1...
	Three parameter required
	1) flag : this will be either true or false, To mark as check, pass true else pass false
	1) form_name : pass the form object
	2) n : number of checkboxes
	3) fldname : the prefix character used in checkbox id
*/

function change_all_checkbox(flag, form_name, n, fldName ) {
	if (!fldName) {
     fldName = 'cb';
  }
	var f = eval('document.' + form_name.name );
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			cb.checked = flag;
			n2++;
		}
	}	
}

/*********************/
function CheckDeleteSelection(FormName,FieldName)
{
	var elementLength,isChecked;
	isChecked = false;
	elementLength = eval('document.'+FormName+'.elements.length');
	for(var i=0; i<elementLength; i++)
	{
		tt1=eval('document.'+FormName+'.elements[i].type');
		tt2=eval('document.'+FormName+'.elements[i].name');
		tt3=eval('document.'+FormName+'.elements[i].checked');
		
		if(tt1 == 'checkbox' && tt2 == FieldName && tt3)
		{
			isChecked = true;
			break;
		}
	}
	if(!isChecked)
	{
		alert('Please select atleast one checkbox');
		return false;
	}  
	return true;	
}

function listdel(FormName,chk_name)							////Function for delete broker
{
  chkarray=chk_name + "[]";
  if(!CheckDeleteSelection(FormName,chkarray)) return false;
   if(!confirm("Are you sure?"))    //confirm deleting broker
	 return false;
   else
	 return true;
}

function listapp(FormName,chk_name)							////Function for delete broker
{
  chkarray=chk_name + "[]";
  if(!CheckDeleteSelection(FormName,chkarray)) return false;
   if(!confirm("Are you sure you want to Approved?"))    //confirm deleting broker
	 return false;
   else
	 return true;
}


  function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }



function IsValidDate(DateToCheck, FormatString) {

  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i=0; i<strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}



/******************************** TO CHECK LEAP YEAR *******************************/
/* Required to pass the value of the year */
function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0)  {
	    return true;
		}
  } else {
    if ((intYear % 4) == 0) {
		return true;	
    }
  }
  return false
}



function check_password(field_name_1, field_name_2, field_size_min, field_size_max) {

    var password = field_name_1.value;
    var confirmation = field_name_2.value;

    if (password == '' || password.length < field_size_min) {		      
	  alert("Password should be minimum of " + field_size_min + " character");
      return false;
	}else if (confirmation == '' || confirmation.length < field_size_min) {		      
	  alert("Password should be minimum of " + field_size_min + " character");
      return false;
	}else if (password.length > field_size_max) {		      
	  alert("Password should be maximum of " + field_size_max + " character");
      return false;
	}else if (confirmation.length > field_size_max) {		      
	  alert("Password should be maximum of " + field_size_max + " character");
      return false;	  
    } else if (password != confirmation) {
	  alert("Password mismatch");
      return  false;
    }
	return true;
}


function isEmail(obj)
{
      var s_email = isTrim(obj.value);
      
     if ((s_email.length < 6) ||(s_email.indexOf('@',0) < 1) ||
         (s_email.lastIndexOf('@') != s_email.indexOf('@',0)) ||
         (s_email.lastIndexOf('@') > (s_email.length - 5)) ||
         (s_email.lastIndexOf('.') > (s_email.length - 3)) ||
         (s_email.lastIndexOf('.') < (s_email.length - 5)) ||
         (s_email.indexOf('..',0) > -1) ||
         (s_email.indexOf('@.',0) > -1) ||
         (s_email.indexOf('.@',0) > -1) ||
         (s_email.indexOf(',',0) > -1))
         {
             obj.value='';
			 obj.focus();
			 return false;
   		}
	return true;
}

function isTrim(str)
{
	return str.replace(/\s/g,"");
}

function isSpaceOrDash(obj)
{	
	if (obj.value.search(/[" "-]/) != -1)  
	{
		alert('Space or dashes are not allowes');
		obj.focus();
		return false;			
	}
	return true;
}

// Dhaval Dave : 30 May 2005. function check any special character for validation. e.g. @, %, & etc.
function validate_character(frm)
{
	var myRegxp = /[^0-9a-zA-Z_-]/

	if (myRegxp.test(frm) ) 
	{
		alert ("Special character like spaces, ~,!,@,#,$,%,^,& are not allowed");
		return false;
	}
	else
		return true;
}

/******************************** TO COUNT REMAINING CHARACTER *******************************/
/* 	field 		: 	field name whose value you want to count
	countfield	:	field name where you want to display remaining text
	maxlimit	:	maximum character 		
*/

function textCounter(field, countfield, maxlimit) 
{
  if (field.value.length > maxlimit)
  {
      field.value = field.value.substring(0, maxlimit);
  }
  else
  {
      countfield.value = maxlimit - field.value.length;
  }
}

