/*

  MyWebExam.com Utility JavaScript Functions
  
  Copyright (C) Educatia AS, 2000-2010. All rights reserved.

*/  
  
  function openWin(obj, pWidth, pHeight) {
  
     var OpenWin;
  
     OpenWin = this.open(obj, "CtrlWindow", "top=10,left=10,width=" + pWidth + ",height=" + pHeight + ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
     
  }

  function openExeWin(obj) {
  
     var OpenWin;
     OpenWin = this.open(obj, "ExeWindow", "fullscreen=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");

  }

  function openImgWin(pURL) {
    var OpenWin;
  
    if (pURL != "") {
      OpenWin = this.open(pURL, "ImgWindow", "top=10,left=10,width=650,height=400,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
    }
  }


  function openFileLib(pFieldId) {
    var OpenWin;
  
    OpenWin = this.open("t_fil.asp?s1=search&r=" + pFieldId, "FileLibWindow", "top=10,left=10,width=750,height=600,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
    //OpenWin = this.open("t.asp", "FileLibWindow", "top=10,left=10,width=650,height=400,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");

  }

  function ToggleAll(pForm, checked) {
    len = pForm.elements.length;
    var i = 0;
    for(i = 0; i < len; i++) {
        pForm.elements[i].checked = checked;
    }
  }
  
  function showhide (id){ 
    if (document.getElementById) {
    obj = document.getElementById(id);
  	if (obj.style.display == "") {
  	  obj.style.display = "block";
  	}
  	else {
  	if (obj.style.display == "none") { 
  		obj.style.display = "block"; 
  	  } else { 
  		obj.style.display = "none"; 
  	  }
    }
    }
  }  

  function isFilled (passedField, pMessage) {
    var str = passedField.value;
		if (str == ""){
			alert(pMessage);
			passedField.focus();
			return false;
    }
    return true; 
  }
 
  function isNum(passedField, pMessage){   
    var str = passedField.value;
  	if (str == ""){
      alert(pMessage);
      passedField.focus();
      passedField.select();
  		return false;         
  	}   
  	for (var i = 0; i < str.length; i++){
  		var ch = str.substring(i, i + 1);
  		if ((ch < "0" || "9" < ch) && ch != '.'){
  			alert(pMessage);
        passedField.focus();
        passedField.select();
  			return false;         
  		}      
  	}   
  	return true;   
  }

  function isChecked (passedField, pMessage) {
		if (! passedField.checked){
			alert(pMessage);
			passedField.focus();
			return false;
    }
    return true; 
  }
  
	function validEmail(passedField, pMessage) {
    var str = passedField.value;
		invalidChars = " /:,;";

		if (str == "") {
			alert(pMessage);
      passedField.focus();
      passedField.select();
			return false;
		}
		for (i=0; i<invalidChars.length; i++) {

			badChar = invalidChars.charAt(i);
			if (str.indexOf(badChar,0) != -1) {
  			alert(pMessage);
        passedField.focus();
        passedField.select();
				return false;
			}
		}

		atPos = str.indexOf("@",1);
		if (atPos == -1) {
			alert(pMessage);
      passedField.focus();
      passedField.select();
			return false;
		}

		if (str.indexOf("@",atPos+1) != -1) {
			alert(pMessage);
      passedField.focus();
      passedField.select();
			return false;
		}

		periodPos = str.indexOf(".",atPos)

		if (periodPos == -1) {
			alert(pMessage);
      passedField.focus();
      passedField.select();
			return false;
		}

		if (periodPos+3 > str.length)	{
			alert(pMessage);
      passedField.focus();
      passedField.select();
			return false;
		}

		return true;
    }


	function validEmailOptional(passedField, pMessage) {
    var str = passedField.value;
		invalidChars = " /:,;";

    // accept blank values, but non-blanks must be valid email addresses

		if (str == "") {
			return true;
		}
		
		return validEmail (passedField, pMessage);
		
    }


  function checkPasswordSimple(passedField, pMessage) { 
   var str = passedField.value;
   
   //check new password 
   
   if(str.length < 5 ) { 
      alert(pMessage); 
      passedField.focus();
      passedField.select();
      return false; 
   } 
   
   if(str.length > 35 ) { 
      alert(pMessage); 
      passedField.focus();
      passedField.select();
      return false; 
   } 
   
   return true; 
   } 
  
  
  function limitSize(field, maxlimit) {
    if (field.value.length > maxlimit)
      field.value = field.value.substring(0, maxlimit);
  }


  //////////////////
  // date validation
  //////////////////


  var dtCh= ".";
  var minYear=1900;
  var maxYear=2100;


  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(passedField, pMessage){
  
    var dtStr = passedField.value;
  	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);
  
    // the date format is wrong (dd.mm.yyyy)
  	if (pos1==-1 || pos2==-1){
      passedField.focus();
      passedField.select();
  		alert(pMessage);
  		return false;
  	  }
  
    // the month is invalid
  	if (strMonth.length<1 || month<1 || month>12){
      passedField.focus();
      passedField.select();
  		alert(pMessage);
  		return false;
  	  }
  
    // the day is invalid
  	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
      passedField.focus();
      passedField.select();
  		alert(pMessage);
  		return false;
  	  }
  
    // the year is outside the valid range
  	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
      passedField.focus();
      passedField.select();
  		alert(pMessage);
  		return false;
  	  }
  
    // the date is invalid
  	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
      passedField.focus();
      passedField.select();
  		alert(pMessage);
  		return false;
  	  }
  
    return true;
  
  }

var soundEmbed = null;

function soundPlay(which) {

    if (soundEmbed) {
       document.body.removeChild(soundEmbed);
    }
    soundEmbed = document.createElement("embed");
    soundEmbed.setAttribute("src", which);
    soundEmbed.setAttribute("hidden", true);
    soundEmbed.setAttribute("height", 0);
    soundEmbed.setAttribute("width", 0);
    soundEmbed.setAttribute("autostart", true);
    soundEmbed.setAttribute("loop", false);
    document.body.appendChild(soundEmbed);
}
