// form validation function //
	function trim(s)
	{
	  return s.replace(/^\s+|\s+$/,'');
	}
	
	function validate_ceiling(form)
	{
 
   var user_ceiling = form.user_ceiling.value;
 
   var date_ceiling = form.date_ceiling.value;
     
   var userfile = form.userfile.value;
   
  //alert(userfile);
  
 if(user_ceiling == "") {
    inlineMsg('user_ceiling','You must enter User name.',4);
    return false;
  }
        if(date_ceiling == "") {
    inlineMsg('date_ceiling','You must Select Date.',4);
    return false;
  }

   if(userfile == "") {
    inlineMsg('userfile','You must upload photo.',4);
    return false;
  }
     return true;
}

	
function vali_delivery(delivery)
 {
  var product_id = delivery.product_id.value;
  var distributor = delivery.distributor.value;
  var transportation = delivery.transportation.value;
  var deliverystat = delivery.deliverystat.value;
  
  
  if(product_id == "Select") {
  inlineMsg('product_id','<strong>Error</strong><br />You must select a product.',2);
  return false;
  }
  
  if(distributor == "") {
  inlineMsg('distributor','<strong>Error</strong><br />You must select a Distributor.',2);
  return false;
  }
  
  if(transportation == "Select") {
  inlineMsg('transportation','<strong>Error</strong><br />You must select a Transportation.',2);
  return false;
 }
 
 if(deliverystat == "Select") {
  inlineMsg('deliverystat','<strong>Error</strong><br />You must select a Delivery status.',2);
  return false;
 }
  
 
   return true;
		
}

function valincentive(user_register) 
{

  var incentive_type = user_register.incentive_type.options[user_register.incentive_type.selectedIndex].value;
  var incentive_name = user_register.incentive_name.options[user_register.incentive_name.selectedIndex].value;
  var individual = user_register.individual.options[user_register.individual.selectedIndex].value;
  var fromid = user_register.fromid.options[user_register.fromid.selectedIndex].value;
  var toid = user_register.toid.options[user_register.toid.selectedIndex].value;  
  var incentive_amount = user_register.incentive_amount.value;
  var product = user_register.product.options[user_register.product.selectedIndex].value;
  var numberRegex = /^[0-9]+/;

  var c_value = "";
for (var i=0; i < user_register.selectall.length; i++)
   {
   if (user_register.selectall[i].checked)
      {
      c_value = c_value + user_register.selectall[i].value + "\n";
	  
      }
   }
   
   if(!c_value)
   {
	inlineMsg('selectall','<strong>Error</strong><br />You must select distributors...',4);
   return false;
   }
   
c_value = trim(c_value);

individual== trim(individual);

   
   if(c_value == 'one')
   {  
if(individual == "Select") 
	{      
    inlineMsg('individual','<strong>Error</strong><br />You must select one distributor...',4);
    return false;
  }
   }
   
   
      if(c_value == 'ids')
   {  
if(fromid == "Selectfirstid") 
	{      
    inlineMsg('fromid','<strong>Error</strong><br />You must select starting distributor id...',4);
    return false;
  }
  
  if(toid == "Selectlastid") 
	{      
    inlineMsg('toid','<strong>Error</strong><br />You must select ending distributor id...',4);
    return false;
  }
  
   }
   
   
    if(c_value == 'product_all')
   {  
		if(product == "Selectproduct") 
		{      
    		inlineMsg('product','<strong>Error</strong><br />You must select one Product ...',4);
   			return false;
  		}
   }
   
   
   
   
    if(incentive_type == "NULL123") 
	{
    inlineMsg('incentive_type','<strong>Error</strong><br />You must select incentive type...',4);
    return false;
  }
  
    if(incentive_name == "NULL456") 
	{
    inlineMsg('incentive_name','<strong>Error</strong><br />You must select incentive name...',4);
    return false;
  }
  
    if(incentive_amount == "") {
    inlineMsg('incentive_amount','<strong>Error</strong><br />You must enter the amount...',2);
    return false;
  }
  if(!incentive_amount.match(numberRegex)) {
    inlineMsg('incentive_amount','<strong>Error</strong><br />The amount should be number...',2);
    return false;
  }
  
  return true;
}



