function ValidateMandatory(strFields)
{
    var fields = new Array();
    
    
    fields = strFields.split(",");
    
    for(count = 0;count < fields.length;count++)
    {
        if(document.getElementById(fields[count]).value == "" || document.getElementById(fields[count]).value == "-1")
        {
            return false;
        }
    }
    return true;
}
/*
function ValidateFields(strButtonField, strManFields, strNumFieldNames, strNumFields, strEmailField, strURLField, strMessageField)
{
    var isValid;
    var strMessage;
    //alert(document.getElementById(strButtonField).value);
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        if(strEmailField != "")
        {
            isValid = ValidateEmail(document.getElementById(strEmailField).value);
            if(!isValid)
            {
                strMessage = "Please enter a valid email address";
                document.getElementById(strMessageField).className = "errorMessage";
                document.getElementById(strMessageField).innerText = strMessage;
                return false;
            }
        }
        if(strURLField != "")
        {
            isValid = ValidateURL(document.getElementById(strURLField).value);
            if(!isValid)
            {
                strMessage = "Please enter a valid web site address";
                document.getElementById(strMessageField).className = "errorMessage";
                document.getElementById(strMessageField).innerText = strMessage;
                return false;
            }
       }
    }
    }
}
*/
function ValidateFieldsContacts(strButtonField, strManFields, strNumFieldNames, strNumFields, strEmailField, strURLField, strMessageField)
{
    //alert('start');
    var isValid;
    var strMessage;
    //alert(document.getElementById(strButtonField).value);
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        //alert('mandatory');
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        //alert('else');
        needToConfirm = false;
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                //alert('numeric');
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        if(strEmailField != "")
        {
            isValid = ValidateEmail(document.getElementById(strEmailField).value);
            if(!isValid)
            {
                //alert('email');
                strMessage = "Please enter a valid email address";
                document.getElementById(strMessageField).className = "errorMessage";
                document.getElementById(strMessageField).innerText = strMessage;
                return false;
            }
        }
        if(strURLField != "")
        {
            isValid = ValidateURL(document.getElementById(strURLField).value);
            if(!isValid)
            {
            //alert('web');
                strMessage = "Please enter a valid web site address";
                document.getElementById(strMessageField).className = "errorMessage";
                document.getElementById(strMessageField).innerText = strMessage;
                return false;
            }
       }
        //if(document.getElementById("strRbtnSensivityID").checked)
        //alert (RbtnSensivityID);
        var rb = document.forms[0].rblsensivity; //document.forms[0].RbtnSensivityID;
        //alert (rb);
         //for (var i=0; i< rb.length; i++)
          //{
          //alert (rb[i].value);
          //alert (rb[i].checked);
            if (rb[0].checked)  
            {
              var ans = confirm ("You have requested to make this Contact visible to ALL CRM users. Use the 'Make Visible to Users' button on the top of the page to share a Contact within a sales team or with up to 5 other persons. Hit OK to make Public or Cancel to use the 'Make Visible to Users' button at the top of the page.");
              if(ans)
              {
                return true;
              }
              else
              {
                return false;
              }
            }
         
          //}

       //alert('isvalidelse');
    }
    //alert('isvalidelsekkkkkkkk');
    }
    //alert('end');
    needToConfirm = false;
}

function ValidateFields1(strButtonField, strManFields, strNumFieldNames, strNumFields, strEmailField, strURLField, strMessageField)
{
    //alert('start');
    var isValid;
    var strMessage;
    //alert(document.getElementById(strButtonField).value);
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        //alert('mandatory');
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        //alert('else');
        needToConfirm = false;
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                //alert('numeric');
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        if(strEmailField != "")
        {
            isValid = ValidateEmail(document.getElementById(strEmailField).value);
            if(!isValid)
            {
                //alert('email');
                strMessage = "Please enter a valid email address";
                document.getElementById(strMessageField).className = "errorMessage";
                document.getElementById(strMessageField).innerText = strMessage;
                return false;
            }
        }
        if(strURLField != "")
        {
            isValid = ValidateURL(document.getElementById(strURLField).value);
            if(!isValid)
            {
            //alert('web');
                strMessage = "Please enter a valid web site address";
                document.getElementById(strMessageField).className = "errorMessage";
                document.getElementById(strMessageField).innerText = strMessage;
                return false;
            }
       }
       //alert('isvalidelse');
    }
    //alert('isvalidelsekkkkkkkk');
    }
    //alert('end');
    needToConfirm = false;
}

