var newwinG;

function chkCivilId(civil){
	var c = false;
	if(isNaN(civil)){
		c = false;
	}
	if(civil.length != 12){
		c = false;
	}
	if((parseInt(civil.substr(3,2)) > 12) || (parseInt(civil.substr(5,2)) > 31)){
		c = false;
	}
	var res = 11 - ( ( (civil.substr(0,1)*2) + (civil.substr(1,1)*1) + (civil.substr(2,1)*6) +(civil.substr(3,1)*3) +
				    (civil.substr(4,1)*7) + (civil.substr(5,1)*9) + (civil.substr(6,1)*10) +(civil.substr(7,1)*5) +
				    (civil.substr(8,1)*8) + (civil.substr(9,1)*4) +(civil.substr(10,1)*2) 
				  ) % 11 
				);
	
	if (parseInt(civil.substr(11,1)) ==  res ){
		c =true ;
	}
	if(!c)	{
		alert("Please enter a valid Civil Id");
		return false;
	}else{
		return true;
	}
}


function openPDF(flag){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.kfic-kw.com/";
	switch(flag){
		case "wasmSubscription":
			var newwin = window.open(path+'uploadfiles/funds/WASMSubscription.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "WASMRedemption":
			var newwin = window.open(path+'uploadfiles/funds/WASMRedemption.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "wasmArticle":
			var newwin = window.open(path+'uploadfiles/funds/wasmArticle.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		   case "wasmBook":
			var newwin = window.open(path+'uploadfiles/funds/wasmbook.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;

		case "wasmProspectus":
			var newwin = window.open(path+'uploadfiles/funds/wasmProspectus.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "basSubscription":
			var newwin = window.open(path+'uploadfiles/funds/bas_sub.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "basRedemption":
			var newwin = window.open(path+'uploadfiles/funds/bas_red.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
	
		case "basProspectus":
			var newwin = window.open(path+'uploadfiles/funds/bas_prospectus.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		   case "basAR":
			var newwin = window.open(path+'uploadfiles/funds/bashayer_ar.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;

		case "bayan07":
			var newwin = window.open(path+'uploadfiles/bayan/BayanHoldPR07EN.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "bayan06":
			var newwin = window.open(path+'uploadfiles/bayan/BayanHoldPR06EN.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
	
		case "bayan05":
			var newwin = window.open(path+'uploadfiles/bayan/BayanHoldPR05EN.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		



	}

		

}

function openFin(name){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.kfic-kw.com/";

	var newwin = window.open(path+'uploadfiles/finstmt/'+name+'.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		

}
function openPEReport(name){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.kfic-kw.com/";

	var newwin = window.open(path+'uploadfiles/pereport/'+name+'.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		

}
function openPENavReport(name){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.kfic-kw.com/";

	var newwin = window.open(path+'uploadfiles/penavreport/'+name, '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		

}
function openPEFundUpdate(name){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.kfic-kw.com/";

	var newwin = window.open(path+'uploadfiles/pefundupdate/'+name, '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		

}

	function confirmDelete(){
		if (confirm("Are you sure you want to remove this?")) {
		    return true;
		  }
		return false;
	
	}

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
function Number_onKeyPress() 
{
	if (event.keyCode == "38" || event.keyCode == "44" || event.keyCode == 39){event.returnValue=false;}
	else
	{if (event.keyCode != "8")
		{if (event.keyCode < "48" || event.keyCode > "57"){event.returnValue=false;}
		 else{event.returnValue=true;}}}
}
function String_onkeypress()
{
	var i
	var sCh
	
			if (event.keyCode == "38" || event.keyCode == "44" || event.keyCode == "39" || event.keyCode == "36")
			{event.returnValue=false;}


}
 function printsuccessform(){
 
		 var curdate = new Date();
		dstring = curdate.toGMTString();
		var printtitle = document.getElementById("printtitle");
		if(printtitle != null){
			title = document.getElementById("printtitle").value;
		}

		var win = window.open("", "win", "width=675,height=200,scrollbars=yes,resizable=yes"); // a window object
		win.document.open("text/html", "replace");
		win.document.writeln("<LINK href=../images/onlinekfic.css type=text/css rel=stylesheet>");
        win.document.writeln("<script>function dis(){window.print(); return false;}</s"+"cript>");
		win.document.writeln("<style> .noprint{display:none;} </style>");
		win.document.writeln("<body background-color='#FFFFFF' onload='dis()'> " );
		win.document.writeln("  <table width='100%' align='center' border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#FFFFFF'> ");
		win.document.writeln("   <tr><td  height='98' colspan=2>");
		win.document.writeln("    <table width='100%' align='center' border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#E0EBED'> ");
		win.document.writeln("   <tr bgcolor='#FFFFFF'><td  align='center'  ><img src='../images/kficLogo.JPG'></td></tr>");
		win.document.writeln("   </table>");
		win.document.writeln("   </td></tr>");
		win.document.writeln("    <tr><td colspan='2'  bgcolor='#FFFFFF' align='center'>");
		win.document.write(document.getElementById("printtab").innerHTML);
		    
		win.document.writeln(" <br><br></td></tr></table></td></tr>       </table>       ");	
			win.document.writeln("</body></html>");
		
		win.document.close();
}



function showAdd(){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2;
	var mess = new Array('ad1.swf','ad2.swf');
	var max = mess.length;
	var num = Math.floor((Math.random() * max));
	//newwinG = window.open("https://www.mutadawil.com/images/add/tadawul.JPG", 'add1', 'height=324,width=507,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');			//newwinG = window.open("https://www.mutadawil.com/images/add/"+mess[num], 'add1', 'height=385,width=600,left='+lt+',top='+tp+',scrollbars=no,resizable=no');		
	//newwinG = window.open("https://www.mutadawil.com/images/add/ramadan.jpg", 'add1', 'height=500,width=650,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');		
}



function openWin(url){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	newwinG = window.open(url, 'newwin', 'height=200,width=300,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');		
}

function openLogin(url){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	newwinG = window.open(url, 'newwinG', 'height=200,width=300,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');		
}
function clswin(){
	newwinG.close();
	}



function openFormPDF(filepath){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.mutadawil.com/pdf/forms/";
	alert(path);
	var newwin = window.open(path+filepath, '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
}


function openReportPDF(filepath){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.mutadawil.com/pdf/reports/";
	var newwin = window.open(path+filepath, '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
}


 function isBlank(str){
        var len = str.length
        var i
        for (i=0;i<len;++i){
                if (str.charAt(i) != " ") return false;
                }
        return true
        }

 function trim(inputString) {
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length-1);
        ch = retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) {
        // Note that there are two spaces in the string -
        //look for multiple spaces within the string
        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; // Return the trimmed string back to the user
}
function emailCheck(emailStr) {
                var emailPat=/^(.+)@(.+)$/
                var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
                var validChars="\[^\\s" + specialChars + "\]"
                var quotedUser="(\"[^\"]*\")"
                var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
                var atom=validChars + '+'
                var word="(" + atom + "|" + quotedUser + ")"
                var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
                var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
                var matchArray=emailStr.match(emailPat)
                if (matchArray==null) {
                                alert("Email address seems incorrect (check @ and .'s)")
                                return false
                }
                var user=matchArray[1]
                var domain=matchArray[2]
                

                if (user.match(userPat)==null) {
                    alert(" The username in the email doesn't seem to be valid")
                    return false
                }
                var IPArray=domain.match(ipDomainPat)
                if (IPArray!=null) {
                    // this is an IP address
                          for (var i=1;i<=4;i++) {
                            if (IPArray[i]>255) {
                                //alert(" Destination IP address is invalid ")
                                alert(" Please check the email address ")
                                return false
                            }
                    }
                    return true
                }
                

                // Domain is symbolic name
                var domainArray=domain.match(domainPat)
                if (domainArray==null) {
                        alert(" The domain name in the email address doesn't seem to be valid ")
                    return false
                }
                

                /* domain name seems valid, but now make sure that it ends in a
                   three-letter word (like com, edu, gov) or a two-letter word,
                   representing country (uk, nl), and that there's a hostname preceding 
                   the domain or country. */
                

                /* Now we need to break up the domain to get a count of how many atoms
                   it consists of. */
                var atomPat=new RegExp(atom,"g")
                var domArr=domain.match(atomPat)
                var len=domArr.length
                if (domArr[domArr.length-1].length<2 || 
                    domArr[domArr.length-1].length>3) {
                   // the address must end in a two letter or three letter word.
                   alert(" The email must end in a three-letter domain, or two letter country")
                   return false
                }
                

                // Make sure there's a host name preceding the domain.
                if (len<2) {
                   var errStr=" Email is missing a hostname!"
                   alert(errStr)
                   return false
		}
                return true;
	}


function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}


/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var dtCh= "-";
	var minYear=1900;
	var maxYear=2100;


	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}


