var newwinG;
function openPDF(flag){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.mutadawil.com/pdf/";
	switch(flag){
		case "guide_E":
			var newwin = window.open(path+'User_Guide_English.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "guide_A":
			var newwin = window.open(path+'User_Guide_Arabic.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;

		 case "downloadappln_E":
			var newwin = window.open(path+'Application.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		    case "downloadappln_A":
			var newwin = window.open(path+'Application_ar.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;

		case "forms1_E":
			var newwin = window.open(path+'forms1.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "forms2_E":
			var newwin = window.open(path+'forms2.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "forms3_E":
			var newwin = window.open(path+'forms3.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "forms4_E":
			var newwin = window.open(path+'forms4.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;
		case "forms5_E":
			var newwin = window.open(path+'forms5.pdf', '', 'height=500,width=600,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
		    break;



	}

		

}
function printform(title){
		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/mutadawil.css type=text/css rel=stylesheet>");
		win.document.writeln("<title>"+title+"</title>");
        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 border='0'><tr><td>");
		win.document.writeln("<img src=../images/head.gif>");
		win.document.writeln("</td></tr><tr><td><br>");
		win.document.writeln("<p align=center class=headprint>"+title+"</p>");

		win.document.write(document.getElementById("printtab").innerHTML);
		win.document.writeln("</td></tr>");
		win.document.writeln("</body></html>");
		
		win.document.close();

}


function openKnetHelp(lang){
	var tp = (screen.height-480)/2;
	var lt = (screen.width-740)/2 +50;
	var path = "https://www.mutadawil.com/help/knet/";
	var file ="";
	if(lang == "E"){
		file = "knethelp1.htm";
	}
	else{
		file = "knethelp1_ar.htm";	
	}
	var newwin = window.open(path+file, '', 'height=590,width=790,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
}
function openKnetHelp1(lang){
	var tp = (screen.height-480)/2;
	var lt = (screen.width-740)/2 +50;
	var path = "https://www.mutadawil.com/help/knet/";
	var file ="";
	if(lang == "E"){
		file = "knethelp1new.htm";
	}
	else{
		file = "knethelp1new_ar.htm";	
	}
	var newwin = window.open(path+file, '', 'height=590,width=790,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
}

function openHelp(filepath){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +50;
	var path = "https://www.mutadawil.com/pdf/help/";
	var newwin = window.open(path+filepath, '', 'height=550,width=790,left='+lt+',top='+tp+',scrollbars=yes,resizable=yes');		
}

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/announcethis/en_qatar.jpg", 'add1', 'height=650,width=470,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/announcethis/ramadannew.jpg", 'add1', 'height=500,width=650,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');		
}
function showAdd1(){
	var tp = (screen.height-450)/2 +100;
	var lt = (screen.width-740)/2  +100;
	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/newsite.htm", 'add1',  'height=290,width=390,left='+lt+',top='+tp+',scrollbars=no,resizable=yes,toolbars=no,menubar=no,location=no');			//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/announcethis/ramadannew.jpg", 'add1', 'height=500,width=650,left='+lt+',top='+tp+',scrollbars=no,resizable=yes');		
}
function showAdd_file(f){
	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/announcethis/"+f, 'add1', 'height=650,width=480,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/announcethis/ramadannew.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 openLogout(url){
	top.parent.hlogin.location.href=url		;

}


function openHLogin(url){
	var page ='';
	var pagePos = url.indexOf("OpenTrading");
	if(pagePos >0){
		page ="opentrading";
	}else{
		page = "mutadawilexpert";
	}
	//passing the parameter to the hidden iframe
	window.open("waitmessage.php?page="+page, 'newwinG', 'height=100,width=200,left=200,top=300,scrollbars=no,resizable=yes');
	top.parent.hlogin.location.href=url		
}

function openHLogin1(url){

	//alert(url);
	//passing the parameter to the hidden iframe
	window.open("waitmessage.php", 'newwinG', 'height=100,width=200,left=200,top=300,scrollbars=no,resizable=yes');
	
top.parent.hlogin.location.href=url		
}

function openFormPDF(filepath){
	var tp = (screen.height-450)/2;
	var lt = (screen.width-740)/2 +100;
	var path = "https://www.mutadawil.com/pdf/forms/";
	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
}