function ValidateFields2(strButtonField, strManFields, strNumFieldNames, strNumFields, strMessageField,from,to)
{
    var isValid;
    var strMessage;
  
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        var fromdate = Date.parse(document.getElementById(from).value);
        var todate =Date.parse(document.getElementById(to).value);
        if( fromdate > todate)
        {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = "From Date Should be Less than To Date";
                 return false;
        } 
        
    }
    }
    needToConfirm = false;
}
function ValidateFieldsCamp(strButtonField, strManFields, strNumFieldNames, strNumFields, strMessageField,from,to,pbd,ped)
{
    var isValid;
    var strMessage;
    
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        
        var fromdate = Date.parse(document.getElementById(from).value);
        var todate =Date.parse(document.getElementById(to).value);
        if( fromdate > todate)
        {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = "Actual Begin Date Should be Less than To Actual End Date";
                 return false;
        } 
        
        var pfromdate = Date.parse(document.getElementById(pbd).value);
        var ptodate =Date.parse(document.getElementById(ped).value);
        
         if( pfromdate > ptodate)
        {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = "Proposed Begin Date Should be Less than To Proposed End Date";
                 return false;
        } 
        
        
    }
    }
    needToConfirm = false;
}

function ValidateFields(strButtonField, strManFields, strNumFieldNames, strNumFields, strMessageField)
{
    var isValid;
    var strMessage;
    
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
    }
    }
    needToConfirm = false;
}

function ValidateFieldsQ(strButtonField, strManFields, strNumFieldNames, strNumFields, strMessageField,qd,sd)
{
    var isValid;
    var strMessage;
    
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        var quotedate = Date.parse(document.getElementById(qd).value);
        var shipdate =Date.parse(document.getElementById(sd).value);
        if(quotedate > shipdate)
        {
             document.getElementById(strMessageField).className = "errorMessage";
             document.getElementById(strMessageField).innerText = "Ship Date Sholud be greater than Quote Date";
             return false;
        }
        
    }
    }
    needToConfirm = false;
}

function ValidateFieldsIO(strButtonField, strManFields, strNumFieldNames, strNumFields, strMessageField,id,ed,sd,Itype)
{
    var isValid;
    var strMessage;
    
    if(document.getElementById(strButtonField).value != "Edit")
    {
    isValid = ValidateMandatory(strManFields);
       
    if(!isValid)
    {
        document.getElementById(strMessageField).className = "errorMessage";
        document.getElementById(strMessageField).innerText = "Please enter values for the mandatory fields";
        return false;
    }
    else
    {
        if(strNumFieldNames != "")
        {
            strMessage = ValidateNumeric(strNumFieldNames, strNumFields);
            if(strMessage != "")
            {
                 document.getElementById(strMessageField).className = "errorMessage";
                 document.getElementById(strMessageField).innerText = strMessage;
                 return false;
            }
        }
        var invoicedate = Date.parse(document.getElementById(id).value);
        var shipdate =Date.parse(document.getElementById(sd).value);
        var expirationdate = Date.parse(document.getElementById(ed).value);
       
        if( invoicedate > shipdate )
        {
                 document.getElementById(strMessageField).className = "errorMessage";
                 if (Itype=="Invoice")
                 {
                 document.getElementById(strMessageField).innerText = "Shipping Date Should be Greater than Invoice Date";
                 }else
                 {
                 document.getElementById(strMessageField).innerText = "Shipping Date Should be Greater than Order Date";
                 }
                 return false;
        } 
         if( invoicedate > expirationdate)
        {
                 document.getElementById(strMessageField).className = "errorMessage";
                  if (Itype=="Invoice")
                 {
                 document.getElementById(strMessageField).innerText = "Expiration Date Should be Greater than Invoice Date";
                 }else
                 {
                 document.getElementById(strMessageField).innerText = "Expiration Date Should be Greater than Order Date";
                  }
                 return false;
        } 
        
    }
    }
    needToConfirm = false;
}

function ValidateNumeric(strFieldNames, strFields)
{
    var fields = new Array();
    var fieldNames = new Array();
    var invalidFields = "";
    
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    
    fields = strFields.split(",");
    fieldNames = strFieldNames.split(",");
    
    for(count = 0;count < fields.length;count++)
    {
        blnResult = true;
        //if (document.getElementById(fields[count]).value.length == 0) return false;

         //  test strString consists of valid characters listed above
        for (i = 0; i < document.getElementById(fields[count]).value.length && blnResult == true; i++)
        {
          strChar = document.getElementById(fields[count]).value.charAt(i);
          if (strValidChars.indexOf(strChar) == -1)
          {
             blnResult = false;
          }
        }
       if(!blnResult)
       {
            invalidFields += fieldNames[count] + ",";
       }
    }
    
    if(invalidFields == "")
    {
        return "";
    }
    else
    {
        return "The following fields should be numeric: " + invalidFields;
    }
}