function validate(user_register) {
  var newusername = user_register.newusername.value;
  var password1 = user_register.password1.value;
  var password2 = user_register.password2.value;
  var full_name = user_register.full_name.value;
  var last_name = user_register.last_name.value;
  var address = user_register.address.value;
  var address2 = user_register.address2.value; 
  var town = user_register.town.value; 
  var district = user_register.district.value; 
  var state = user_register.state.value; 
  var pin = user_register.pin.value; 
  var p_id = user_register.p_id.value;
  var passcode = user_register.passcode.value; 
  var mobile = user_register.mobile.value;
  var father_id = user_register.father_id.value; 
  var sponser_office = user_register.sponser_office.value; 
 
  var position = user_register.position.value; 
 
  
  

    if(newusername == "") {
    inlineMsg('newusername','<strong>Error</strong><br />You must Enter Username',4);
    return false;
  }
  
  
  if(password1 == "") {
    inlineMsg('password1','<strong>Error</strong><br />You must Enter Password',4);
    return false;
  }
  if(password1.length < 6 ) {
    inlineMsg('password1','<strong>Error</strong><br />Password length should be more than 6...',4);
    return false;
  }
  if(password2 == "") {
    inlineMsg('password2','<strong>Error</strong><br />Retype Your Password again',4);
    return false;
  }
    if(password2 != password1) {
    inlineMsg('password2','<strong>Error</strong><br />Password Mismatch',4);
    return false;
  }
  
   if(full_name == "") {
    inlineMsg('full_name','<strong>Error</strong><br />You must Enter First Name',4);
    return false;
  }
  
  if(last_name == "") {
    inlineMsg('last_name','<strong>Error</strong><br />You must Enter Last Name',4);
    return false;
  }
  
  if(address == "") {
    inlineMsg('address','<strong>Error</strong><br />You must Enter Addresss',4);
    return false;
  }
  if(address2 == "") {
    inlineMsg('address2','<strong>Error</strong><br />You must Enter Addresss',4);
    return false;
  }
  if(town == "") {
    inlineMsg('town','<strong>Error</strong><br />You must Enter Your Town',4);
    return false;
  }
  if(district == "") {
    inlineMsg('district','<strong>Error</strong><br />You must Enter District',4);
    return false;
  }
  if(state == "") {
    inlineMsg('state','<strong>Error</strong><br />You must Enter State',4);
    return false;
  }
  if(pin == "") {
    inlineMsg('pin','<strong>Error</strong><br />You must Enter Pincode',4);
    return false;
  }
  
   if(pin.length < 6 ) {
    inlineMsg('pin','<strong>Error</strong><br />Pincode Must Contain 6 Digits',4);
    return false;
  } 
    if(p_id == "") {
    inlineMsg('p_id','<strong>Error</strong><br />You must Select a product',4);
    return false;
  }
   if(passcode == "") {
    inlineMsg('passcode','<strong>Error</strong><br />You must fill Passcode',4);
    return false;
  }
   if(mobile == "") {
    inlineMsg('mobile','<strong>Error</strong><br />You must Enter Your Mobile No',4);
    return false;
  }
  
    if(mobile.length < 10) {
    inlineMsg('mobile','<strong>Error</strong><br />Enter 10 digit mobile no...',4);
    return false;
  }
    if(father_id == "") {
    inlineMsg('father_id','<strong>Error</strong><br />You must enter Sponser Code...',4);
    return false;
  }
  if(sponser_office == "") {
    inlineMsg('sponser_office','<strong>Error</strong><br />You must enter Sponser Name...',4);
    return false;
  }
  
    if(position == "") {
    inlineMsg('position','<strong>Error</strong><br />You must Select a Leg here...',4);
    return false;
  } 
  

  return true;
}


