﻿// forckeck section
var defaultEmptyOK = false
var whitespace = " \t\n\r";

//constants for Jawwal
var jfirst4digs = "0599";
var jfirst4digs1 = "0598";
var jnumberlength = 10;

//constants for Orange
var ofirst3digs="054";
var onumberlength=10;

//constants for Cellcom
var cfirst3digs="052";
var cnumberlength=10;


function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}



// to trim vals
function trim(str)
{
	if(str==null)
	return '';
   return str.replace(/^\s*|\s*$/g,"");
}


function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}




// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

///////////////


function getElementReference(elementId)
{
  var value=false;

  if(document.layers)
    value=document.layers[elementId];
  else
  {
    if(document.all)
      value=document.all[elementId];
    else
      if(document.getElementById)
        value=document.getElementById(elementId);
  }

  return value;
}



function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// to trim vals
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


function checkDateRange(dm1, dy1, dm2, dy2){
	
	if(((parseInt(dy1.value)) > (parseInt(dy2.value)) )){
wrong1=true;

}
else if(((parseInt(dy1.value)) == (parseInt(dy2.value)) )){
if(parseInt(dm1.value)  >= parseInt(dm2.value) ){
   wrong1=true;

}
 else{
wrong1= false; 
 }
}
else{
wrong1= false;
}


if(wrong1){

if(!((dy2.value == 13) || (dm2.value== 13))){
return false;
}
}

	return true;
	
	}




function validateList(i){
if (i==null) return false; 
if(isListEmpty(i.value)){
	i.focus();
return false;
}
return true;
}

function isListEmpty(s){
if ( s== null || s== 0){
	return true;
}
return false;
}



function isGroupEmpty(rad){

if ( rad == null ){
	return true;
}
var emp=true;


for (var i = 0; i < rad.length; i++)
    {  
		if (rad[i].checked) { emp=false;  break }
    }
    return emp;
	
	
}

function shomsg(msg){
   alert(msg);
}

//check if the number is a valid Jawwal number
function isJawwalNumber(j)
{
    first4digs = trim(j).substring(0,4);
	
	if( (first4digs == jfirst4digs) || (first4digs == jfirst4digs1) || (j.length == jnumberlength))
		return true;
	return false;
}

//check if number is a valid Cellecom number
function isCellcomNumber(j)
{
    first3digs = trim(j).substring(0,3);
	if( (first3digs != cfirst3digs) || (j.length != cnumberlength) )
		return false;
	return true;
}

//check if number is a valid Orange number
function isCellcomNumber(j)
{
    first3digs = trim(j).substring(0,3);
	if( (first3digs != cfirst3digs) || (j.length != cnumberlength) )
		return false;
	return true;
}



function isMobileNumber(num)
{	
	if (isWhitespace(trim(num)))
		return false;
	
	if( (num.substring(0,1) == '+') && (isInteger(num.substring(1))) && (num.length > 12)) 
		return true;
	
	if( !isInteger(num) )
		return false;
	if( (num.substring(0,2) == '00') && (num.length> 11))
		return true;
	if( !isJawwalNumber(num) )
		return false;
	
	/*if( isJawwalNumber(num) )
		return true;
	if( isCellcomNumber(num) )
		return true;
	if( isOrangeNumber(num) )
		return true;*/
	return true;
}
 
function showPopup(hi) {
		hp = document.getElementById("hoverpopup");
		//alert(hp);
		var pos = findPos(hi);
		
		// Set position of hover-over popup
		hp.style.top = pos[1] - 50;
		hp.style.left = pos[0];
		// Set popup to visible
		hp.style.visibility = "Visible";
}

function hidePopup() {
	hp = document.getElementById("hoverpopup");
	hp.style.visibility = "Hidden";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function autoIframe(frameId){
	try{
		frame = document.getElementById(frameId);
		innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
		objToResize = (frame.style) ? frame.style : frame;
		objToResize.height = innerDoc.body.scrollHeight + 10;
	}
	catch(err){
		window.status = err.message;
	}
}

function GroupValidation(){ 
	var txt = document.getElementById('GroupName');	
	if(txt.value == null || txt.value == ''){
		alert('الرجاء ادخال اسم المجموعة');
		return false;
	}
	else
		return true;
	return false;	
}	

function validateContactUs() {
	var name = document.getElementById('name');	
	if (isWhitespace(trim(name.value))){
    	shomsg('الرجاء ادخال الاسم');
		name.focus();
		return false;
	}
	var email = document.getElementById('email');	
	if (isWhitespace(trim(email.value))){
    	shomsg('الرجاء ادخال البريد الالكتروني');
		email.focus();
		return false;
	}
	
	if(!isEmail(email.value)){
		shomsg('خطأ في البريد الالكتروني');
		email.focus();
		return false;
	}
	
	var subject = document.getElementById('subject');	
	if (isWhitespace(trim(subject.value))){
    	shomsg('الرجاء ادخال الموضوع');
		subject.focus();
		return false;
	}
	var message = document.getElementById('message');	
	if (isWhitespace(trim(message.value))){
    	shomsg('الرجاء ادخال الرسالة');
		subject.focus();
		return false;
	}
	return true;
}

function DeleteR(x, t)
{
     var a = confirm ("Are you sure you want to delete this picture?")
     if (a==true) {
        window.location.href="deleteuserfile.jsp?fileName="+x+"&type="+t;
     }

}