function ValidateEmail(strEmail) 
{

	var at="@";
	var dot=".";
	var lat=strEmail.indexOf(at);
	var lstr=strEmail.length;
	var ldot=strEmail.indexOf(dot);
	
	if(strEmail.length == 0)
	{
	    return true;
	}
	
	if (strEmail.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false;
	}

	if (strEmail.indexOf(at)==-1 || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false;
	}

	if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==lstr){
	    //alert("Invalid E-mail ID")
	    return false;
	}

	 if (strEmail.indexOf(at,(lat+1))!=-1){
	    //alert("Invalid E-mail ID")
	    return false;
	 }

	 if (strEmail.substring(lat-1,lat)==dot || strEmail.substring(lat+1,lat+2)==dot){
	    //alert("Invalid E-mail ID")
	    return false;
	 }

	 if (strEmail.indexOf(dot,(lat+2))==-1){
	    //alert("Invalid E-mail ID")
	    return false;
	 }
	
	 if (strEmail.indexOf(" ")!=-1){
	    //alert("Invalid E-mail ID")
	    return false;
	 }

	 return true;					
}

function ValidateURL(strURL)
{
     //var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
     var tomatch= /[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
     
     if(strURL.length == 0)
     {
         return true;
     }
	
     if (tomatch.test(strURL))
     {
        //window.alert("URL OK.");
        return true;
     }
     else
     {
        //window.alert("URL invalid. Try again.");
        return false; 
     }
}


function SignOut()
{
   
    window.navigate("Login.aspx");
}

function breakout_of_frame()
{
  if (top.location != location) 
  {
    top.location.href = document.location.href ;
  }
}

/*SAMPLE FUNCTION TO BE ADDED TO EVERY PAGE TO SEARCH
function GetVals(val1,val2)
{
   document.getElementById("Button1").value = val2;
}
*/

function SetVals(entityType,val1,val2)
{
    //alert(val1);
    //alert(val2);
    opener.GetVals(entityType,val1,val2);
    window.close();
}

var MouseX;
var MouseY;
function getMouseXY(e) 
{
          // Detect if the browser is IE or not.
        // If it is not IE, we assume that the browser is NS.
        var IE = document.all?true:false;

        // If NS -- that is, !IE -- then set up for mouse capture
        if (!IE) document.captureEvents(Event.MOUSEMOVE);

        // Set-up to use getMouseXY function onMouseMove
        //document.onmousemove = getMouseXY;

        // Temporary variables to hold mouse x-y pos.s
        var tempX = 0;
        var tempY = 0;

        // Main function to retrieve mouse x-y pos.s

      if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX; //+ document.body.scrollLeft;
        tempY = event.clientY; //+ document.body.scrollTop;
      } else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
      }  
      // catch possible negative values in NS4
      if (tempX < 0){tempX = 0}
      if (tempY < 0){tempY = 0}  
      // show the position values in the form named Show
      // in the text fields named MouseX and MouseY
      MouseX = tempX;
      MouseY = tempY;
      //document.getElementById("text1").value = MouseX + "," + MouseY;
      return true;
}

/*TO BE ADDED TO THE EDIT PAGES
function OpenCalendar(strField)
{
    window.open("Common/Pages/CalendarPopup.aspx?Field=" + strField, "Calendar",  "width=220,height=200,toolbar=0,scrollbars=0,screenX=" + (MouseX - 180) + 
        ",screenY=" + (MouseY + 135) + ",left=" + (MouseX - 180) + ",top=" + (MouseY + 135));
}
*/

/*TO BE ADDED TO THE EDIT PAGES
function OpenEntityPopUp()
{
    window.open("Common/Pages/EntityPopup.aspx" , "CRM Entity",  "width=220,height=200,toolbar=0,scrollbars=0,screenX=" + (MouseX - 180) + 
        ",screenY=" + (MouseY + 135) + ",left=" + (MouseX - 180) + ",top=" + (MouseY + 135));
}
*/

function GetQueryString(sName)
{

  /*
   get last loc. of ?
   right: find first loc. of sName
   +2
   retrieve value before next &
  
  */
  
  var sURL = new String(window.location);
  var iQMark= sURL.lastIndexOf('?');
  var iLensName=sName.length;
  
  //retrieve loc. of sName
  var iStart = sURL.indexOf('?' + sName +'=') //limitation 1
  if (iStart==-1)
        {//not found at start
        iStart = sURL.indexOf('&' + sName +'=')//limitation 1
		if (iStart==-1)
		   {//not found at end
		    return 0; //not found
		   }   
        }
        
  iStart = iStart + + iLensName + 2;
  var iTemp= sURL.indexOf('&',iStart); //next pair start
  if (iTemp ==-1)
		{//EOF
		iTemp=sURL.length;
		}  
  return sURL.slice(iStart,iTemp ) ;
  sURL=null;//destroy String
}