function validate_password(change_pass) {

  var current_pwd = change_pass.current_pwd.value;
  var new_pwd = change_pass.new_pwd.value;
  var confirm_pwd = change_pass.confirm_pwd.value;
 
 
 if(current_pwd == "") {
    inlineMsg('current_pwd','<strong>Error</strong><br />You must Enter Current Password',4);
    return false;
  }
 
  if(new_pwd == "") {
    inlineMsg('new_pwd','<strong>Error</strong><br />You must Enter New Password',4);
    return false;
  }
  if(new_pwd.length < 6 ) {
    inlineMsg('new_pwd','<strong>Error</strong><br />Password length should be more than 6...',4);
    return false;
  }
  if(confirm_pwd== "") {
    inlineMsg('confirm_pwd','<strong>Error</strong><br />Retype Your Password again',4);
    return false;
  }
    if(confirm_pwd != new_pwd) {
    inlineMsg('confirm_pwd','<strong>Error</strong><br />Password Mismatch',4);
    return false;
  }

  return true;
}





function validate_incentive(manage_incentive) {
  
  //alert('111213');
   var incentive_type = manage_incentive.incentive_type.value;
   var incentive_name = manage_incentive.incentive_name.value;
 
  if(incentive_type == "") {
    inlineMsg('incentive_type','You must select Incentive Type .',4);
    return false;
  }
    if(incentive_name == "") {
    inlineMsg('incentive_name','You must Enter Incentive Name.',4);

    return false;
  }

     return true;
}


function validate_code(code) {
   
   
   var product_name = code.product_name.value;
   var p_code = code.p_code.value;
 
    if(product_name == "") {
    inlineMsg('product_name','You must Select Product Name...',4);

    return false;
  }
   if(p_code == "") {
    inlineMsg('p_code','You must Enter Pass Code...',4);
    return false;
  }

     return true;
}





function validate_products(products) {
   
   var p_name = products.p_name.value;
   var p_point = products.p_point.value;
   var p_code = products.p_code.value;
   var p_caping = products.p_caping.value;
   var product_point = products.product_point.value;
   var product_referal = products.product_referal.value;
   var product_pair = products.product_pair.value;
  
  if(p_name == "") {
    inlineMsg('p_name','You must Enter Product Name...',4);
    return false;
  }  
  if(p_point == "") {
    inlineMsg('p_point','You must Enter Product Point...',4);
    return false;
  }
    if(p_code == "") {
    inlineMsg('p_code','You must Enter Product code...',4);

    return false;
  }
  
  if(p_caping == "") {
    inlineMsg('p_caping','You must Enter Product caping no...',4);

    return false;
  }
  
   if(product_point == "") {
    inlineMsg('product_point','You must Enter Multiple with Rs (Product)...',4);

    return false;
  }
  
  if(product_referal == "") {
    inlineMsg('product_referal','You must Enter Multiple with Rs (Direct Referral)...',4);

    return false;
  }
 if(product_pair == "") {
    inlineMsg('product_pair','You must Enter Multiple with Rs (Pair)...',4);

    return false;
  }
  

     return true;
}


function validate_sms(code) {
    
    var numbers = code.numbers.value;
   var word_count = code.word_count.value;
   

   
  if(numbers == "") {
    inlineMsg('numbers','You must Enter number to send sms...',4);
	disablesms();
    return false;
  }
  
  
    if(numbers.length <10) {
    inlineMsg('numbers','number must be 10 digit long...',4);
	disablesms();
    return false;
  }
  
    if(word_count == "") {
    inlineMsg('word_count','You must Enter Message...',4);
	disablesms();
    return false;
  }

 enablesms();
  return true;
}


function validate_sms1() {

 var c_value = "";
 var individual=document.send_sms.individual.value;
       var fromid = document.send_sms.fromid.value;
	     var toid = document.send_sms.toid.value;
		   var group = document.send_sms.group.value;
for (var i=0; i < document.send_sms.selectall.length; i++)
   {
   if (document.send_sms.selectall[i].checked)
      {
      c_value = c_value + document.send_sms.selectall[i].value + "\n";
	  
      }
   }
   
   if(!c_value)
   {
	inlineMsg('selectall','<strong>Error</strong><br />You must select distributors...',4);
   return false;
   }
   
  c_value = trim(c_value);

individual== trim(individual);


    if(c_value == 'one')
   {  
if(individual == "Select") 
	{      
    inlineMsg('individual','<strong>Error</strong><br />You must select one distributor from list...',4);
    return false;
  }
   }
    if(c_value == 'grp')
   {  
if(group == "") 
	{      
    inlineMsg('group','<strong>Error</strong><br />You must select one Group from list...',4);
    return false;
  }
   }
   
     if(c_value == 'ids')
   {  
if(fromid == "Selectfirstid") 
	{      
    inlineMsg('fromid','<strong>Error</strong><br />You must select starting distributor id...',4);
    return false;
  }
  
  if(toid == "Selectlastid") 
	{      
    inlineMsg('toid','<strong>Error</strong><br />You must select ending distributor id...',4);
    return false;
  }
   }
   
    enablesms();
  return true;
}

