//*********************************************************************
// File Name               : form_submit.js
// Scope Of Program        : validation script for  blank, numeric,
//			     alpha-numeric,email id form field.
// Created On              : 09/04/2007
// Modified On             : 09/04/2007
// Reason of Modification  : Add date validation function
// Special Remark          :
//*********************************************************************

function valid_form(sThis)
{
	if(validate_form(sThis))
	{
		if((!document.getElementById('bestway1').checked) && (!document.getElementById('bestway2').checked))
		{
			alert("Please select atleast one way to reach you.");
			return false;
		}
		else if((!document.getElementById('business1').checked) && (!document.getElementById('business2').checked))
		{
			alert("Please select type of business.");
			return false;
		}
		else if((sThis.clientTel1.value != "") && (isNaN(sThis.clientTel1.value)))
		{
			alert("Please enter valid telephone country code.");
			return false;
		}
		else if((sThis.clientTel2.value != "") && (isNaN(sThis.clientTel2.value)))
		{
			alert("Please enter valid telephone state code.");
			return false;
		}
		else if((sThis.clientTel3.value != "") && (isNaN(sThis.clientTel3.value)))
		{
			alert("Please enter valid telephone number.");
			return false;
		}
		else if(1)
		{
			if(sThis.clientEmailConfirm.value == sThis.clientEmail.value)
			{
				return true;
			}
			else
			{
				alert("Email address and Confirm email address should match.");
				return false;
			}
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function javaCal1(fld,frmName)
{
  /* Name   : javaCal            */
  /* Purpose  : Creates a popup Window for selecting the date. The
        selected date is set in the textfield.     */
  /* Inputs : fld = textfield for setting the date       */
  /* Outputs  : Calendar Popup           */
  /* Calls  : calnav.htm             */
  /* Called By  :                */


  tmp_dt_today= new Date();
  tmp_str_dt  = tmp_dt_today.getDate();
        tmp_str_mon = tmp_dt_today.getMonth();
        tmp_str_yr  = tmp_dt_today.getFullYear();

  var tmp_dt_timeStamp  = new Date(tmp_str_yr,tmp_str_mon,tmp_str_dt,0,0,0);
  var tmp_int_todaySecs = Date.parse(tmp_dt_timeStamp);

  /* allowPast = 1 allows selection of past dates
     allowPast = 0 does not allow selection of past dates
  */

  doc = "./include/inc_common/calnav.htm?frmField="+fld+"&tmp_int_todaySecs="+tmp_int_todaySecs+"&allowPast=1&form="+frmName;

  //alert(doc);
  window.open(doc,"Calendar","toolbar=0, location=0,directories=0,resizable=no,status=0,menubar=0,scrollbars=no,width=275,height=275,screenX=0,screenY=0");

}
function replaceAll(text, strA, strB)
		{
		    while ( text.indexOf(strA) != -1)
		    {
		        text = text.replace(strA,strB);
		    }
		    return text;
		}
function isBlank(tmp_str)
{
// Name      : isBlank.

// Purpose   : keeping validation for blank field.

// Inputs    : tmp_str -> string for validation

// Outputs   : return the value of veriable newString.
//             if newString = "" returns null

//alert(tmp_str);

var newString  = ''; //trim value of given string
var substring  = ''; // temporary string for checking white spaces in string.
beginningFound = false; // position of white space

// copy characters over to a new string
// retain whitespace characters if they are between other characters

for (var i = 0; i < tmp_str.length; i++)
 {
	// copy non-whitespace characters
	// hold whitespace characters in a temporary string if they follow a non-whitespace character

	if (tmp_str.charAt(i) != ' ' && tmp_str.charCodeAt(i) != 9)
	{
		// if the temporary string contains some whitespace characters, copy them first
		if (substring != '')
		{
			newString += substring;
			substring = '';
		}
		newString += tmp_str.charAt(i);
		 if (beginningFound == false)
		 {
		   beginningFound = true;
		 }
	}

	else if (beginningFound == true)
	{
	   substring += tmp_str.charAt(i);
	}
  }
  //alert(newString);

  return newString;

}
function isEmailId(tmp_str)
{
  // Name      : isEmailId.
  // Purpose   : allow user to enter value in email id format(xxx@kk.com).
  // Inputs    : tmp_str -> string for validation.
  // Outputs   : return 1 -> if form field is as email id format.
  //		 return -1 -> if form field is not as email id format.


//ignore validation if tmp_str is blank.

if(tmp_str != "")
  {
     // searching whole string word by word

       if (tmp_str.search)
        {
          //checking the words in string.
          //  if given string is not in email id format(xxx@zzz.com), return -1.
          //  else return 1.

       	       fsign = tmp_str.indexOf("@");
       	       ssign = tmp_str.indexOf(".");

	       if(fsign <= 0 || ssign <= 0)
	       {
		     return -1;
	       }
        }
  }
 return 1;
}
function isPhoneNo(tmp_str)
{
  // Name      : isPhoneNo
  // Purpose   : allow user to enter value in phone numbers with only "-" and "." .
  // Inputs    : tmp_str -> string for validation.
  // Outputs   : return 1 -> if form field is as phone no format.
  //		 return -1 -> if form field is not as phone no format.

 Chars = "0123456789+-.";
 flag=0;

      for (i = 0; i < tmp_str.length; i++)
      {
          // Check that current character is number.
          var c = tmp_str.charAt(i);

          if (Chars.indexOf(c) == -1)
          	flag = 1;
      }
      if(flag)
      {
	  return true;
      }
}
function validate_form(doc)
{
	//On Error Resume Next;

  // Name      : validate_form.

  // Purpose   : validate the form fields.

  // Inputs    : all form feilds values

  // Outputs   : return true -> if user enter enter validate data
  //             else return false
  //             alert box displays with error message.
  // Calls     : isBlank(tmp_str) -> checks the field is blank or containing
  //		                       only white speces.
  //             isAlphaNemeric(tmp_str) -> checks the feild is Alpha-Nemeric
  //		 isNumeric(tmp_int) -> checks the field is nemeric
  //		 isEmailId(tmp_str) -> checks the field is in email id
  //		                        format(xxx@yyy.com)



 //declearing veriables
 var str;                     // stores error messages.
 var blank_field;             // stores a string of form fields and error lable for
                              // blank validation.
 var numeric_field;           // stores a string of form fields and error lable for
                              // numeric[0-9] validation.
 var email_field;             // stores a string of form fields and error lable for
                              // email[xxx@zz.com] validation.
var phone_field;             // stores a value of phone no


 			      // of form field and error lable for image validation
 var str_blank_field;         // array in which value of blank_field stored by
                              // comma separating.
 var str_numeric_field;       // array in which value of numeric_field stored by
                              // comma separating.
var str_email_field;         // array in which value of email_field stored by
                              // comma separating.
var arr_phone_field;         // stores a form field details of Phone fields bycomma seperated
  if(typeof doc.js_Blank == "undefined")
  {
    blank_field = "";
  }
  else
  {
    blank_field = doc.js_Blank.value;
  }

  if(typeof doc.js_Numeric == "undefined")
  {
     numeric_field = "";
  }
  else
  {
      numeric_field = doc.js_Numeric.value;
  }

  if(typeof doc.js_Email == "undefined")
  {
      email_field = "";
  }
  else
  {
      email_field = doc.js_Email.value;
  }

  if(typeof doc.js_Phone == "undefined")
  {
    phone_field = "";
  }
 else
  {
    phone_field = doc.js_Phone.value;
  }
  str = "";
     //alert(blank_field);

     //calls function is_blank for blank validation, if blank_field is not null

       if(blank_field != "")
       {

         //creats array in which values stores without comma
          str_blank_field = blank_field.split(",");

          //loop for getting value from array for validating fields.
          for(var a=0; a<str_blank_field.length; a++)
           {

              //getting error lables for messages
              tmp_str_FlType = str_blank_field[a];

               a=a+1;

              //getting field name
               tmp_str_FldName = str_blank_field[a];

              //getting value of form fields
              if(tmp_str_FlType != "mul")
              {
                tmp_str_value = eval('document.requote_form.'+str_blank_field[a]+'.value');
              }
              else
              {
                tmp_str_value = "";
              }
               a=a+1;
              //getting error lables for messages
              tmp_err_Mess2 = str_blank_field[a];

           //calls function for blank validation
 			switch(tmp_str_FlType)

         	{
                case 'text':
                 tmp_Blank_validate = isBlank(tmp_str_value);
                 tmp_str_Mess1 = "Please enter ";
  	        break;
                case 'check':
                 tmp_Blank_validate = "checked";
                 if(eval('document.requote_form.'+tmp_str_FldName+'.checked') == false)
                 {
                   tmp_Blank_validate = "";
                   tmp_str_Mess1 = "Please check ";
                 }
                break;
                case 'radio':
                 tmp_Blank_validate = "";
                 tmp_str_Mess1 = "Please check ";

                 radiogroup = eval('document.requote_form.elements[tmp_str_FldName]');
		var radioLength = radiogroup.length;

		 if(radioLength == undefined)
		 {

		  if(radiogroup.checked)
		  {
		     tmp_Blank_validate = "checked";
		  }
		 }
		 else
		 {
                  for(var r = 0 ; r < radioLength ; ++r)
	           {
	            if(radiogroup[r].checked)
	            {
	              tmp_Blank_validate = "checked";
	            }
	           }
	          }
                break;
	        case 'select':
                 tmp_Blank_validate = "selected";
                 //alert(tmp_str_value);
                 if((eval('document.requote_form.'+tmp_str_FldName+'.selected') == true) || (tmp_str_value == ""))
                 {
                   tmp_Blank_validate = "";
                   tmp_str_Mess1 = "Please select ";
                 }
                break;
	        case 'mul':
                 tmp_Blank_validate = "selected";
                 mulCnt  = eval('document.requote_form["'+tmp_str_FldName+'"].length');
                 selMulCnt = 0;
		  /* loop through the selected titles.  */
		  for(m=0; m<mulCnt; m++)
		  {
		    tmp_str_value
		    if(eval('document.requote_form["'+tmp_str_FldName+'"]['+m+'].selected') == true)
		    {
		      if(eval('document.requote_form["'+tmp_str_FldName+'"]['+m+'].value' != ""))
		      {
		        selMulCnt++;
		      }
		    }
		  }
		  if(selMulCnt == 0)
		  {
		    tmp_Blank_validate = "";
	            tmp_str_Mess1      = "Please select ";
                  }
                break;
            }
          //if value of tmp_validate = "", sets the error message.
          if(tmp_Blank_validate == "")
           {
              str +=  tmp_str_Mess1 + tmp_err_Mess2 + "\n";
           }
         }
     }

//calls function isNumeric for numeric validation, if numeric_field is not null

     if(numeric_field != "")
     {
       //creats array in which values stores without comma
       str_numeric_field = numeric_field.split(",");

       //loop for getting value from array for validating fields.
       for(a=0; a<str_numeric_field.length; a++)
       {
         //getting value of form fields
	 tmp_str = eval('document.requote_form.'+str_numeric_field[a]+'.value');

	  a=a+1;

	 //getting error lables for messages
	 tmp_err_message = str_numeric_field[a];



	   //calls function for Numeric(0-9) validation
	   tmp_validate = isNumeric(tmp_str);

	//if value of tmp_validate = -1 setting error message.
	if(tmp_validate == -1)
	   {
	      str += "Please enter valid " + tmp_err_message + "\n";
	   }
       }
      }

   //calls function isEmailId for Email Id validation, if email_field is not null


     if(email_field != "")
     {
       //creats array in which values stores without comma
       str_email_field = email_field.split(",");

       //loop for getting value from array for validating fields.
       for(a=0; a<str_email_field.length; a++)
        {

          //getting value of form field
	  tmp_str = eval('document.requote_form.'+str_email_field[a]+'.value');

	  a=a+1;

	  //getting error lables for messages
	  tmp_err_message = str_email_field[a];



	 //calls function for Email ID(xxx@yyy.com) validation

	 tmp_validate = isEmailId(tmp_str);

	 //if value of tmp_validate = -1 setting error message
	  if(tmp_validate == -1)
	    {
	       str += "Please enter valid " + tmp_err_message + " \n";
            }
        }
     }

//calls function isPhoneNo for validating values for phone numbers
     if(phone_field != "")
     {
       //creats array in which values stores without comma
       arr_phone_field = phone_field.split(",");

       //loop for getting value from array for validating fields.
       for(a=0; a<arr_phone_field.length; a++)
       {
         //getting value of form fields
	 tmp_str = eval('document.requote_form.'+arr_phone_field[a]+'.value');

	  a=a+1;

	 //getting error lables for messages
	 tmp_err_message = arr_phone_field[a];


	   //calls function for phone no. validation
	   tmp_validate = isPhoneNo(tmp_str);

	//if value of tmp_validate = -1 setting error message.
	if(tmp_validate)
	   {
	      str += "Please enter valid " + tmp_err_message + "\n";
	   }
       }
   }
if(str)
    {
       alert(str);
       return false;
    }
   else
    {
       return true;
    }

}


// function for confirm delete
function chkdel(doc)
{
	tmp_str_confirm=confirm("Are you sure, You want to delete this News?\nClick on Ok will Delete this News.");
	if(tmp_str_confirm)
	{
		document.editNewsform.frm_str_submit.value="delete";
		return true;
	}
	else
	{
		return false;
	}
}