﻿// Permet de redimensionner les Iframe en fonction de leur taille réelle
function calcHeight(iframe)
{
  //find the height of the internal page
  if(document.getElementById(iframe)!= 'undefined')
  {
  var the_height= document.getElementById(iframe).contentWindow.document.body.scrollHeight + 1;
  //change the height of the iframe
  document.getElementById(iframe).height=   the_height+150;
  var th = the_height+150
  }
}

// Permet de redimensionner les Iframe en fonction de leur taille réelle
function calcWidth(iframe)
{
  //find the height of the internal page
  if(document.getElementById(iframe)!= 'undefined')
  {
    var the_Width= document.getElementById(iframe).contentWindow.document.documentElement.scrollWidth;
  //change the height of the iframe
  document.getElementById(iframe).width=   the_Width;
  }
}

function FrameHeight(the_iframe, marge)
{
var doc;
   if(the_iframe.contentDocument)   
      // Firefox, Opera   
      doc = the_iframe.contentDocument;   
   else if(the_iframe.contentWindow)   
      // Internet Explorer   
      doc = the_iframe.contentWindow.document;   
   else if(the_iframe.document)   
      // Others?   
      doc = the_iframe.document;   
  //find the height of the internal pagey
  if(the_iframe.id != 'undefined')
  {
  var the_height= doc.body.scrollHeight + marge;
  //change the height of the iframe
  the_iframe.height=   the_height;
  }
} 
function FrameWidth(the_iframe)
{
    var doc;
   if(the_iframe.contentDocument)   
      // Firefox, Opera   
      doc = the_iframe.contentDocument;   
   else if(the_iframe.contentWindow)   
      // Internet Explorer   
      doc = the_iframe.contentWindow.document;   
   else if(the_iframe.document)   
      // Others?   
      doc = the_iframe.document;   
  if(the_iframe.id!= 'undefined')
  {
     var the_Width=doc.documentElement.scrollWidth
    the_iframe.style.width= the_Width;
   }
} 

function FrameHeight2(the_iframe, marge)
{
var doc;
   if(the_iframe.contentDocument)   
      // Firefox, Opera   
      doc = the_iframe.contentDocument;   
   else if(the_iframe.contentWindow)   
      // Internet Explorer   
      doc = the_iframe.contentWindow.document;   
   else if(the_iframe.document)   
      // Others?   
      doc = the_iframe.document;   
  //find the height of the internal pagey
  if(the_iframe.id != 'undefined')
  {
  var the_height= doc.body.scrollHeight + marge;
  //change the height of the iframe
  the_iframe.height=   the_height;
  window.parent.document.getElementById("ctl00_CPH0_ifAdmin").style.height=the_height + marge;
  }
  var th = the_height + marge;
} 

function FrameWidth2(the_iframe)
{
    var doc;
   if(the_iframe.contentDocument)   
      // Firefox, Opera   
      doc = the_iframe.contentDocument;   
   else if(the_iframe.contentWindow)   
      // Internet Explorer   
      doc = the_iframe.contentWindow.document;   
   else if(the_iframe.document)   
      // Others?   
      doc = the_iframe.document;         
  if(the_iframe.id!= 'undefined')
  {
     var the_Width=doc.documentElement.scrollWidth
    //change the height of the iframe
    the_iframe.style.width= the_Width;
   window.parent.document.getElementById("ctl00_CPH0_ifAdmin").style.width=the_Width;
   }
   var tw = the_Width;
} 

//----------------------------------
// Fonctin pour gérere les cookies
//----------------------------------
var caution = false;
// name : name of the cookie
// value : value of the cookie
// [expires] : expiration date of cookie (defaults: end of current session)
// [path] : path for which the cookie is valid (default: path of calling document)
// [domain] : domain for which the cookie is valid (default: domain of calling document)
// [secure] : flag, indicating if cookie transmission requires a secure transmission (default: not require)
function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + '=' + escape(value) +
		((expires) ? '; expires = ' + expires.toGMTString() : '') +
		((path) ? '; path = ' + path : '') +
		((domain) ? '; domain = ' + domain : '') +
		((secure) ? '; secure' : '');
	if (!caution || (name + '=' + escape(value)).length <= 4000)
		document.cookie = curCookie;
	else
		if (confirm('Cookie exceeds 4KB and will be cut!'))
			document.cookie = curCookie;
}

// name : name of the desired cookie
// return string containing value of specified cookie, return NULL if cookie dose not exist
function getCookie(name, subname)
{
	var prefix = name + '=';
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(';', cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length;
	if (!subname)
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	else
	{
		var subcookie = document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex);
		prefix = subname + '=';
		cookieStartIndex = subcookie.indexOf(prefix);
		if (cookieStartIndex == -1)
			return null;
		cookieEndIndex = subcookie.indexOf('&', cookieStartIndex + prefix.length);
		if (cookieEndIndex == -1)
			cookieEndIndex = subcookie.length;
		return unescape(subcookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	}
}

// name : name of the cookie
// [path] : path of the cookie (must be same as created)
// [domain] : domain of the cookie (must be same as created)
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + '=' + 
			((path) ? '; path = ' + path : '') +
			((domain) ? '; domain = ' + domain : '') +
			 '; expires = Wednesday, 04-Nov-1997 23:00:00 GMT';
	}
}

