
function replaceSubstring(inputString, fromString, toString) {
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { 
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { 
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";

      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } 

      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }

      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } 
   return temp; 
}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue;
}

//****************************************************


function datediff(date1,date2)
{
	var temp1	 = new Array();
	var temp2	 = new Array();	
	temp1		 = date1.split("/")
	temp2		 = date2.split("/")	

	//Set the two dates
	var millennium =new Date(temp1[2], temp1[0]-1, temp1[1]) //Month is 0-11 in JavaScript
	today=new Date(temp2[2], temp2[0]-1, temp2[1])
	//Get 1 day in milliseconds
	var one_day=1000*60*60*24

	//Calculate difference btw the two dates, and convert to days
	return (Math.ceil((today.getTime()-millennium.getTime())/(one_day)))

	
}

//****************************************************

function isArray(obj)
{
	return(typeof(obj.length)=="undefined")?false:true;
}

//****************************************************

function SelectRadio(ctrl,num,tmp_value)
{
	for (i=0 ; i<num ; i++)
	{	
		if (ctrl(i).value == tmp_value) 
			ctrl(i).checked = true;
	}
}

//****************************************************

function chkCost(val)
{
	var re = /^[0-9\.\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;
}


function chkDiscCode(val)
{
	var re = /^[0-9\.\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;
}

function chkUserID(val)
{
	var re = /^[a-z0-9A-Z]*$/;
	if (!val.match(re)) 
	return 0;
	else
	return 1;

}
function chkpass(val)
{
	var re = /^[a-z0-9A-Z]*$/;
	if (!val.match(re)) 
	return 0;
	else
	return 1;

}

//*************************************************************//

function chkEmpty(txt)
{
if (txt=="")
   return 0;
else
  return 1;
}

//*************************************************************//

function chkEmail(eml)
{
	var re = /^[0-9a-zA-Z\-\.\_]+@[0-9a-zA-Z\-]+\.[0-9a-zA-Z\-\.]+$/;
	if (!eml.match(re)) 
		return 0;
	else
		return 1;

}

//*************************************************************//

function chkCityName(val)
{
	var re = /^[a-zA-Z\ \\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}


function chkChrOnly(val)
{
	var re = /^[a-zA-Z\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

function CheckTitle(val)
{
	var re = /^[a-zA-Z\.\ \\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

//*************************************************************//
function chkPhone(val)
{
	var re = /^[0-9\,\-\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;
}


function chkNum(val)
{
	var re = /^[0-9\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;
}

//*************************************************************//
//*************************************************************//

function chkOnlyNum(val)
{
	var re = /^[0-9\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;
}

//*************************************************************//

function chkRadio(val,num)
{
	for (i=0 ; i<num ; i++)
	{	
		if (val(i).checked) 
			return 1;
	}
	return 0;
}


//*************************************************************//

function chkChrNumOnly(val)
{
	var re = /^[a-zA-Z0-9\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

//************************************************************//
function chkZipCode(val)
{
	var re = /^[a-z0-9A-Z\-\,\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}


function chkName(val)
{
	var re = /^[a-z0-9A-Z\ \\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

function chk_US_TelFax(val)
{
	var re = /^[0-9\-\\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

//************************************************************//
function formreset(frmID){
	
		var e
		e = 0;

		while (frmID.elements.length > e ){

				if (frmID.elements[e].type == "text" || frmID.elements[e].type == "password" || frmID.elements[e].type == "textarea" || frmID.elements[e].type == "hidden" ){

					frmID.elements[e].value = "";
					}
				else 
					if (frmID.elements[e].type == "select-one" ){
					if (frmID.elements[e].name != "cboPackage" )
	///				frmID.elements[e].selectedIndex = 0;
					frmID.elements[e].value = 0;
					}
				else 
					if (frmID.elements[e].type == "checkbox"  ){
					frmID.elements[e].checked = false;
					}
				else 
					if (frmID.elements[e].type == "radio" )
					document.getElementById(frmID.elements[e].name).checked = true;
				
				e = e + 1;
		}

}
//************************************************************//

function isLeapYear(Year) 
{
if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
return true;
}
}

//************************************************************//

//*******************************************************************//
/**
 * strPad
 *
 * Pad a string to a certain length with another string
 *
 * This functions returns the input string padded on the left, the right, or both sides
 * to the specified padding length. If the optional argument pad_string is not supplied,
 * the output is padded with spaces, otherwise it is padded with characters from pad_string
 * up to the limit.
 *
 * The optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH.
 * If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
 *
 * If the value of pad_length is negative or less than the length of the input string,
 * no padding takes place.
 *
 * object string
 * return string
 *
 * examples:
 *   var input = 'foo';
 *   input.strPad(9);                      // returns "foo      "
 *   input.strPad(9, "*+", STR_PAD_LEFT);  // returns "*+*+*+foo"
 *   input.strPad(9, "*", STR_PAD_BOTH);   // returns "***foo***"
 *   input.strPad(9 , "*********");        // returns "foo******"
 */

var STR_PAD_LEFT  = 0;
var STR_PAD_RIGHT = 1;
var STR_PAD_BOTH  = 2;

String.prototype.strPad = function(pad_length, pad_string, pad_type)
{
  /* Helper variables */
  var num_pad_chars   = pad_length - this.length;/* Number of padding characters */
  var result          = '';                       /* Resulting string */
  var pad_str_val     = ' ';
  var pad_str_len     = 0;                        /* Length of the padding string */
  var pad_type_val    = pad_type;            /* The padding type value */
  var i               = 0;
  var left_pad        = 0;
  var right_pad       = 0;
  var error           = false;
  var error_msg       = '';
  var output           = this;

  if (arguments.length < 2 || arguments.length > 4)
  {
    error     = true;
    error_msg = "Wrong parameter count.";
  }


  else if(isNaN(arguments[0]) == true)
  {
    error     = true;
    error_msg = "Padding length must be an integer.";
  }
  /* Setup the padding string values if specified. */
  if (arguments.length > 2)
  {
    if (pad_string.length == 0)
    {
      error     = true;
      error_msg = "Padding string cannot be empty.";
    }
    pad_str_val = pad_string;
    pad_str_len = pad_string.length;

    if (arguments.length > 3)
    {
      pad_type_val = pad_type;
      if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH)
      {
        error     = true;
        error_msg = "Padding type has to be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH."
      }
    }
  }

  if(error) throw error_msg;

  if(num_pad_chars > 0 && !error)
  {
    /* We need to figure out the left/right padding lengths. */
	
    switch (pad_type_val)
    {
      case STR_PAD_RIGHT:
        left_pad  = 0;
        right_pad = num_pad_chars;
        break;

      case STR_PAD_LEFT:
        left_pad  = num_pad_chars;
        right_pad = 0;
        break;

      case STR_PAD_BOTH:
        left_pad  = Math.floor(num_pad_chars / 2);
        right_pad = num_pad_chars - left_pad;
        break;
    }

    for(i = 0; i < left_pad; i++)
    {
      output = pad_str_val.substr(0,num_pad_chars) + output;
    }

    for(i = 0; i < right_pad; i++)
    {
      output += pad_str_val.substr(0,num_pad_chars);
    }
  }

  return output;
}


///////////////////////////////////////////////////////
function getDays(month, year) {
  // create array to hold number of days in each month
  var ar = new Array(12);
  ar[0] = 31; // January
  ar[1] = (isLeapYear(year)) ? 29 : 28; // February
  ar[2] = 31; // March
  ar[3] = 30; // April
  ar[4] = 31; // May
  ar[5] = 30; // June
  ar[6] = 31; // July
  ar[7] = 31; // August
  ar[8] = 30; // September
  ar[9] = 31; // October
  ar[10] = 30; // November
  ar[11] = 31; // December

  // return number of days in the specified month (parameter)
  return ar[month];
}


//************************************************************//
	//---------------selected value for ddl-------------------
	//------parameter-------(valueto be selected, object)-----
	function SelectDD(cnt,obj)
	{
		if(cnt != "")
		{
			for (i = 0 ; i < obj.length ; i++)
			{
				if (obj.options[i].value == cnt)
				{
					obj.options[i].selected = true ;
					break;
				}
			}
		}
	}
//************************************************************//

//************************************************************//

//--------------rights selected------------------
	function Sel_option(cnt, ctrl)
	{
		Form = document.frm_user;
		tmp_control		= Form[ctrl];
		total_chk_box	= tmp_control.length;
		
		if(cnt != "")
		{
			var name;
			for (i = 1 ; i <= total_chk_box ; i++)
			{
				if (cnt == i)
				{
					var obj_sel=document.getElementById("rights"+i); 
					obj_sel.checked = true ;
				}
			}//end for 					
		}
					
	} //end function
//************************************************************//

//********************* ADDED BY HARIS HABIB *****************//
function chk_numeric(val)
{
	var re = /^[0-9]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

function chk_url(val)
{
	var re = "([http:\/\/]*)www.([-+/w+]+[-+.][-+/w+]+)*([-+.][-+/w+]+)*((//[-+/w+]+)*//[-+/w+]+[-+.][-+/w+]+)*";
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}

function Replace_Sub_String ( inputString, badString, goodString, caseSensitive ) {
	fixedReplace = " ";
	UI = inputString;
	UB = badString;
	if ((caseSensitive !=1) && (caseSensitive != true)) {
	UI = inputString.toUpperCase();
	UB = badString.toUpperCase();
	}
	badEnd = -1;
	badLoc = UI.indexOf(UB);
	if (badLoc != -1) {
	for (x=1; (badLoc != -1); x++) {
	fixedReplace = fixedReplace + inputString.substring((badEnd + 1), badLoc) + goodString
	badEnd = badLoc + UB.length - 1;
	badLoc = UI.indexOf(UB, (badLoc + 1)); }
	fixedReplace = fixedReplace + inputString.substring((badEnd + 1), inputString.length); }
	else { fixedReplace = inputString; }
	return fixedReplace;
}




//************************************************************//
//************************************************************//
//----------- ------------------------------  -----------------
function chkpcName(val)
{
	var re = /^[a-z0-9A-Z\-\.\ \\]*$/;
	if (!val.match(re)) 
	return 0;
	else
	return 1;

}
//----------- ------------------------------  -----------------
function chkFullName(val)
{
	var re = /^[a-zA-Z\ \\]*$/;
	if (!val.match(re)) 
		return 0;
	else
		return 1;

}
//------------------------------------------------

function check_time()
{
	var obj = (document.frm_sch.rb_time[0].checked);
	
	if(obj)
	{
		document.frm_sch.ddl_zone.disabled = true;
		document.frm_sch.ddl_time.disabled = true;
		document.frm_sch.ddl_date.disabled = true;
	}
	else
	{
		document.frm_sch.ddl_zone.disabled = false;
		document.frm_sch.ddl_time.disabled = false;
		document.frm_sch.ddl_date.disabled = false;
	}
}

//------------------------------------------------
function check_contact()
{
	var Form1 = document.frm_contact;
	var errmsg ="";
	
	if(!chkEmpty(Form1.cname.value))
	{
		errmsg = errmsg +"Please Enter Company Name \n";
	}	
	if(!chkEmpty(Form1.name.value))
	{
		errmsg =errmsg + "Please Enter Your Name \n";
	}	
	else
	{
		if (!chkCityName(Form1.name.value))
		{
			errmsg = errmsg + "Please Enter Character Only \n";
		}
	}
	if(!chkEmpty(Form1.email.value))
	{
		errmsg = errmsg + "Please Enter Email Address \n";
	}
	else
	{
		if(!chkEmail(Form1.email.value))
		{
			errmsg = errmsg + "Please Enter Valid Email Address \n";
		}
	}
	
	
	
	if(errmsg =="")
	{
		Form1.action="send_email.php";
		Form1.submit();
	}
	else
	{
		alert(errmsg);
	}
	
}