function validate_paymentreport(report) {
   
   var father_id = report.sponsor_id1.value;
  
   if(father_id == "") {
    inlineMsg('sponsor_id1','You must Select Sponsor Name...',4);
    return false;
  }

     return true;
}

function validate_profilereport(report1) {
   
   var father_id = report1.sponsor_id2.value;
  
   if(father_id == "") {
    inlineMsg('sponsor_id2','You must Select Sponsor Name...',4);
    return false;
  }

     return true;
}

function validate_productreport(report2) {
   
   var father_id = report2.sponsor_id3.value;
  
   if(father_id == "") {
    inlineMsg('sponsor_id3','You must Select Sponsor Name...',4);
    return false;
  }

     return true;
}


function validate_payoutreport(report6) {
   
   var point = report6.point.value;
  
   if(point == "") {
    inlineMsg('point','You must Enter Point...',4);
    return false;
  }

     return true;
}



function validate_password_other(change_pass_other) {

  var user_id = change_pass_other.user_id.value;
  var current_pwd = change_pass_other.current_pwd.value;
  var new_pwd = change_pass_other.new_pwd.value;
  var confirm_pwd = change_pass_other.confirm_pwd.value;
  
 if(user_id == "") {
    inlineMsg('user_id','<strong>Error</strong><br />You must Select Username First...',4);
    return false;
  }
 
 if(current_pwd == "") {
    inlineMsg('current_pwd','<strong>Error</strong><br />You must Enter Current Password',4);
    return false;
  }
 
  if(new_pwd == "") {
    inlineMsg('new_pwd','<strong>Error</strong><br />You must Enter New Password',4);
    return false;
  }
  if(new_pwd.length < 6 ) {
    inlineMsg('new_pwd','<strong>Error</strong><br />Password length should be more than 6...',4);
    return false;
  }
  if(confirm_pwd== "") {
    inlineMsg('confirm_pwd','<strong>Error</strong><br />Retype Your Password again',4);
    return false;
  }
    if(confirm_pwd != new_pwd) {
    inlineMsg('confirm_pwd','<strong>Error</strong><br />Password Mismatch',4);
    return false;
  }

  return true;
}

function validaterequest(training_request) 
{
  var tra_date = training_request.tra_date.value;
  var tra_place = training_request.tra_place.value;
 
  
 
  if(tra_date == "") {
    inlineMsg('tra_date','<strong>Error</strong><br />You must Select a date',4);
    return false;
  }
  
  if(tra_place == "") {
    inlineMsg('tra_place','<strong>Error</strong><br />You must select a place',4);
    return false;
  }
  return true;
}




function validatetraining(training) {
  var tra_det_club = training.tra_det_club.value;
  var tra_det_date = training.tra_det_date.value;
  var tra_det_place = training.tra_det_place.value;
  var tra_det_count = training.tra_det_count.value;
  
 
if(tra_det_club == "") {
    inlineMsg('tra_det_club','<strong>Error</strong><br />You must Select a Club',4);
    return false;
  }
  if(tra_det_date == "") {
    inlineMsg('jscal_trigger_Month1','<strong>Error</strong><br />You must Select a Date',4);
    return false;
  }
  
  if(tra_det_place == "") {
    inlineMsg('tra_det_place','<strong>Error</strong><br />You must Enter a place',4);
    return false;
  }
  
  if(tra_det_count == "") {
    inlineMsg('tra_det_count','<strong>Error</strong><br />You must Enter the count of members',4);
    return false;
  }
  return true;
}


// START OF MESSAGE SCRIPT //


var MSGTIMER = 20;
var MSGSPEED = 10;
var MSGOFFSET = 10;
var MSGHIDE = 100;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) 
{
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}