//Check if cookies is enable, return 0 if enable, 1 if error
function detectCookies()
{
	var retour = 0;
	setCookie('TestCookiesEnable', 'Enable');
	if(getCookie('TestCookiesEnable') == null)
		retour = 1;
	deleteCookie('TestCookiesEnable');
	return retour;
}
////---------------------------------

function ouverturePopup(PageAOuvrir, PageName, Height, Width, scroll, statut, resize, MenuBar)
{
	var NewWindow, LeftPosition, TopPosition;
	if(!MenuBar) MenuBar = 'yes';
	if(!resize) resize = 'yes';
	if(!statut) statut = 'yes';
	if(!scroll) scroll = 'yes';
	if(!Width) Width = screen.availWidth;
	if(!Height) Height = screen.availHeight;
	if(!PageName) PageName = 'popupHPV';
	LeftPosition = (Width) ? (screen.availWidth - Width) / 2 : 0;
	TopPosition = (Height) ? (screen.availHeight - Height) / 2 : 0;
	NewWindow = window.open(PageAOuvrir, PageName, 'height=' + Height + 'px,width=' + Width + 'px,top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',status=' + statut + ',resizable=' + resize + ',menubar=' + MenuBar);
	NewWindow.focus();
	if(Width == screen.availWidth || Height == screen.availHeight)
	{
		if(document.all)
			NewWindow.resizeTo(screen.availWidth, screen.availHeight);
		else if(document.layers || document.getElementById)
		{
			NewWindow.outerHeight = screen.availHeight;
			NewWindow.outerWidth = screen.availWidth;
		}
	}
}

//Select organisation or ministry in ddl first
function SelectDdlfirst(msg)
{alert(msg);}
 
//Email address suffix do not match the organisation suffixe- Now use vbscript procedure.
function ConfirmEmailSuffix(ddlCliid, msg)
{
    if (confirm(msg) == true)
    { __doPostBack(ddlCliid, ''); }
    else
    { window.location.href = 'default.aspx'; return true; }
}

function OtherMinOrgSel(OtherMinOrgTbId,txtOrgMinOtherId,selorgmin)
{
    if( (document.getElementById(OtherMinOrgTbId)!=null && document.getElementById(OtherMinOrgTbId)!='undefined' && document.getElementById(OtherMinOrgTbId).value!=1 ) && ( selorgmin=='undefined' || document.getElementById(selorgmin)=='undefined' ||document.getElementById(selorgmin)==null || document.getElementById(selorgmin).selectedIndex<1)) //&& ddlsel!=-1 
    {
        var tbTxt = document.getElementById(txtOrgMinOtherId).value
        if(tbTxt.length<2)
        {
             document.getElementById(OtherMinOrgTbId).value=1;
             return true;
        }
    }
    else 
    {
    return false;
    }
}

function NextShowPopUpVerifOtherMin(MinOtherTbId)
{
    if (Page_ClientValidate())
    {
        var tb = document.getElementById(MinOtherTbId);
        if(tb!='undefined' && tb.value!=null && tb.value!="")
        {
        return true;//show popup
        }
    }
    else 
    {
    return false;
    }
}

function CheckPhone(tb)
{
    var pattern = /^[\+](([0-9\-\s]+[\s|\.|\-]?(\([1-9]+[0-9\s\.\-]{0,}\))?)|(\([0-9\-\s]+[\s|\.|\-]?[0-9\s\.\-]*\)))[0-9\-\.\s]*$/;
    var phoneEntry = tb.value;
    if(!pattern.test(phoneEntry))
    {
        OpenHelp();
    }
   
}

function ClientPhoneValidate(source, clientside_arguments)
{  
      var phone =   clientside_arguments.Value;
      var lastCharWasADigit = 1;
      var nbOpeningBracket = 0;
      var i = 1;      
      if(phone.substring(0 , 1)!='+')
      {
        clientside_arguments.IsValid=false;
             return;
      }
      else
      {
          while (i < phone.length)
          {
            var c = phone.substring(i , i+1);
            if (c != ' ')
            {
                if(!isNaN(c))
                {
                     lastCharWasADigit = 1;
                }
                else if (lastCharWasADigit == 0)
                {
                    clientside_arguments.IsValid=false;
                    return;
                }
                else
                {       
                       lastCharWasADigit = 0
                       if (c == '(')
                       {
                             nbOpeningBracket = nbOpeningBracket + 1;
                       }
                       else if (c == ')')
                       {
                             nbOpeningBracket =  nbOpeningBracket - 1   ;     
                             if (nbOpeningBracket < 0)
                             {
                                  clientside_arguments.IsValid=false;
                                  return;
                             }
                        }
                        else if (c != '.' && c != '-')
                        {
                             clientside_arguments.IsValid=false;
                             return
                        }
                  }
            }                            
            i++;
         }
     }
      
      if (nbOpeningBracket != 0)
      {
             clientside_arguments.IsValid=false;
             return;
      }
      clientside_arguments.IsValid = true;
      return;
}

function OpenHelp()
{
var w=1000;var h=700;
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
window.open("./PhoneNumbers.aspx","PhoneWindow", "directories = no,menubar = no,scrollbars=yes, width="+w+", height="+h+", left="+left+",top="+top);
}
